diff --git a/agenda/types.py b/agenda/types.py index 37fbe06..4f9d273 100644 --- a/agenda/types.py +++ b/agenda/types.py @@ -204,6 +204,10 @@ class Trip: else None ) + def total_co2_kg(self) -> float | None: + """Total CO₂ for trip.""" + return sum(float(t.get("co2_kg", 0)) for t in self.travel) + @property def flights(self) -> list[StrDict]: """Flights.""" diff --git a/templates/macros.html b/templates/macros.html index a7b67a7..9a7b068 100644 --- a/templates/macros.html +++ b/templates/macros.html @@ -113,7 +113,7 @@ {% endif %} {% endif %} - {% for i in range(8) %} + {% for i in range(9) %}
{% endfor %} @@ -143,6 +143,7 @@ {{ "{:,.0f} km / {:,.0f} miles".format(item.distance, item.distance / 1.60934) }} {% endif %} +
{{ "{:,.1f}".format(item.co2_kg) }} kg
{% endfor %} {% endmacro %} @@ -306,6 +307,7 @@ {% macro trip_item(trip) %} {% set distances_by_transport_type = trip.distances_by_transport_type() %} {% set total_distance = trip.total_distance() %} + {% set total_co2_kg = trip.total_co2_kg() %} {% set end = trip.end %}

@@ -335,6 +337,7 @@

{% endif %} + {% if distances_by_transport_type %} {% for transport_type, distance in distances_by_transport_type %}
@@ -344,6 +347,10 @@ {% endfor %} {% endif %} + {% if total_co2_kg %} +
Total CO₂: {{ "{:,.1f}".format(total_co2_kg) }} kg
+ {% endif %} + {{ conference_list(trip) }} {% for day, elements in trip.elements_grouped_by_day() %} @@ -381,6 +388,7 @@ duration: {{ e.detail.duration }} {% endif %} {#
{{ e.detail | pprint }}
#} + CO₂: {{ "{:,.1f}".format(e.detail.co2_kg) }} kg {% endif %} {% if e.detail.distance %} distance: {{ format_distance(e.detail.distance) }} diff --git a/templates/travel.html b/templates/travel.html index 06d1d4e..ebb937b 100644 --- a/templates/travel.html +++ b/templates/travel.html @@ -3,7 +3,7 @@ {% block title %}Travel - Edward Betts{% endblock %} -{% set flight_column_count = 10 %} +{% set flight_column_count = 11 %} {% set column_count = 10 %} {% block style %} @@ -47,6 +47,7 @@
flight
tracking
distance
+
CO₂ (kg)
{% for item in flights %} {{ flight_booking_row(item) }} diff --git a/templates/trip/list.html b/templates/trip/list.html index afb5c2c..2e9fe5a 100644 --- a/templates/trip/list.html +++ b/templates/trip/list.html @@ -62,6 +62,7 @@ distance: {{format_distance(distance) }}
{% endfor %} +
Total CO₂: {{ "{:,.1f}".format(total_co2_kg / 1000.0) }} tonnes
{% for trip in items %} {{ trip_item(trip) }} diff --git a/update.py b/update.py index 30b65a7..2c94e6a 100755 --- a/update.py +++ b/update.py @@ -8,7 +8,7 @@ import typing from datetime import date, datetime from time import time -import deepdiff # type: ignore +import deepdiff import flask import requests import yaml diff --git a/web_view.py b/web_view.py index 70c48dd..62222be 100755 --- a/web_view.py +++ b/web_view.py @@ -404,13 +404,17 @@ def calc_total_distance(trips: list[Trip]) -> float: """Total distance for trips.""" total = 0.0 for item in trips: - dist = item.total_distance() - if dist: + if dist := item.total_distance(): total += dist return total +def calc_total_co2_kg(trips: list[Trip]) -> float: + """Total CO₂ for trips.""" + return sum(item.total_co2_kg() for item in trips) + + def sum_distances_by_transport_type(trips: list[Trip]) -> list[tuple[str, float]]: """Sum distances by transport type.""" distances_by_transport_type: defaultdict[str, float] = defaultdict(float) @@ -443,6 +447,7 @@ def trip_past_list() -> str: format_list_with_ampersand=format_list_with_ampersand, fx_rate=agenda.fx.get_rates(app.config), total_distance=calc_total_distance(past), + total_co2_kg=calc_total_co2_kg(past), distances_by_transport_type=sum_distances_by_transport_type(past), ) @@ -475,6 +480,7 @@ def trip_future_list() -> str: format_list_with_ampersand=format_list_with_ampersand, fx_rate=agenda.fx.get_rates(app.config), total_distance=calc_total_distance(current + future), + total_co2_kg=calc_total_co2_kg(current + future), distances_by_transport_type=sum_distances_by_transport_type(current + future), )