Compare commits

...

2 commits

Author SHA1 Message Date
Edward Betts e1688629a3 Trip distance by means of transport: air and rail
Closes: #148
2024-04-16 12:41:00 +01:00
Edward Betts dce8fde29a Update template 2024-04-16 12:12:19 +01:00
2 changed files with 44 additions and 16 deletions

View file

@ -2,6 +2,7 @@
import datetime
import typing
from collections import Counter
from dataclasses import dataclass, field
from pycountry.db import Country
@ -141,6 +142,25 @@ class Trip:
else None
)
def distances_by_transport_type(self) -> list[tuple[str, float]]:
"""Calculate the total distance travelled for each type of transport.
Any travel item with a missing or None 'distance' field is ignored.
"""
transport_distances: Counter[float] = Counter()
for item in self.travel:
distance = item.get("distance")
if distance:
transport_type = item.get("type", "unknown")
transport_distances[transport_type] += distance
return list(transport_distances.items())
# Example usage:
# You would call the function with your travel list here to get the results.
@dataclass
class Holiday:

View file

@ -61,6 +61,7 @@
{% set end = trip.end %}
{% set total_distance = trip.total_distance() %}
{% set distances_by_transport_type = trip.distances_by_transport_type() %}
{% block content %}
<div class="row">
@ -87,6 +88,14 @@
</div>
{% endif %}
{% if distances_by_transport_type %}
{% for transport_type, distance in distances_by_transport_type %}
<div>{{ transport_type | title }} distance:
{{ "{:,.0f} km / {:,.0f} miles".format(distance, distance / 1.60934) }}
</div>
{% endfor %}
{% endif %}
{% set delta = human_readable_delta(trip.start) %}
{% if delta %}
@ -248,23 +257,22 @@
<div class="mt-3">
<h4>Holidays</h4>
{% if holidays %}
<table class="table table-hover w-auto">
{% for item in holidays %}
{% set country = get_country(item.country) %}
<tr>
{% if loop.first or item.date != loop.previtem.date %}
<td class="text-end">{{ display_date(item.date) }}</td>
{% else %}
<td></td>
{% endif %}
<td>{{ country.flag }} {{ country.name }}</td>
<td>{{ item.display_name }}</td>
</tr>
{% endfor %}
</table>
<table class="table table-hover w-auto">
{% for item in holidays %}
{% set country = get_country(item.country) %}
<tr>
{% if loop.first or item.date != loop.previtem.date %}
<td class="text-end">{{ display_date(item.date) }}</td>
{% else %}
<td></td>
{% endif %}
<td>{{ country.flag }} {{ country.name }}</td>
<td>{{ item.display_name }}</td>
</tr>
{% endfor %}
</table>
{% else %}
<p>No public holidays during trip.</p>
<p>No public holidays during trip.</p>
{% endif %}
</div>