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 3fb3b0a..9a7b068 100644 --- a/templates/macros.html +++ b/templates/macros.html @@ -143,7 +143,7 @@ {{ "{:,.0f} km / {:,.0f} miles".format(item.distance, item.distance / 1.60934) }} {% endif %} </div> - <div class="grid-item text-end">{{ item.co2_kg }} kg</div> + <div class="grid-item text-end">{{ "{:,.1f}".format(item.co2_kg) }} kg</div> {% endfor %} {% endmacro %} @@ -307,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 %} <div class="border border-2 rounded mb-2 p-2"> <h3> @@ -336,6 +337,7 @@ </div> {% endif %} + {% if distances_by_transport_type %} {% for transport_type, distance in distances_by_transport_type %} <div> @@ -345,6 +347,10 @@ {% endfor %} {% endif %} + {% if total_co2_kg %} + <div>Total CO₂: {{ "{:,.1f}".format(total_co2_kg) }} kg</div> + {% endif %} + {{ conference_list(trip) }} {% for day, elements in trip.elements_grouped_by_day() %} @@ -382,6 +388,7 @@ <span class="text-nowrap"><strong>duration:</strong> {{ e.detail.duration }}</span> {% endif %} {# <pre>{{ e.detail | pprint }}</pre> #} + <span class="text-nowrap"><strong>CO₂:</strong> {{ "{:,.1f}".format(e.detail.co2_kg) }} kg</span> {% endif %} {% if e.detail.distance %} <span class="text-nowrap"><strong>distance:</strong> {{ format_distance(e.detail.distance) }}</span> 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) }} </div> {% endfor %} + <div>Total CO₂: {{ "{:,.1f}".format(total_co2_kg / 1000.0) }} tonnes</div> {% for trip in items %} {{ trip_item(trip) }} 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), )