-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
376 lines (348 loc) · 15.7 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
import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import plotly.express as px
import plotly.graph_objects as go
import json
import requests
import csv
import pandas as pd
from datetime import datetime as dt
import flask
# Import and sort data from Folkhälsomyndigheten
df1 = pd.read_excel(
"assets/fhm_data.xlsx",
sheet_name="Veckodata Region",
header=0,
engine="openpyxl",
keep_default_na=False,
)
df1["day"] = 1 # display Monday date for each week
df1["date"] = df1.apply(
lambda row: dt.fromisocalendar(row["år"], row["veckonummer"], row["day"]), axis=1
)
df1 = df1.drop(df1[(df1.år == 2020) & (df1.veckonummer < 9)].index) # remove weeks before week 9 of 2020 because there is no data for those
# For the cases plot
df1_fall = df1[["date", "Region", "Antal_fall_vecka"]]
dfc_swe = df1_fall.groupby(["date"]).sum().reset_index()
dfc_swe.insert(loc=1, column="Region", value="Sweden")
df1_fall = pd.concat([df1_fall, dfc_swe])
# for the ICU plot
df1_intense = df1[["date", "Region", "Antal_intensivvårdade_vecka"]]
dfi_swe = df1_intense.groupby(["date"]).sum().reset_index()
dfi_swe.insert(loc=1, column="Region", value="Sweden")
df1_intense = pd.concat([df1_intense, dfi_swe])
# for the deaths plot
df1_deaths = df1[["date", "Region", "Antal_avlidna_vecka"]]
dfd_swe = df1_deaths.groupby(["date"]).sum().reset_index()
dfd_swe.insert(loc=1, column="Region", value="Sweden")
df1_deaths = pd.concat([df1_deaths, dfd_swe])
server = flask.Flask(__name__)
# Dash app
app = dash.Dash(
__name__,
server=server,
title="COVID-19 in Sweden: Cases, ICU Admissions, Deaths",
suppress_callback_exceptions=True,
external_stylesheets=[dbc.themes.BOOTSTRAP],
meta_tags=[
{"name": "viewport", "content": "width=device-width, initial-scale=1.0"}
],
)
tabDataSource_content = dbc.CardBody(
[
html.Div(
[
dbc.Row(
dbc.Col(
html.Div([
html.P(["The visualizations in this dashboard are based on openly shared data by the Public Health Agency of Sweden (",html.A("Folkhälsomyndigheten", href="https://www.folkhalsomyndigheten.se/"),") between February 2020 and March 2023. During this period, the data was shared on a weekly basis. An Excel file containing all data for this period ", html.A("can be downloaded here", href="https://github.com/ScilifelabDataCentre/dash-covid-in-sweden/raw/main/assets/fhm_data.xlsx"), "."]),
html.P(["The ", html.I("Confirmed Cases"), " plot is based on data from the SmiNet reporting system, published as ", html.I("Antal_fall_vecka"), ". The ", html.I("Intensive Care Admissions"), " plot is based on data from the Swedish Intensive Care Register's (Svenska Intensivvårdsregistret) reporting system, published as ", html.I("Antal_intensivvårdade_vecka"), ". The ", html.I("Deaths"), " plot is based on data from the SmiNet reporting system, published as ", html.I("Antal_avlidna_vecka"), ". Here, the people with laboratory-confirmed COVID-19 which died are included, regardless of the cause of death."])
]),
)
),
dbc.Row(
dbc.Col(
html.Div(
html.P(["If you have questions regarding the dashboard, get in touch with us on ", html.A("GitHub", href="https://github.com/ScilifelabDataCentre/dash-covid-in-sweden"), "."])
),
)
),
],
),
],
)
tabPublicHealth_content = dbc.CardBody(
[
html.P(
"This dashboard visualizes the timeline of the spread of COVID-19 in Sweden: number of confirmed cases, number of people admitted to ICU units, as well as deaths with ongoing or recent confirmed COVID-19. The dashboard covers the period between February 2020 and March 2023. Information displayed here is based on data that was made at the time by the Swedish Public Health Agency (Folkhälsomyndigheten). Please see the 'Data sources' tab for more information.",
className="card-text",
),
html.P(
"You can select an area of interest (either Sweden as a whole, or individual counties) and focus on a specific timeframe. The 'Date' menu below can be used to select a particular timeframe to focus on, but otherwise information is shown for the whole period for which data was shared. Similarly, the 'Select County' dropdown menu can be used to focus in on a particular Swedish county (region), or Sweden as a whole (the latter is shown by default).",
className="card-text",
),
html.P(
"The dates below indicate the first day of the week for which the numbers are shown. For example, Feb 24 2020 stands for the week between February 24 and March 1 2020.",
className="card-text",
),
dbc.Row(
dbc.Col(
html.Div(
[
dbc.Row([
html.Div([
dbc.Row(
dbc.Col("Select date range", className="font-weight-bold"),
),
dbc.Row(
dbc.Col(
dcc.DatePickerRange(
id="my-date-picker-range", # ID to be used for callback
calendar_orientation="horizontal", # vertical or horizontal
day_size=39, # size of calendar image. Default is 39
end_date_placeholder_text="Return", # text that appears when no end date chosen
with_portal=False, # if True calendar will open in a full screen overlay portal
first_day_of_week=0, # Display of calendar when open (0 = Sunday)
reopen_calendar_on_clear=True,
is_RTL=False, # True or False for direction of calendar
clearable=True, # whether or not the user can clear the dropdown
number_of_months_shown=1, # number of months shown when calendar is open
min_date_allowed=df1_intense["date"].iloc[
0
], # minimum date allowed on the DatePickerRange component
max_date_allowed=df1_intense["date"].iloc[
-1
],
initial_visible_month=df1_intense[
"date"
].iloc[-1],
start_date=df1_intense["date"].iloc[0],
end_date=df1_intense["date"].iloc[-1],
display_format="DD MMM YY", # how selected dates are displayed in the DatePickerRange component.
month_format="MMMM, YYYY", # how calendar headers are displayed when the calendar is opened.
minimum_nights=1, # minimum number of days between start and end date
persistence=True,
persisted_props=["start_date"],
persistence_type="session", # session, local, or memory. Default is 'local'
updatemode="singledate", # singledate or bothdates. Determines when callback is triggered
)
)
),
], className="col-md-6 mt-2"
),
html.Div([
dbc.Row(
dbc.Col("Select county (whole of Sweden shown by default)", className="font-weight-bold"),
),
dbc.Row(
dbc.Col(
dcc.Dropdown(
id="county-dropdown",
clearable=False,
persistence=True,
persistence_type="session",
options=[
{"label": x, "value": x}
for x in sorted(
df1_intense["Region"].unique()
)
],
value="Sweden",
)
),
),
], className="col-md-6 mt-2"
),
]),
dcc.Graph(id="cases_graph"),
dcc.Graph(id="intensive_graph"),
dcc.Graph(id="deaths_graph")
],
)
),
),
html.P(html.I(
"Please keep in mind that the range of Y axes is different in each of these graphs."),
className="card-text mt-3",
)
],
)
app.layout = html.Div(
[
dbc.Row(
[
html.Div(
html.Div([
html.H1("COVID-19 in Sweden: Cases, ICU Admissions, Deaths")
],
), className="col-md-auto",
)
],
),
dbc.Row(
dbc.Col(
html.Div(
[
dbc.Tabs(
[
dbc.Tab(label="Graphs", tab_id="tabPublicHealth"),
dbc.Tab(label="Data sources", tab_id="tabDataSource")
],
id="tabs",
active_tab="tabPublicHealth" ),
html.Div(id="content"),
], className="mt-3",
),
),
),
html.Div([
html.Hr(),
dbc.Row(
dbc.Col([
html.P(["This dashboard was created using ", html.A("Dash framework", href="https://github.com/plotly/dash"), " (v.1.20). The source code for the current version of the dashboard is ", html.A("available on GitHub", href="https://github.com/ScilifelabDataCentre/dash-covid-in-sweden"), "."],
className="text-muted")
], className="mt-4"
),
),
],
),
], className="container-lg",
)
@app.callback(Output("content", "children"), [Input("tabs", "active_tab")])
def switch_tab(at):
if at == "tabPublicHealth":
return tabPublicHealth_content
elif at == "tabDataSource":
return tabDataSource_content
@app.callback(
Output("cases_graph", "figure"),
[
Input("county-dropdown", "value"),
Input("my-date-picker-range", "start_date"),
Input("my-date-picker-range", "end_date"),
],
)
def update_cases_graph(value, start_date, end_date):
mask = (df1_fall["date"] > start_date) & (df1_fall["date"] <= end_date)
df1 = df1_fall.loc[mask]
df = df1[(df1["Region"] == value)]
trace1 = go.Bar(
x=df["date"],
y=df["Antal_fall_vecka"],
name="Case number",
marker_color="#648fff",
hovertemplate="<b>Number of confirmed cases</b>" + "<br>Week: %{x}" + "<br>Confirmed Cases: %{y}<extra></extra>",
)
# figure layout
fig = go.Figure(data=trace1)
fig.update_layout(
plot_bgcolor="white", font=dict(size=13), margin=dict(l=0, r=0, t=50, b=50)
)
# modify x-axis
fig.update_xaxes(
title="<b>Date</b>",
showgrid=True,
linecolor="black",
# set start point of x-axis
tick0="2020-02-17",
)
# modify y-axis
fig.update_yaxes(
title="<b>Confirmed Cases</b>",
showgrid=True,
gridcolor="lightgrey",
linecolor="black",
# change range to envelope the appropriate range
range=[0, max(df["Antal_fall_vecka"] + 50)],
)
return fig
@app.callback(
Output("intensive_graph", "figure"),
[
Input("county-dropdown", "value"),
Input("my-date-picker-range", "start_date"),
Input("my-date-picker-range", "end_date"),
],
)
def update_intensive_graph(value, start_date, end_date):
mask1 = (df1_intense["date"] > start_date) & (df1_intense["date"] <= end_date)
ddf1 = df1_intense.loc[mask1]
ddf = ddf1[(ddf1["Region"] == value)]
trace2 = go.Bar(
x=ddf["date"],
y=ddf["Antal_intensivvårdade_vecka"],
name="ICU admissions",
marker_color="#dc267f",
hovertemplate="<b>Number of ICU adminissions</b>" + "<br>Week: %{x}" + "<br>Admissions: %{y}<extra></extra>",
)
# figure layout
fig = go.Figure(data=trace2)
fig.update_layout(
plot_bgcolor="white", font=dict(size=13), margin=dict(l=0, r=0, t=50, b=50)
)
# modify x-axis
fig.update_xaxes(
title="<b>Date</b>",
showgrid=True,
linecolor="black",
# set start point of x-axis
tick0="2020-02-17",
)
# modify y-axis
fig.update_yaxes(
title="<b>Intensive Care Admissions</b>",
showgrid=True,
gridcolor="lightgrey",
linecolor="black",
# change range to envelope the appropriate range
range=[0, max(ddf["Antal_intensivvårdade_vecka"] + 10)],
)
return fig
@app.callback(
Output("deaths_graph", "figure"),
[
Input("county-dropdown", "value"),
Input("my-date-picker-range", "start_date"),
Input("my-date-picker-range", "end_date"),
],
)
def update_deaths_graph(value, start_date, end_date):
mask1 = (df1_deaths["date"] > start_date) & (df1_deaths["date"] <= end_date)
ddf1 = df1_deaths.loc[mask1]
ddf = ddf1[(ddf1["Region"] == value)]
trace2 = go.Bar(
x=ddf["date"],
y=ddf["Antal_avlidna_vecka"],
name="Deaths",
marker_color="#785ef0",
hovertemplate="<b>Number of deaths</b>"+ "<br>Week: %{x}" + "<br>Deaths: %{y}<extra></extra>",
)
# figure layout
fig = go.Figure(data=trace2)
fig.update_layout(
plot_bgcolor="white", font=dict(size=13), margin=dict(l=0, r=0, t=50, b=50)
)
# modify x-axis
fig.update_xaxes(
title="<b>Date</b>",
showgrid=True,
linecolor="black",
# set start point of x-axis
tick0="2020-02-17",
)
# modify y-axis
fig.update_yaxes(
title="<b>Deaths</b>",
showgrid=True,
gridcolor="lightgrey",
linecolor="black",
# change range to envelope the appropriate range
range=[0, max(ddf["Antal_avlidna_vecka"] + 10)],
)
return fig
# Server clause
if __name__ == "__main__":
app.run_server(debug=False)