diff --git a/apis/interactive_maps.py b/apis/interactive_maps.py index 7338f08..e0533af 100644 --- a/apis/interactive_maps.py +++ b/apis/interactive_maps.py @@ -3,13 +3,14 @@ import folium class SwissMap: def __init__(self) -> None: - self.m = folium.Map(location=[46.8132, 8.2242], tiles='OpenStreetMap', zoom_start=7.6, min_zoom = 6, max_zoom = 12) + self.m = folium.Map(location=[46.8132, 8.2242], tiles='OpenStreetMap', zoom_start=6.5, min_zoom = 6, max_zoom = 12) # Returns travel map of customer as html string - def travel_history_map(self, customer_id, location_list): + def travel_history_map(self, customer_id, travel_history): local_m = self.m - for place, coordinates in location_list: + for e in travel_history: + place, coordinates = e.location_name, e.location_coordinates folium.Marker( location=coordinates, # coordinates for the marker popup=place, # pop-up label for the marker diff --git a/dummy_data.py b/dummy_data.py new file mode 100644 index 0000000..90b25dd --- /dev/null +++ b/dummy_data.py @@ -0,0 +1,54 @@ +import datetime + +def populate_data(Users, Events): + Events.add_event( + id = 1, + location_name = 'Center of Switzerland', + location_coordinates = [46.8132, 8.2242], + date = datetime.date.today() - datetime.timedelta(days=100) + ) + Events.add_event( + id = 2, + location_name = 'Center of Switzerland', + location_coordinates = [46, 8], + date = datetime.date.today() - datetime.timedelta(days=1) + ) + Events.add_event( + id = 3, + location_name = 'Center of Switzerland', + location_coordinates = [46.8132, 9], + date = datetime.date.today() - datetime.timedelta(days=2) + ) + Events.add_event( + id = 4, + location_name = 'Center of Switzerland', + location_coordinates = [47, 8.2242], + date = datetime.date.today() - datetime.timedelta(days=3) + ) + Events.add_event( + id = 5, + location_name = 'Center of Switzerland', + location_coordinates = [40.8132, 8.2242], + date = datetime.date.today() - datetime.timedelta(days=56) + ) + Events.add_event( + id = 6, + location_name = 'Zermatt', + location_coordinates = [46.11654, 10.445683], + date = datetime.date.today() + ) + + Users.add_user( + id=239842123, + name="Remy", + event_preferences=["hiking","skiing"], + event_blacklist = [], + home_coordinates=[0,0], + group_size=1, + min_age=20, + max_age=20, + ) + u = Users.get_by_id(239842123) + for e in Events: + u.travel_history.append(e) + diff --git a/models/event.py b/models/event.py index 7db3d61..8305bd1 100644 --- a/models/event.py +++ b/models/event.py @@ -1,16 +1,22 @@ class Event: - coordinates = [] + id = 0 + location_name = [] + location_coordinates = [] reviews = [] category = [] weather_requirements = 0 - - trip_to = "" + date = "" + + duration = "" # datetime object + trip_to = "" # Trip object trip_back = "" def __init__(self, **kwargs): - pass - + self.id = kwargs.pop("id") + self.location_name = kwargs.pop("location_name") + self.location_coordinates = kwargs.pop("location_coordinates") + self.date = kwargs.pop("date") def find_optimal_trip(self): pass @@ -19,6 +25,12 @@ class Event: def trip_is_good(self): pass + @property + def co2_savings(self): + try: + return self.trip_to.co2_savings + self.trip_back.co2_savings + except: + return 5 class Review: @@ -35,4 +47,16 @@ class Review: def add_photo(self, photo): pass +class Events: + _events = [] + def add_event(self, **kwargs): + self._events.append(Event(**kwargs)) + + def get_by_id(self, id): + for e in self.events: + if e.id == id: + return e + + def __iter__(self): + return iter(self._events) \ No newline at end of file diff --git a/models/user.py b/models/user.py index 3fda0b6..45410c7 100644 --- a/models/user.py +++ b/models/user.py @@ -7,8 +7,7 @@ class User: group_size = 0 min_age = 0 max_age = 0 - travel_history = [] # (Name, coordinates) - co2_savings = [] + travel_history = [] # (list of Events) def __init__(self, **kwargs): self.id = kwargs.pop("id") @@ -27,7 +26,8 @@ class User: @property def total_co2_savings(self): - return sum(self.co2_savings) + return sum([e.co2_savings for e in self.travel_history]) + class Users: diff --git a/server.py b/server.py index bc15cca..4e24174 100644 --- a/server.py +++ b/server.py @@ -3,11 +3,16 @@ import random from apis.interactive_maps import SwissMap from models.user import Users +from models.event import Events +import plotly.graph_objects as go +from plotly.offline import plot USERBASE = Users() -USERBASE.add_user(id=239842123, name="Remy", event_preferences=["hiking","skiing"], event_blacklist = [], home_coordinates=[0,0], group_size=1, min_age=20, max_age=20) MAP = SwissMap() +EVENTBASE = Events() +import dummy_data +dummy_data.populate_data(USERBASE, EVENTBASE) app = Flask(__name__) app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT' @@ -30,11 +35,30 @@ def index(): def profile(): uid = session["user_id"] user = USERBASE.get_by_id(uid) + days, co2_savings = [], [] + + for e in user.travel_history: + days.append(e.date.replace(day=1)) + co2_savings.append(e.co2_savings) + + fig = go.Figure() + fig.layout.update( + xaxis = {'showgrid': False}, + yaxis = {'showgrid': False}, + showlegend=False, + margin=dict(l=0, r=0, t=0, b=0), + paper_bgcolor='rgba(0,0,0,0)', + plot_bgcolor='rgba(0,0,0,0)', + colorway=["#D50505", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf"] + ) + fig.add_trace(go.Bar(x=days, y=co2_savings)) + plot_div = plot(fig, output_type='div', include_plotlyjs=False) context = { - "map_path" : MAP.travel_history_map(user.id, user.travel_history) + "map_path" : MAP.travel_history_map(user.id, user.travel_history), + "plotly_html" : plot_div } - return render_template("user_detail.html", conbtext=context, user=user) + return render_template("user_detail.html", context=context, user=user) diff --git a/static/SBB-Logo.png b/static/SBB-Logo.png deleted file mode 100644 index b5d9933..0000000 Binary files a/static/SBB-Logo.png and /dev/null differ diff --git a/static/destinationmap_customer=235_destinationZermatt.html b/static/destinationmap_customer=235_destinationZermatt.html index 1dc43a6..db181b8 100644 --- a/static/destinationmap_customer=235_destinationZermatt.html +++ b/static/destinationmap_customer=235_destinationZermatt.html @@ -23,7 +23,7 @@ - - - - - - - - - - - - - - - - - - -
- - - \ No newline at end of file diff --git a/static/travelhistorymap_customer235.html b/static/travelhistorymap_customer235.html index 7c96fd0..3ed95f1 100644 --- a/static/travelhistorymap_customer235.html +++ b/static/travelhistorymap_customer235.html @@ -23,7 +23,7 @@ + + + + + + + + + + + + + + + + + + +
+ + + \ No newline at end of file diff --git a/templates/user_detail.html b/templates/user_detail.html index 6b6a246..703ca01 100644 --- a/templates/user_detail.html +++ b/templates/user_detail.html @@ -1,5 +1,7 @@ {% extends "base.html" %} {% block content %} + +
@@ -7,24 +9,19 @@
Previous destinations
- +
-
Travel overview
- + {{ context.plotly_html|safe }}
{% endblock %}