-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
305 lines (289 loc) · 12.5 KB
/
app.py
File metadata and controls
305 lines (289 loc) · 12.5 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
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 Metro Incidents"
app.index_string = """
<!DOCTYPE html>
<html>
<head>
{%metas%}
<title>Montreal Metro Incidents</title>
<meta name="title" content="Montreal Metro Incidents" />
<meta name="description" content="Monitoring dashboard for Montreal Metro incidents and operational status." />
<meta property="og:type" content="website" />
<meta property="og:url" content="https://mtl-metro-incidents.plotly.app/" />
<meta property="og:title" content="Montreal Metro Incidents" />
<meta property="og:description" content="Monitoring dashboard for Montreal Metro incidents and operational status." />
<meta property="og:image" content="https://mtl-metro-incidents.plotly.app/assets/thumbnail.png" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:url" content="https://mtl-metro-incidents.plotly.app/" />
<meta name="twitter:title" content="Montreal Metro Incidents" />
<meta name="twitter:description" content="Monitoring dashboard for Montreal Metro incidents and operational status." />
<meta name="twitter:image" content="https://mtl-metro-incidents.plotly.app/assets/thumbnail.png" />
{%favicon%}
{%css%}
</head>
<body>
{%app_entry%}
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
</body>
</html>
"""
symptomes_list = [
"Clientèle",
"Matériel roulant",
"Équipements fixes",
"Exploitation trains",
"Feu, fumée, odeur, produit, etc…",
]
app.layout = dmc.MantineProvider(
html.Div(
[
dcc.Interval(id="clock-interval", interval=1000, n_intervals=0),
html.Div(
[
html.Div(
[
html.Span("MÉTROPOLITAIN", className="header-title"),
html.Div(
[
html.Span(
"OPÉRATIONNEL", className="status-operational"
),
html.Span("ACTIVE", className="status-active"),
html.Span(id="live-clock", className="live-clock"),
],
className="status-bar",
),
],
className="top-bar",
),
html.Div(
"CENTRE DE CONTRÔLE DES INCIDENTS",
className="control-center-title",
),
html.Hr(className="header-divider"),
],
className="metro-header",
),
html.Div(
[
html.Div(
[
html.Div(
[
html.H4(
"🛠️ FILTRES & CONTRÔLE",
className="filter-title",
),
html.Label(
"Année:",
className="filter-label",
),
dcc.Dropdown(
id="year-dropdown",
options=[
{"label": str(year), "value": year}
for year in range(2019, 2026)
],
value=2023,
className="dropdown-style",
),
html.Label(
"Ligne:",
className="filter-label",
),
dcc.Dropdown(
id="line-dropdown",
options=[
{"label": "Ligne Verte", "value": 1},
{"label": "Ligne Orange", "value": 2},
{"label": "Ligne Jaune", "value": 4},
{"label": "Ligne Bleue", "value": 5},
],
value=2,
className="dropdown-style",
),
html.Label(
"Types d'incidents:",
className="filter-label-with-margin",
),
dcc.Checklist(
symptomes_list,
symptomes_list,
id="incident-checklist",
className="checklist-style",
inputClassName="checklist-input-style",
labelClassName="checklist-label-style",
),
],
className="card filters",
),
],
className="left-column",
),
html.Div(
[
html.Div(
[
html.Div(
[
html.H4(
"🚨 INCIDENTS TOTAUX",
className="data-card-header primary",
),
html.H2(
id="total-incidents",
className="data-card-value",
),
],
className="card small-card data-card",
),
html.Div(
[
html.H4(
"📅 CE MOIS-CI",
className="data-card-header success",
),
html.H2("89", className="data-card-value"),
],
className="card small-card data-card",
),
html.Div(
[
html.H4(
"⏱️ RÉSOLUTION MOY.",
className="data-card-header danger",
),
html.H2(
id="resolution-moyenne",
className="data-card-value",
),
],
className="card small-card data-card",
),
],
className="row",
),
html.Div(
[
html.Div(
[
html.H4("🗺️ CARTOGRAPHIE RÉSEAU MÉTRO"),
dcc.Graph(id="map"),
],
className="card medium-card",
),
],
className="row",
),
],
className="flex-one",
),
],
className="flex-container",
),
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("live-clock", "children"), Input("clock-interval", "n_intervals"))
def update_clock(n_intervals):
return datetime.now().strftime("%H:%M:%S")
@callback(
Output("map", "figure"),
Output("total-incidents", "children"),
Output("resolution-moyenne", "children"),
Input("year-dropdown", "value"),
Input("line-dropdown", "value"),
Input("incident-checklist", "value"),
)
def update_map(year, line, selected_incidents):
df = pd.read_csv("incidents_metro.csv")
df = df[df["Année civile"] == year]
df = df[df["Ligne"].astype(str).str.contains(str(line), na=False)]
if selected_incidents:
df = df[df["Symptome"].isin(selected_incidents)]
nombre_incidents = str(df["Numero d'incident"].nunique())
try:
df["Heure de l'incident"] = pd.to_datetime(
df["Heure de l'incident"], format="%H:%M", errors="coerce"
)
df["Heure de reprise"] = pd.to_datetime(
df["Heure de reprise"], format="%H:%M", errors="coerce"
)
df["resolution_time"] = (
df["Heure de reprise"] - df["Heure de l'incident"]
).dt.total_seconds() / 60
mean_resolution = df["resolution_time"].mean()
if pd.notna(mean_resolution):
resolution_moyenne = f"{int(mean_resolution)} min"
else:
resolution_moyenne = "N/A"
except:
resolution_moyenne = "N/A"
grouped = df.groupby("Code de lieu").size().reset_index(name="Nombre d'incidents")
grouped = grouped.sort_values(by="Nombre d'incidents", ascending=False)
fig = px.bar(
grouped,
x="Code de lieu",
y="Nombre d'incidents",
labels={"Code de lieu": "Station", "Nombre d'incidents": "Nombre d'incidents"},
)
fig.update_traces(
marker_color="#00b04f", marker_line_color="#00ffff", marker_line_width=1
)
fig.update_layout(
template="plotly_dark",
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(13, 20, 33, 0.1)",
font=dict(family="Rajdhani, sans-serif", color="#ffffff", size=12),
title=dict(
font=dict(family="Orbitron, monospace", color="#ff8c00", size=16),
x=0.5,
xanchor="center",
),
xaxis=dict(
title_font=dict(color="#00ffff", family="Orbitron, monospace"),
tickfont=dict(color="#ffffff", family="Rajdhani, sans-serif"),
gridcolor="rgba(0, 158, 224, 0.2)",
showgrid=True,
),
yaxis=dict(
title_font=dict(color="#00ffff", family="Orbitron, monospace"),
tickfont=dict(color="#ffffff", family="Rajdhani, sans-serif"),
gridcolor="rgba(0, 158, 224, 0.2)",
showgrid=True,
),
margin=dict(l=40, r=40, t=60, b=40),
height=None,
)
fig.update_traces(
marker=dict(line=dict(color="#00ffff", width=1), color="#00b04f", opacity=0.8),
hovertemplate="<b>%{x}</b><br>Incidents: %{y}<extra></extra>",
hoverlabel=dict(
bgcolor="rgba(13, 20, 33, 0.9)",
bordercolor="#00ffff",
font=dict(color="#ffffff", family="Rajdhani"),
),
)
return fig, nombre_incidents, resolution_moyenne
if __name__ == "__main__":
app.run(debug=True, port=8050)