-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
598 lines (525 loc) · 21 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State, ClientsideFunction
import pandas as pd
import locale
import data_processing
import json
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
external_stylesheets = ["https://codepen.io/chriddyp/pen/bWLwgP.css"]
app = dash.Dash(
__name__, meta_tags=[{"name": "viewport", "content": "width=device-width"},
{"property":"og:image","content":"https://i.imgur.com/EJLqbKL.png"}, # og image needs absolute URL. I don't know a better way to do this
{"property":"og:image:height","content":"918"},
{"property":"og:image:width","content":"1880"}],
suppress_callback_exceptions=True, external_stylesheets=external_stylesheets
)
app.title = "COVID-19 Cases NZ"
app.index_string = '''
<!DOCTYPE html>
<html>
<head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-PV80931Z25"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-PV80931Z25');
</script>
{%metas%}
<title>{%title%}</title>
{%favicon%}
{%css%}
</head>
<body>
{%app_entry%}
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
</body>
</html>
'''
server = app.server
dataSourceText = "Data sources: [Johns Hopkins](https://github.com/CSSEGISandData/COVID-19), " \
"[NZ Ministry of Health](https://www.health.govt.nz/our-work/diseases-and-conditions/" \
"covid-19-novel-coronavirus/covid-19-current-situation/covid-19-current-cases)"\
" | Site source code: [GitHub](https://github.com/acreegan/covid-nz)"
default_country = "New Zealand"
def createLayout():
global cases, casesText, casesNew, deaths, deathsText, deathsNew, population
cases, casesText, casesNew, deaths, deathsText, deathsNew, population = data_processing.getData()
return html.Div(
id="mainContainer",
className="mainContainer",
children=[
html.Div(id="header_accumulator_cases", style={"display":"none"}),
html.Div(id="header_accumulator_deaths", style={"display":"none"}),
# empty Div to trigger javascript file for autocomplete off
html.Div(id="output-clientside"),
html.Div(
className="flex-display",
children=[
html.H3(id="header", style={"flex": "1", "marginTop": "0", "textAlign": "center"}),
],
),
dcc.Tabs(
id='tab_selector',
value='cases',
className="tab_container",
# Style needs to be here not in style.css or it doesn't work
style={"display": "flex", "flexFlow": "row nowrap"},
children=[
dcc.Tab(label='Total cases', value='cases', className="tab", selected_className="tab--selected"),
dcc.Tab(label='Deaths', value='deaths', className="tab", selected_className="tab--selected"),
dcc.Tab(label='New cases vs total cases', value='newVsTotal', className="tab", selected_className="tab--selected"),
]
),
html.Div(
id="main_row",
className="main_row pretty_container",
style={
"marginTop": "0"
},
# Tab content goes here
),
html.Div(
className="flex-display",
children=[
dcc.Markdown(children=dataSourceText, style={"textAlign": "center", "margin": "2rem", "flex": "1"}),
]
),
])
app.layout = createLayout
@app.callback(Output('main_row', 'children'), [Input('tab_selector', 'value')])
def create_tab_content(tab_value):
dropdown_columns = cases.columns
if tab_value == 'cases' or tab_value == 'deaths':
return [
html.Div(
id=tab_value + '_graph_container',
className="graph_container",
children=[
dcc.Graph(
id=tab_value + '_graph',
className="graph",
config={
"displayModeBar": False,
"responsive": True},
)
]
),
html.Div(
className="separator",
),
html.Div(
className="control_container",
children=[
html.P("New or Total Cases:"),
dcc.RadioItems(
id=tab_value + '_new_v_total',
options=[
{'label': 'Total', 'value': 'total'},
{'label': 'New', 'value': 'new'},
],
value='total',
persistence=True,
),
html.P(""),
html.P("Graph Scale: "),
dcc.RadioItems(
id=tab_value + '_scale',
options=[
{'label': 'Linear', 'value': 'linear'},
{'label': 'Log', 'value': 'log'},
{'label': 'Per 10,000 population', 'value': 'per_capita'},
],
value='linear',
persistence=True,
),
html.P(""),
html.P("Select Countries/States"),
html.Div(
className="button_container",
children=[
html.Button("Select All",
id=tab_value + '_select_all',
style={"flex": "1", "margin": ".5rem"}
),
html.Button("Select None",
id=tab_value + '_select_none',
style={"flex": "1", "margin": ".5rem"}
),
]
),
dcc.Dropdown(
id=tab_value + '_dropdown',
className="dropdown",
value=[default_country], # Value is list since multi=True
persistence=True,
multi=True,
# Style needs to be here not in style.css or it doesn't work
style={
"flex": "1 1 0",
"overflow": "auto"
},
options=[
{
"label": i,
"value": i
} for i in dropdown_columns],
),
]
)]
elif tab_value == 'newVsTotal':
return [
html.Div(
id=tab_value + '_graph_container',
className="graph_container",
children=[
dcc.Graph(
id=tab_value + '_graph',
className="graph",
config={
"displayModeBar": False,
"responsive": True},
)
]
),
html.Div(
className="separator",
),
html.Div(
className="control_container",
children=[
html.P("Select Countries/States"),
html.Div(
className="button_container",
children=[
html.Button("Select All",
id=tab_value + '_select_all',
style={"flex": "1", "margin": ".5rem"}
),
html.Button("Select None",
id=tab_value + '_select_none',
style={"flex": "1", "margin": ".5rem"}
),
]
),
dcc.Dropdown(
id=tab_value + '_dropdown',
className="dropdown",
value=["New Zealand"],
persistence=True,
multi=True,
# Style needs to be here not in style.css or it doesn't work
style={
"flex": "1 1 0",
"overflow": "auto"
},
options=[
{
"label": i,
"value": i
} for i in cases.columns],
),
]
)]
# The accumulators are a workaround to enable us to use dynamically generated elements as inputs.
# Dash requires all inputs of a callback to exist at the time the callback is run (And only one callback to output to
# each element). This is not possible with dynamically generated tabs, so we feed their values into the accumulators
# to act as intermediaries
@app.callback(
Output("header", "children"),
[Input("header_accumulator_cases", "children"),
Input("header_accumulator_deaths", "children")],
)
def update_header(value_cases, value_deaths):
ctx = dash.callback_context
values = json.loads(ctx.triggered[0]["value"])
if len(values["dropdown"])>0:
country = values["dropdown"][0]
else:
return dash.no_update
newvtotal = values["newvtotal"]
if ctx.triggered[0]["prop_id"] == "header_accumulator_cases.children":
if newvtotal == "total":
return locale.format_string(
"As of %s, there have been %d cases in total of COVID-19 in %s",
(cases[country].dropna().index[-1].strftime("%d %B %Y"),
cases[country].dropna().iloc[-1],
country),
grouping=True)
else:
number = casesNew[country].dropna().iloc[-1]
return locale.format_string(
"On %s, there were %d new cases of COVID-19 in %s",
(cases[country].dropna().index[-1].strftime("%d %B %Y"),
number,
country),
grouping=True)
elif ctx.triggered[0]["prop_id"] == "header_accumulator_deaths.children":
if newvtotal == "total":
return locale.format_string(
"As of %s, there have been %d deaths from COVID-19 in %s",
(deaths[country].dropna().index[-1].strftime("%d %B %Y"),
deaths[country].dropna().iloc[-1],
country),
grouping=True)
else:
number = deathsNew[country].dropna().iloc[-1]
return locale.format_string(
"On %s, there were %d new deaths from COVID-19 in %s ",
(deaths[country].dropna().index[-1].strftime("%d %B %Y"),
number,
country),
grouping=True)
else:
return dash.no_update
@app.callback(
Output("cases_dropdown", "value"),
[Input("cases_select_all", "n_clicks"),
Input("cases_select_none", "n_clicks")],
[State('cases_dropdown', "options")]
)
def update_dropdown_cases(all_n_clicks, none_n_clicks, options):
ctx = dash.callback_context
if ctx.triggered[0]["value"] is None:
return [default_country]
else:
if ctx.triggered[0]["prop_id"] == "cases_select_all" + ".n_clicks":
return list(i.get("value") for i in options)
else:
return []
@app.callback(
Output("deaths_dropdown", "value"),
[Input("deaths_select_all", "n_clicks"),
Input("deaths_select_none", "n_clicks")],
[State('deaths_dropdown', "options")]
)
def update_dropdown_deaths(all_n_clicks, none_n_clicks, options):
ctx = dash.callback_context
if ctx.triggered[0]["value"] is None:
return [default_country]
elif ctx.triggered[0]["prop_id"] == "deaths_select_all" + ".n_clicks":
return list(i.get("value") for i in options)
else: # prop_id = select_none
return []
@app.callback(
Output("newVsTotal_dropdown", "value"),
[Input("newVsTotal_select_all", "n_clicks"),
Input("newVsTotal_select_none", "n_clicks")],
[State('newVsTotal_dropdown', "options")]
)
def update_dropdown_newVsTotal(all_n_clicks, none_n_clicks, options):
ctx = dash.callback_context
if ctx.triggered[0]["value"] is None:
return [default_country]
elif ctx.triggered[0]["prop_id"] == "newVsTotal_select_all" + ".n_clicks":
return list(i.get("value") for i in options)
else: # prop_id = select_none
return []
@app.callback(
Output('cases_scale', "options"),
[Input('cases_new_v_total', "value")],
[State('cases_scale', 'options')]
)
def update_scale_options_cases(value, options):
if value == "new":
for item in options:
if item["value"] != "linear":
item["disabled"] = True
return options
else:
for item in options:
item["disabled"] = False
return options
@app.callback(
Output('deaths_scale', "options"),
[Input('deaths_new_v_total', "value")],
[State('deaths_scale', 'options')]
)
def update_scale_options_deaths(value, options):
if value == "new":
for item in options:
if item["value"] != "linear":
item["disabled"] = True
return options
else:
for item in options:
item["disabled"] = False
return options
@app.callback(
Output('cases_graph', "figure"),
[Input('cases_dropdown', "value"),
Input('cases_new_v_total', "value"),
Input('cases_scale', "value")]
)
def update_graph_cases(dropdown_value, newvtotal_value, scale_value):
# So we can copy paste these callbacks and only update values at top.
totalTitle = "Total cases of COVID-19<br> over time"
newTitle = "New cases of COVID-19<br> over time"
yLabel = "Confirmed cases"
perCapitaYLabel = 'Confirmed cases per 10,000 population'
data = cases if newvtotal_value == "total" else casesNew
dataText = casesText
if dropdown_value is None or len(dropdown_value) == 0:
return dash.no_update
else:
countryList = dropdown_value
if newvtotal_value == "total":
graphData = [dict(
x=data.index if scale_value != "log" else data.index[data[i] > 50],
y=data[i] if scale_value == "linear" else data[i].loc[data.index[data[i] > 50]] if scale_value == "log" else 10000*data[i]/population.loc["Population", i],
name=i,
text=dataText[i].dropna() if scale_value != "log" else dataText[i].dropna().loc[data.index[data[i] > 50]],
mode="lines+text",
textposition="top left"
) for i in data[countryList].columns]
layout = dict(
xaxis={'title': 'Time'},
yaxis={'title': yLabel if scale_value != "per_capita" else perCapitaYLabel,
"type": "linear" if scale_value != "log" else "log"},
margin={'l': 50, 'b': 40, 't': 40, 'r': 20},
hovermode='closest',
title=totalTitle,
showlegend=False,
)
else: # newvtotal_value = new
graphData = [dict(
x=data.index,
y=data[i],
name=i,
type="bar",
textposition="top left"
) for i in data[countryList[0]].to_frame().columns]
layout = dict(
xaxis={'title': 'Time'},
yaxis={'title': yLabel,
"type": "linear"},
margin={'l': 50, 'b': 40, 't': 40, 'r': 20},
hovermode='closest',
title=newTitle,
showlegend=False,)
return {'data': graphData,
'layout': layout}
@app.callback(
Output('deaths_graph', "figure"),
[Input('deaths_dropdown', "value"),
Input('deaths_new_v_total', "value"),
Input('deaths_scale', "value")]
)
def update_graph_deaths(dropdown_value, newvtotal_value, scale_value):
# So we can copy paste these callbacks and only update values at top.
totalTitle = "Deaths from COVID-19<br> over time"
newTitle = "Change in deaths from COVID-19<br> over time"
yLabel = "Deaths"
perCapitaYLabel = 'Deaths per 10,000 population'
data = deaths if newvtotal_value == "total" else deathsNew
dataText = deathsText
if dropdown_value is None or len(dropdown_value) == 0:
return dash.no_update
else:
countryList = dropdown_value
if newvtotal_value == "total":
graphData = [dict(
x=data.index if scale_value != "log" else data.index[data[i] > 50],
y=data[i] if scale_value == "linear" else data[i].loc[
data.index[data[i] > 50]] if scale_value == "log" else 10000 * data[i] / population.loc[
"Population", i],
name=i,
text=dataText[i].dropna() if scale_value != "log" else dataText[i].dropna().loc[data.index[data[i] > 50]],
mode="lines+text",
textposition="top left"
) for i in data[countryList].columns]
layout = dict(
xaxis={'title': 'Time'},
yaxis={'title': yLabel if scale_value != "per_capita" else perCapitaYLabel,
"type": "linear" if scale_value != "log" else "log"},
margin={'l': 50, 'b': 40, 't': 40, 'r': 20},
hovermode='closest',
title=totalTitle,
showlegend=False,
)
else: # newvtotal_value = new
graphData = [dict(
x=data.index,
y=data[i],
name=i,
type="bar",
textposition="top left"
) for i in data[countryList[0]].to_frame().columns]
layout = dict(
xaxis={'title': 'Time'},
yaxis={'title': yLabel,
"type": "linear"},
margin={'l': 50, 'b': 40, 't': 40, 'r': 20},
hovermode='closest',
title=newTitle,
showlegend=False, )
return {'data': graphData,
'layout': layout}
@app.callback(
Output('newVsTotal_graph', "figure"),
[Input('newVsTotal_dropdown', "value")]
)
def update_graph_newVsTotal(value):
if value is None or len(value) == 0:
return dash.no_update
else:
countryList = value
newData = [dict(
# Starting at the point where total cases > 50 (different times and lengths for each country)
x=cases[i].loc[cases.index[cases[i] > 50]],
y=casesNew[i].rolling(pd.to_timedelta("7days")).sum().loc[cases.index[cases[i] > 50]],
name=i,
text=casesText[i].dropna().loc[cases.index[cases[i] > 50]],
mode="lines+text",
textposition="top left"
) for i in cases[countryList].columns]
return {
'data': newData,
'layout': dict(
xaxis={'title': 'Total cases',
"type": "log"},
yaxis={'title': 'New cases in past 7 days',
"type": "log"},
margin={'l': 50, 'b': 40, 't': 40, 'r': 20},
hovermode='closest',
title="New cases of COVID-19<br> vs total cases",
showlegend=False,
)
}
@app.callback(
Output('header_accumulator_cases', 'children'),
[Input('cases_dropdown', "value"),
Input('cases_new_v_total', "value")]
)
def update_header_accumulator_cases(dropdown, newvtotal):
return json.dumps(dict(
dropdown=dropdown,
newvtotal=newvtotal
))
@app.callback(
Output('header_accumulator_deaths', 'children'),
[Input('deaths_dropdown', "value"),
Input('deaths_new_v_total', "value")]
)
def update_header_accumulator_deaths(dropdown, newvtotal):
return json.dumps(dict(
dropdown=dropdown,
newvtotal=newvtotal
))
app.clientside_callback(
ClientsideFunction(namespace="clientside", function_name="autocomplete_off"),
Output("output-clientside", "children"),
[Input('tab_selector', 'value')],
)
if __name__ == '__main__':
app.run_server(debug=True, port=8085, host="0.0.0.0")