-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
306 lines (284 loc) · 12.3 KB
/
app.py
File metadata and controls
306 lines (284 loc) · 12.3 KB
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
from datetime import datetime
import dash_ag_grid as dag
import dash_mantine_components as dmc
import pandas as pd
import plotly.express as px
from dash import Dash, Input, Output, callback, dcc, html
app = Dash(__name__)
server = app.server
app.title = "Montreal Events"
app.index_string = """
<!DOCTYPE html>
<html>
<head>
{%metas%}
<title>Montreal Events</title>
<meta name="title" content="Montreal Events" />
<meta name="description" content="Discover and explore public events happening in Montreal with interactive maps and filtering." />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://evenements-de-montreal.plotly.app/" />
<meta property="og:title" content="Montreal Events" />
<meta property="og:description" content="Discover and explore public events happening in Montreal with interactive maps and filtering." />
<meta property="og:image" content="https://evenements-de-montreal.plotly.app/assets/thumbnail.png" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:url" content="https://evenements-de-montreal.plotly.app/" />
<meta name="twitter:title" content="Montreal Events" />
<meta name="twitter:description" content="Discover and explore public events happening in Montreal with interactive maps and filtering." />
<meta name="twitter:image" content="https://evenements-de-montreal.plotly.app/assets/thumbnail.png" />
{%favicon%}
{%css%}
</head>
<body>
{%app_entry%}
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
</body>
</html>
"""
df = pd.read_csv("evenements.csv")
def generate_map(df):
scatter_map = px.scatter_map(
df,
lat="lat",
lon="long",
color="type_evenement",
color_discrete_sequence=px.colors.qualitative.Prism,
zoom=10,
hover_data=["titre"],
)
scatter_map.update_layout(
margin={"l": 0, "r": 0, "t": 0, "b": 50},
legend=dict(
orientation="h",
yanchor="bottom",
y=-0.2,
xanchor="center",
x=0.5,
title=None,
),
)
return scatter_map
def generate_dropdown(col, label, value=[]):
data = pd.read_csv("evenements.csv")
return dmc.MultiSelect(
data=[{"label": i, "value": i} for i in data[col].dropna().unique()],
label=label,
placeholder="Choisir...",
value=value,
id=col,
)
app.layout = dmc.MantineProvider(
html.Div(
[
html.Div(
[
html.Div(
[
dcc.Markdown(
"""
**Événements de Montréal**
Filtrez les événements ci dessous et consultez la carte et cliquez sur une ligne pour voir les détails.
"""
),
dmc.Divider(),
generate_dropdown("type_evenement", "Événement(s)"),
generate_dropdown("emplacement", "Emplacement(s)"),
generate_dropdown("arrondissement", "Arrondissement(s)"),
dmc.DatePickerInput(
id="date_debut",
label="Date de Début",
value=datetime.now(),
),
dmc.DatePickerInput(
id="date_fin",
label="Date de Fin",
placeholder="Choisir...",
),
],
className="card sidebar",
),
html.Div(
[
html.Div(
[
html.Div(
id="search-results", className="card small-card"
),
html.Div(
id="event-info", className="card small-card"
),
html.Div(
id="event-date", className="card small-card"
),
html.Div(
id="event-description",
className="card small-card",
),
],
className="row",
),
html.Div(
[
html.Div(
[dcc.Graph(id="map")],
className="card medium-card",
),
html.Div(
[
dag.AgGrid(
id="grid",
columnDefs=[
{
"field": "titre",
"checkboxSelection": True,
"minWidth": 300,
},
{
"field": "date_debut",
"headerName": "Date de début",
"width": 120,
"valueFormatter": {
"function": "d3.timeFormat('%d %b %Y')(d3.timeParse('%Y-%m-%d')(params.value))"
},
},
{
"field": "date_fin",
"headerName": "Date de fin",
"width": 120,
"valueFormatter": {
"function": "d3.timeFormat('%d %b %Y')(d3.timeParse('%Y-%m-%d')(params.value))"
},
},
{
"field": "type_evenement",
"headerName": "Type d'événement",
"width": 150,
},
{
"field": "arrondissement",
"headerName": "Arrondissement",
"width": 200,
},
],
rowData=[],
dashGridOptions={
"pagination": True,
"paginationPageSize": 30,
"rowSelection": "single",
},
defaultColDef={
"wrapText": True,
"autoHeight": True,
},
className="ag-theme-balham",
style={"height": "calc(73vh - 60px)"},
)
],
className="card medium-card",
),
],
className="row",
),
],
style={"flex": "1"},
),
],
style={"display": "flex"},
),
dmc.Affix(
dcc.Link(
dmc.Button("Try Plotly Cloud", className="cloud-button"),
href="https://cloud.plotly.com/",
target="_blank",
),
position={"bottom": 20, "right": 20},
),
],
className="main-content",
)
)
@callback(
Output("grid", "rowData"),
Output("map", "figure"),
Output("search-results", "children"),
Input("type_evenement", "value"),
Input("emplacement", "value"),
Input("arrondissement", "value"),
Input("date_debut", "value"),
Input("date_fin", "value"),
)
def update_grid(type_evenement, emplacement, arrondissement, date_debut, date_fin):
df = pd.read_csv("evenements.csv")
if len(type_evenement) > 0:
df = df[df["type_evenement"].isin(type_evenement)]
if len(emplacement) > 0:
df = df[df["emplacement"].isin(emplacement)]
if len(arrondissement) > 0:
df = df[df["arrondissement"].isin(arrondissement)]
if date_debut:
df = df[df["date_debut"] >= date_debut]
if date_fin:
df = df[df["date_fin"] <= date_fin]
row_data = df.to_dict("records")
scatter_map = generate_map(df)
search_results = dcc.Markdown(
f"""
**{len(row_data)} événements trouvés**
📅 {df["type_evenement"].nunique()} types d'événements
📍 {df["arrondissement"].nunique()} arrondissements
📊 Source: [Données de la Ville de Montréal](https://donnees.montreal.ca/dataset/evenements-publics)
"""
)
return row_data, scatter_map, search_results
@callback(
Output("event-info", "children"),
Output("event-date", "children"),
Output("event-description", "children"),
Input("grid", "selectedRows"),
)
def update_event_description(selected_rows):
df = pd.read_csv("evenements.csv")
if selected_rows:
cout = selected_rows[0]["cout"]
inscription = selected_rows[0]["inscription"]
emplacement = selected_rows[0]["emplacement"]
link = selected_rows[0]["url_fiche"]
event_info = html.Div(
[
dcc.Markdown(
f"**Coût :** {cout}\n\n**Inscription :** {inscription}\n\n**Emplacement :** {emplacement}"
),
html.A(
"Voir l'événement",
href=link,
className="event-btn",
target="_blank",
),
]
)
date_debut_raw = selected_rows[0]["date_debut"]
date_fin_raw = selected_rows[0]["date_fin"]
try:
date_debut = datetime.strptime(date_debut_raw, "%Y-%m-%d").strftime(
"%b %d, %Y"
)
date_fin = datetime.strptime(date_fin_raw, "%Y-%m-%d").strftime("%b %d, %Y")
except:
date_debut = date_debut_raw
date_fin = date_fin_raw
event_date = dcc.Markdown(
f"**Date de début :** {date_debut}\n\n**Date de fin :** {date_fin}"
)
titre = selected_rows[0]["titre"]
description = selected_rows[0]["description"]
event_description = dcc.Markdown(f"**{titre}**\n\n{description}")
return event_info, event_date, event_description
no_data = dcc.Markdown(
"Selectionnez un événement dans la grille pour voir les détails"
)
return no_data, no_data, no_data
if __name__ == "__main__":
app.run(debug=True, port=8050)