Add CO2 breakdown by transport type to trip list and detail pages
Implements bug #194 by adding CO2 emission display by transport type: - Add co2_by_transport_type() method to Trip class - Display CO2 breakdown on trip list page and individual trip items - Display CO2 breakdown on individual trip detail pages - Show both total CO2 and breakdown by transport type (flight/train/ferry) 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
af49dac232
commit
a68df381fe
|
@ -230,6 +230,21 @@ class Trip:
|
|||
|
||||
return list(transport_distances.items())
|
||||
|
||||
def co2_by_transport_type(self) -> list[tuple[str, float]]:
|
||||
"""Calculate the total CO₂ emissions for each type of transport.
|
||||
|
||||
Any travel item with a missing or None 'co2_kg' field is ignored.
|
||||
"""
|
||||
transport_co2: defaultdict[str, float] = defaultdict(float)
|
||||
|
||||
for item in self.travel:
|
||||
co2_kg = item.get("co2_kg")
|
||||
if co2_kg:
|
||||
transport_type: str = item.get("type", "unknown")
|
||||
transport_co2[transport_type] += float(co2_kg)
|
||||
|
||||
return list(transport_co2.items())
|
||||
|
||||
def elements(self) -> list[TripElement]:
|
||||
"""Trip elements ordered by time."""
|
||||
elements: list[TripElement] = []
|
||||
|
|
|
@ -351,6 +351,16 @@
|
|||
<div>Total CO₂: {{ "{:,.1f}".format(total_co2_kg) }} kg</div>
|
||||
{% endif %}
|
||||
|
||||
{% set co2_by_transport = trip.co2_by_transport_type() %}
|
||||
{% if co2_by_transport %}
|
||||
{% for transport_type, co2_kg in co2_by_transport %}
|
||||
<div>
|
||||
{{ transport_type | title }}
|
||||
CO₂: {{ "{:,.1f}".format(co2_kg) }} kg
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
{% if trip.schengen_compliance %}
|
||||
<div>
|
||||
<strong>Schengen:</strong>
|
||||
|
|
|
@ -64,6 +64,13 @@
|
|||
{% endfor %}
|
||||
<div>Total CO₂: {{ "{:,.1f}".format(total_co2_kg / 1000.0) }} tonnes</div>
|
||||
|
||||
{% for transport_type, co2_kg in co2_by_transport_type %}
|
||||
<div>
|
||||
{{ transport_type | title }}
|
||||
CO₂: {{ "{:,.1f}".format(co2_kg) }} kg
|
||||
</div>
|
||||
{% endfor %}
|
||||
|
||||
{% for trip in items %}
|
||||
{{ trip_item(trip) }}
|
||||
{% endfor %}
|
||||
|
|
|
@ -78,6 +78,8 @@
|
|||
{% set end = trip.end %}
|
||||
{% set total_distance = trip.total_distance() %}
|
||||
{% set distances_by_transport_type = trip.distances_by_transport_type() %}
|
||||
{% set total_co2_kg = trip.total_co2_kg() %}
|
||||
{% set co2_by_transport_type = trip.co2_by_transport_type() %}
|
||||
|
||||
{% block content %}
|
||||
<div class="row">
|
||||
|
@ -112,6 +114,18 @@
|
|||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
{% if total_co2_kg %}
|
||||
<div>Total CO₂: {{ "{:,.1f}".format(total_co2_kg) }} kg</div>
|
||||
{% endif %}
|
||||
|
||||
{% if co2_by_transport_type %}
|
||||
{% for transport_type, co2_kg in co2_by_transport_type %}
|
||||
<div>{{ transport_type | title }} CO₂:
|
||||
{{ "{:,.1f}".format(co2_kg) }} kg
|
||||
</div>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
|
||||
|
||||
{% set delta = human_readable_delta(trip.start) %}
|
||||
{% if delta %}
|
||||
|
|
Loading…
Reference in a new issue