Update trip statistics CO2 display with transport breakdown

- Change label from "Flight CO₂" to "Total CO₂" for accuracy
- Add CO₂ breakdown by transport type (flight/train/ferry)
- Show values in kg for <1000kg, tonnes for ≥1000kg
- Track CO₂ emissions by transport type in stats calculation

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Edward Betts 2025-07-15 16:04:07 +02:00
parent 5a15f48f50
commit 06a7d03404
2 changed files with 23 additions and 1 deletions

View file

@ -10,8 +10,12 @@ def travel_legs(trip: Trip, stats: StrDict) -> None:
"""Calculate stats for travel legs."""
for leg in trip.travel:
stats.setdefault("co2_kg", 0)
stats.setdefault("co2_by_transport_type", {})
if "co2_kg" in leg:
stats["co2_kg"] += leg["co2_kg"]
transport_type = leg["type"]
stats["co2_by_transport_type"].setdefault(transport_type, 0)
stats["co2_by_transport_type"][transport_type] += leg["co2_kg"]
if leg["type"] == "flight":
stats.setdefault("flight_count", 0)
stats.setdefault("airlines", Counter())

View file

@ -45,7 +45,25 @@
{% endif %}
</div>
{% if year_stats.co2_kg %}
<div>Flight CO₂: {{ "{:,.1f}".format(year_stats.co2_kg / 1000.0) }} tonnes</div>
<div>Total CO₂:
{% if year_stats.co2_kg >= 1000 %}
{{ "{:,.2f}".format(year_stats.co2_kg / 1000.0) }} tonnes
{% else %}
{{ "{:,.0f}".format(year_stats.co2_kg) }} kg
{% endif %}
</div>
{% if year_stats.co2_by_transport_type %}
{% for transport_type, co2_kg in year_stats.co2_by_transport_type.items() %}
<div style="margin-left: 20px;">
{{ transport_type | title }} CO₂:
{% if co2_kg >= 1000 %}
{{ "{:,.2f}".format(co2_kg / 1000.0) }} tonnes
{% else %}
{{ "{:,.0f}".format(co2_kg) }} kg
{% endif %}
</div>
{% endfor %}
{% endif %}
{% endif %}
<div>Trains segments in {{ year }}: {{ year_stats.train_count or 0 }}</div>
<div>Total distance in {{ year}}: {{ format_distance(year_stats.total_distance or 0) }}</div>