-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
304 lines (279 loc) · 13.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
# see https://plotly.com/python/line-charts/
# see https://dash.plotly.com/live-updates
import pandas as pd
import dash
import dash_html_components as html
import dash_core_components as dcc
import plotly.graph_objects as go
import os
import io
import requests
import time
import config
# Load data
def load_data():
global df, last_load_data
if (int(time.time()) - last_load_data) > (1 * 60):
last_load_data = int(time.time())
req = requests.get(config.DATASET_RAW_CSV_URL)
if req.status_code != 200:
print(f"WTF ??? {req.status_code} {req.reason} {req.text}")
data = req.text
buffer = io.StringIO(data)
df = pd.read_csv(buffer, parse_dates=True)
df['data'] = pd.to_datetime(df['data'])
df = None
last_load_data = 0
load_data()
external_scripts = config.EXTERNAL_SCRIPTS
# Initialise the app
app = dash.Dash(__name__, external_scripts=external_scripts)
def get_line_figure_from_keys(keys, title, color):
fig = go.Figure()
color.reverse()
for k in keys:
fig.add_trace(go.Scatter(x=df['data'], y=df[k], name=k, line=dict(color=color.pop())))
fig.layout = dict(
title=title,
showlegend=True,
legend=dict(
x=0.05,
y=1.0
),
margin=dict(l=40, r=0, t=40, b=30),
xaxis_tickformat='%d %b',
)
fig.update_yaxes(nticks=20, linecolor='black')
fig.update_xaxes(linecolor='black')
fig.update_layout({'plot_bgcolor': 'rgba(240, 240, 240, 1)', 'paper_bgcolor': 'rgba(240, 240, 240, 1)'})
fig.update_traces(mode="markers+lines", hovertemplate=None)
fig.update_layout(hovermode='x')
fig.layout.xaxis.fixedrange = True
fig.layout.yaxis.fixedrange = True
return fig
def get_bar_figure(data, title, legend_name, color):
fig = go.Figure()
for k in range(len(legend_name)):
fig.add_trace(go.Bar(x=df['data'], y=data[k], name=legend_name[k], text=data[k],
textposition='auto', marker_color=color[k]))
fig.layout = dict(
title=title,
showlegend=True,
legend=dict(
x=0.05,
y=1.0
),
margin=dict(l=40, r=0, t=40, b=30),
xaxis_tickformat='%d %b',
)
fig.update_yaxes(nticks=20, linecolor='black')
fig.update_xaxes(linecolor='black')
fig.update_layout({'plot_bgcolor': 'rgba(240, 240, 240, 1)', 'paper_bgcolor': 'rgba(240, 240, 240, 1)'})
fig.update_traces(hovertemplate='%{y}')
fig.update_layout(hovermode='x', barmode='group')
fig.layout.xaxis.fixedrange = True
fig.layout.yaxis.fixedrange = True
return fig
def get_last_value(key):
delta_tmp = df[key].tail(2).to_list()
return {'value': int(df[key].tail(1)), 'delta_giornaliero': delta_tmp[1] - delta_tmp[0]}
def get_daily_variations(key):
tmp = df[key].to_list()
variations = [tmp[0]]
for i in range(1, len(tmp)):
variations.append(tmp[i] - tmp[i - 1])
return variations
def delta_html(delta):
if delta >= 0:
return html.Footer(f"+{delta} rispetto a ieri", className='green'),
else:
return html.Footer(f"{delta} rispetto a ieri", className='red'),
def serve_layout():
global df
load_data()
return html.Div(
[
html.Div(
[
html.Div(
[
html.Img(
src=app.get_asset_url(config.LOGO_PATH),
id="logo-comune",
style={
"height": "100px",
"width": "auto",
"margin-bottom": "25px",
},
)
],
className="one-third column",
),
html.Div(
[
html.Div(
[
html.H3(
f"Covid-19 {config.CITY_NAME}",
style={"margin-bottom": "0px"},
),
html.H5(
"Dashboard", style={"margin-top": "0px"}
),
]
)
],
className="one-half column",
id="title",
),
html.Div(
[
html.A(
html.Button(config.MAYOR_NAME, id="learn-more-button"),
href=config.MAYOR_LINK,
),
],
className="one-third column",
id="button",
),
],
id="header",
className="row flex-display",
style={"margin-bottom": "25px"},
),
html.Div(
[
html.Div(
[
*config.DASHBOARD_DESCRIPTION,
html.H6(f"Dati aggiornati al {df['data'].tail(1).to_list()[0].strftime('%d/%m/%Y')}"),
html.Div(
[
html.Div(
[html.H6(get_last_value('positivi')['value']),
html.P("Positivi"),
html.Div(delta_html(get_last_value('positivi')['delta_giornaliero']))],
id="positivi_attuali",
className="mini_container",
),
html.Div(
[html.H6(get_last_value('quarantena')['value']), html.P("In Quarantena"),
html.Div(delta_html(get_last_value('quarantena')['delta_giornaliero']))],
id="quarantena_attuali",
className="mini_container",
),
html.Div(
[html.H6(get_last_value('guariti')['value']), html.P("Guariti"),
html.Div(delta_html(get_last_value('guariti')['delta_giornaliero']))],
id="guariti_attuali",
className="mini_container",
),
html.Div(
[html.H6(get_last_value('decessi')['value']), html.P("Decessi"),
html.Div(delta_html(get_last_value('decessi')['delta_giornaliero']))],
id="decessi_attuali",
className="mini_container",
),
],
id="info-container",
className="row container-display",
),
html.Div(
[
dcc.Graph(config={'displayModeBar': False},
animate=False,
figure=get_line_figure_from_keys(
['positivi', 'quarantena', 'guariti', 'decessi'],
'Grafico generale variabili',
['#C94747', '#1f77b4', '#2ca02c', '#bcbd22']
))
],
className="pretty_container",
),
html.Div(
[
dcc.Graph(config={'displayModeBar': False},
animate=False,
figure=get_line_figure_from_keys(
['positivi'],
'Soggetti attualmente positivi', ['#C94747']
)),
html.P('''Nota: Grafico a linee che indica l'andamento dei soggetti
positivi nel tempo.'''),
],
className="pretty_container",
),
html.Div(
[
dcc.Graph(config={'displayModeBar': False},
animate=False,
figure=get_line_figure_from_keys(
['quarantena'],
'Soggetti attualmente in quarantena', ['#1f77b4']
)),
html.P('''Nota: '''),
html.P('''- Grafico a linee che indica l'andamento dei soggetti
in quarantena nel tempo.'''),
html.P('''- I numeri dei soggetti in quarantena non sono strettamente
correlati ai positivi.''')
],
className="pretty_container",
),
html.Div(
[
dcc.Graph(config={'displayModeBar': False},
animate=False,
figure=get_bar_figure([get_daily_variations('positivi'),
get_daily_variations('quarantena')],
'Variazione rispetto al giorno precedente',
['positivi', 'quarantena'],
['#C94747', '#1f77b4'])
),
html.P('''Nota: '''),
html.P('''- Grafico a barre che indica la variazione dei soggetti positivi e
in quarantena.'''),
html.P('''- Valori ottenuti tramite la sottrazione dei dati del
giorno(t) - dati giorno(t-1).'''),
],
className="pretty_container",
),
html.Div(
[
dcc.Graph(config={'displayModeBar': False},
animate=False,
figure=get_line_figure_from_keys(
['guariti'],
'Soggetti attualmente guariti', ['#2ca02c']
)),
html.P('''Nota: Grafico a linee che indica l'andamento dei soggetti
guariti nel tempo.'''),
],
className="pretty_container",
),
html.Div(
[
dcc.Graph(config={'displayModeBar': False},
animate=False,
figure=get_line_figure_from_keys(
['decessi'],
'Decessi', ['#bcbd22']
)),
html.P('''Nota: Grafico a linee che indica l'andamento dei decessi
nel tempo.'''),
],
className="pretty_container",
),
html.P(
config.DASHBOARD_FOOTER
)
]
)
]
),
]
)
app.layout = serve_layout
# Run the app
if __name__ == '__main__':
app.title = f"Covid19{config.CITY_NAME} | Dashboard"
app.run_server(host='0.0.0.0', debug=os.getenv('DEBUG', False), port=os.getenv('PORT', 8050))