-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
365 lines (306 loc) · 10.1 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
""" This module contains our UI and front end code that users can interact with in order
to create and visualize routes to visit up to 6 MLB stadiums on a path.
"""
from datetime import date
from dash import Dash, html, dcc, callback, Output, Input, dash_table, no_update
from dash.exceptions import PreventUpdate
import plotly.graph_objects as go
import dash_loading_spinners as dls
import dash_bootstrap_components as dbc
import pandas as pd
import numpy as np
from make_route.exhaustive_search import (
reduce_routes,
reduce_schedule,
sort_order,
find_all_routes,
)
df = pd.read_csv("data/final_mlb_schedule.csv")
cost_dfx = pd.read_csv("data/cost_df.csv")
schedule = df.copy()
schedule["date"] = pd.to_datetime(schedule["date"])
fig = go.Figure(
go.Scattermapbox(
mode="markers+lines", lon=[], lat=[], hovertext=[], marker={"size": 10}
)
)
fig.update_layout(
margin={"l": 0, "t": 0, "b": 0, "r": 0},
mapbox={
"center": {"lat": 37.0902, "lon": -95.7129},
"style": "open-street-map",
"zoom": 3,
},
)
app = Dash(__name__, external_stylesheets=[dbc.themes.COSMO])
SIDEBAR_STYLE = {
"position": "fixed",
"top": 0,
"left": 0,
"bottom": 0,
"width": "20rem",
"padding": "2rem 1rem",
"background-color": "#f8f9fa",
}
CONTENT_STYLE = {
"margin-left": "22rem",
"margin-right": "22rem",
"padding": "2rem 1rem",
}
instructions_label = html.H5(
"Choose up to 6 MLB teams you would like to visit this season within \
the time frame you want. \
We will create the optimal route that is optimized by time, distance or cost."
)
date_picker = dcc.DatePickerRange(
# date range
id="date-selection",
min_date_allowed=date(2024, 3, 20),
max_date_allowed=date(2024, 9, 29),
initial_visible_month=date(2024, 3, 20),
# end_date=date(2024, 9, 29)
style={"margin-top": "15px"},
)
dropdown = dcc.Dropdown(
# Shows all teams in a drop down list to choose from
np.sort(df["home team"].unique()).tolist(),
multi=True,
id="dropdown-selection",
placeholder="Select teams you wish to visit",
style={"margin-top": "15px"},
)
metrics_label = html.H5("Optimize for: ", style={"margin-top": "15px"})
radio_items = dcc.RadioItems(
options=[
{"label": "Time", "value": "time"},
{"label": "Distance", "value": "distance"},
{"label": "Cost", "value": "cost"},
],
value="time",
id="prioritize",
# inline=True
)
total_table = dash_table.DataTable(
id="total_table",
style_cell={"padding": "5px"},
style_header={"backgroundColor": "white", "fontWeight": "bold"},
)
selection_sidebar = html.Div(
[
instructions_label,
date_picker,
dropdown,
metrics_label,
radio_items,
total_table,
],
style=SIDEBAR_STYLE,
)
schedule_table = dash_table.DataTable(
id="table",
style_cell={"padding": "5px", "margin-top": "15px"},
style_header={"fontWeight": "bold"},
# style_header={"backgroundColor": "white", "fontWeight": "bold",'color':'w'},
)
path = html.Div(id="image_route", children=[], style={"margin-top": "15px"})
content = html.Div(
[
dls.BallTriangle(dcc.Graph(figure=fig, id="graph-content")),
schedule_table,
path,
dcc.Store(
id="remaining_team_list",
storage_type="memory",
data=np.sort(df["home team"].unique()).tolist(),
),
],
style=CONTENT_STYLE,
)
app.layout = html.Div(
[
dbc.Col(selection_sidebar, width=2),
dbc.Col(
[
dbc.Row(
html.H1(
children="Baseball Traveling Fans(man)",
style={
"textAlign": "center",
"margin": "auto",
"margin-left": "270px",
"margin-top": "20px",
},
)
),
# "padding": "2rem 1rem",
# 'margin':'auto'}),
dbc.Row(content),
],
width=9,
),
]
)
@callback(
[Output("dropdown-selection", "options"), Output("remaining_team_list", "data")],
Input("dropdown-selection", "value"),
Input("remaining_team_list", "data"),
prevent_initial_call=True,
)
def update_dropdown(teams_selected, remaining_teams):
"""
Updates the dropdown selections to remove teams already selected as well as not allow
anymore selections once the user selects 6 teams.
"""
# Limit the number of teams selected to 6
if teams_selected is not None and len(teams_selected) > 5:
return teams_selected, remaining_teams
return remaining_teams, remaining_teams
@callback(
Output("graph-content", "figure"),
Input("dropdown-selection", "value"),
Input("date-selection", "start_date"),
Input("date-selection", "end_date"),
Input("prioritize", "value"),
)
# pylint: disable=too-many-locals
def update_graph(teams, start_date, end_date, sort_method):
"""
Updates the map and plots the new route calculated based on the parameters.
"""
dff = df.copy()
dff = dff.groupby("home team").first().reset_index()
sched = df.copy()
if teams is None or len(teams) < 2:
sched = sched[sched["home team"].isin([])]
else:
teamlist = teams
short_sched = reduce_schedule(schedule, teamlist, start_date, end_date)
routes = find_all_routes(teamlist)
route_df = reduce_routes(routes, short_sched, cost_dfx)
sorted_route = sort_order(route_df, sort_method)
sched = sorted_route["games"][0]
team_lons = sched["Longitude"].tolist()
team_lats = sched["Latitude"].tolist()
team_home = sched["home team"].tolist()
team_away = sched["away team"].tolist()
order = list(range(len(sched)))
order_final = []
for rank in order:
order_final.append(str(rank + 1))
sched["date"] = pd.to_datetime(sched["date"])
sched["date"] = sched["date"].dt.strftime("%m-%d-%Y")
dates = sched["date"].tolist()
time = sched["time"].tolist()
hover_data_zipped = list(zip(team_home, team_away, dates, time))
hover_data_format = []
for point in hover_data_zipped:
hover_data_format.append(
f"{point[1]} at {point[0]} on {point[2]} at {point[3]}"
)
new_fig = go.Figure(
go.Scattermapbox(
mode="markers+lines+text",
lon=team_lons,
lat=team_lats,
hovertext=hover_data_format,
hoverinfo="text",
text=order_final,
textposition="top center",
# textposition='TopCenter',
marker={"size": 10},
)
)
new_fig.update_layout(
margin={"l": 0, "t": 0, "b": 0, "r": 0},
mapbox={
"center": {"lat": 37.0902, "lon": -95.7129},
"style": "open-street-map",
"zoom": 3,
},
font={
"family": "Courier New, monospace",
"size": 25, # Set the font size here
"color": "blue",
},
)
return new_fig
@callback(
Output("image_route", "children"),
Input("table", "data"),
)
def update_image_path(table_data):
"""
Updates the route logos below the main table.
"""
if table_data is None or len(table_data) == 0:
raise PreventUpdate
new_images = []
new_images.append(html.H5("Route: ", style={"margin-top": "15px"}))
for team_dict_ind, team_dict in enumerate(table_data):
if team_dict_ind != 0:
arrow = html.Img(
src="assets/arrow_2.png", alt="arrow", style={"width": "50px"}
)
new_images.append(arrow)
team = team_dict["Home Team"]
team = team.replace(" ", "")
img = html.Img(src=f"assets/{team}.png", alt=team, style={"width": "100px"})
new_images.append(img)
return new_images
@callback(
Output("table", "data"),
Input("dropdown-selection", "value"),
Input("date-selection", "start_date"),
Input("date-selection", "end_date"),
Input("prioritize", "value"),
)
def update_game_schedule_table(teams, start_date, end_date, sort_method):
"""
Updates the game schedule table based on the parameters and route
calculation.
"""
if teams is None or len(teams) < 2:
return no_update
teamlist = teams
short_sched = reduce_schedule(schedule, teamlist, start_date, end_date)
routes = find_all_routes(teamlist)
route_df = reduce_routes(routes, short_sched, cost_dfx)
sorted_route = sort_order(route_df, sort_method)
sched = sorted_route["games"][0]
sched["date"] = sched["date"].dt.strftime("%m-%d-%Y")
sched = sched.reset_index()
sched["Route Order"] = sched["index"] + 1
sched = sched[["Route Order", "date", "time", "home team", "away team"]]
sched.columns = ["Route Order", "Date", "Time", "Home Team", "Away Team"]
return sched.to_dict("records")
@callback(
Output("total_table", "data"),
Input("dropdown-selection", "value"),
Input("date-selection", "start_date"),
Input("date-selection", "end_date"),
Input("prioritize", "value"),
)
# pylint: disable=consider-using-f-string
def update_sidebar_metrics_table(teams, start_date, end_date, sort_method):
"""
Updates the sidebar metrics table based on the parameters and route
calculation.
"""
if teams is None or len(teams) < 2:
return no_update
teamlist = teams
short_sched = reduce_schedule(schedule, teamlist, start_date, end_date)
routes = find_all_routes(teamlist)
route_df = reduce_routes(routes, short_sched, cost_dfx)
sorted_route = sort_order(route_df, sort_method)
metrics = ["Time", "Distance", "Travel Cost"]
metric_val = [
str(sorted_route["time"][0]) + " days",
str(sorted_route["distance"][0]) + " miles",
"${0:.2f}".format(sorted_route["cost"][0]),
]
totals = pd.DataFrame({"Metric": metrics, "Total": metric_val})
return totals.to_dict("records")
if __name__ == "__main__":
# temp change to debug
app.run(debug=False)