-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
362 lines (325 loc) · 13.4 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
import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
import dash_table as dtb
import os
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
external_stylesheets = [dbc.themes.BOOTSTRAP, '/assets/css/main.css']
app = dash.Dash(__name__, title='Cambridge Uni COVID-19 Dashboard', external_stylesheets=external_stylesheets, meta_tags=[
{
'name': 'title',
'content': 'University of Cambridge COVID-19 Dashboard'
},
{
'name': 'description',
'content': 'A simple dashboard for tracking weekly case and testing data from the University of Cambridge testing programmes for students and staff.'
},
{
'name': 'keywords',
'content': 'coronavirus, covid, university, cambridge, data, visualisation, dashboard'
},
{
'name': 'language',
'content': 'English',
},
{
'name': 'author',
'content': 'Faizaan Pervaiz'
},
# A tag that tells Internet Explorer (IE)
# to use the latest renderer version available
# to that browser (e.g. Edge)
{
'http-equiv': 'X-UA-Compatible',
'content': 'IE=edge'
},
# A tag that tells the browser not to scale
# desktop widths to fit mobile screens.
# Sets the width of the viewport (browser)
# to the width of the device, and the zoom level
# (initial scale) to 1.
#
# Necessary for "true" mobile support.
# {
# 'name': 'viewport',
# 'content': 'width=device-width, initial-scale=1.0'
# },
{
'property': 'og:type',
'content': 'website'
},
{
'property': 'og:url',
'content': 'http://camcovid.xyz'
},
{
'property': 'og:title',
'content': 'University of Cambridge COVID-19 Dashboard'
},
{
'property': 'og:description',
'content': 'A simple dashboard for tracking weekly case and testing data from the University of Cambridge testing programmes for students and staff.'
},
{
'property': 'og:image',
'content': 'http://camcovid.xyz/assets/sars-cov-2.jpg'
},
{
'property': 'twitter:card',
'content': 'summary_large_image'
},
{
'property': 'twitter:url',
'content': 'http://camcovid.xyz'
},
{
'property': 'twitter:title',
'content': 'University of Cambridge COVID-19 Dashboard'
},
{
'property': 'twitter:description',
'content': 'A simple dashboard for tracking weekly case and testing data from the University of Cambridge testing programmes for students and staff.'
},
{
'property': 'twitter:image',
'content': 'http://camcovid.xyz/assets/sars-cov-2.jpg'
}
])
server = app.server
df_cases = pd.read_csv('./data/cases.csv')
df_cases['week_ending'] = pd.to_datetime(
df_cases['week_ending'])
latest_cases = df_cases.iloc[-1]
try:
previous_cases = df_cases.iloc[-2]
except IndexError:
previous_cases = latest_cases.copy()
previous_cases['total_confirmed'] = 0
previous_cases['student_confirmed'] = 0
previous_cases['staff_confirmed'] = 0
fig_cases = go.Figure()
fig_cases.add_trace(go.Scatter(x=df_cases['week_ending'], y=df_cases['total_confirmed'],
mode='lines+markers',
name='Total',
))
fig_cases.add_trace(go.Scatter(x=df_cases['week_ending'], y=df_cases['student_confirmed'],
mode='lines+markers',
name='Student',
))
fig_cases.add_trace(go.Scatter(x=df_cases['week_ending'], y=df_cases['staff_confirmed'],
mode='lines+markers', name='Staff',
))
fig_cases.update_layout(
title=f"Total Confirmed Cases (as of {latest_cases['week_ending'].strftime('%Y-%m-%d')})",
xaxis_title='Week Ending Date',
yaxis_title='Number',
legend_title='Category',
)
df_colleges = pd.read_csv('./data/colleges.csv').rename(columns={
'college': 'College',
'positives': 'Number of students tested positive',
'isolating_households': 'Number of households isolating',
'isolating_students': 'Number of students isolating',
'last_updated': 'Last updated',
'source_name': 'Source'
})
# Currently no sensible way of placing a hyperlink in a DataTable cell
# so just use URL string for now
df_colleges['Source'] = df_colleges.source_link.where(
df_colleges['source_link'].notna(), df_colleges.Source)
table_colleges = dtb.DataTable(
id='college-table',
columns=[{"name": i, "id": i}
for i in df_colleges.columns if i != 'source_link'],
data=df_colleges.to_dict('records'),
sort_action='native',
style_table={'overflowX': 'auto'},
)
df_testing = pd.read_csv('./data/testing.csv')
df_testing['week_ending'] = pd.to_datetime(
df_testing['week_ending'])
df_testing['total_screened'] = df_testing['asym_screened'] + \
df_testing['sym_screened']
df_testing['total_positive'] = df_testing['asym_positive'] + \
df_testing['sym_positive'] + df_testing['other_positive']
fig_screening = go.Figure()
fig_screening.add_trace(go.Scatter(x=df_testing['week_ending'], y=df_testing['asym_screened'],
mode='lines+markers',
name='Asymptomatic',
))
fig_screening.add_trace(go.Scatter(x=df_testing['week_ending'], y=df_testing['sym_screened'],
mode='lines+markers',
name='Symptomatic',
))
fig_screening.add_trace(go.Scatter(x=df_testing['week_ending'], y=df_testing['total_screened'],
mode='lines+markers',
name='Total',
))
fig_screening.update_layout(
title=f'Weekly number of asymptomatic and symptomatic tests',
xaxis_title='Week Ending Date',
yaxis_title='Number',
legend_title='Test Type',
)
fig_positives = go.Figure()
fig_positives.add_trace(go.Scatter(x=df_testing['week_ending'], y=df_testing['asym_positive'],
mode='lines+markers',
name='Asymptomatic',
))
fig_positives.add_trace(go.Scatter(x=df_testing['week_ending'], y=df_testing['sym_positive'],
mode='lines+markers',
name='Symptomatic',
))
fig_positives.add_trace(go.Scatter(x=df_testing['week_ending'], y=df_testing['other_positive'],
mode='lines+markers',
name='Other (NHS/Targeted)',
))
fig_positives.add_trace(go.Scatter(x=df_testing['week_ending'], y=df_testing['total_positive'],
mode='lines+markers',
name='Total',
))
fig_positives.update_layout(
title=f"Weekly Confirmed Cases (as of {df_testing.iloc[-1]['week_ending'].strftime('%Y-%m-%d')})",
xaxis_title='Week Ending Date',
yaxis_title='Number',
legend_title='Test Type',
)
fig_positivity_rate = go.Figure()
fig_positivity_rate.add_trace(go.Scatter(x=df_testing['week_ending'], y=df_testing['asym_positive_rate'],
mode='lines+markers',
name='Asymptomatic',
))
fig_positivity_rate.add_trace(go.Scatter(x=df_testing['week_ending'], y=df_testing['sym_positive_rate'],
mode='lines+markers',
name='Symptomatic',
))
fig_positivity_rate.update_layout(
title=f'Weekly asymptomatic/symptomatic test positivity rate',
xaxis_title='Week Ending Date',
yaxis_title='Percentage',
legend_title='Test Type',
)
df_participation = pd.read_csv('./data/participation.csv')
df_participation['week_ending'] = pd.to_datetime(
df_participation['week_ending'])
fig_participation = go.Figure()
fig_participation.add_trace(go.Scatter(x=df_participation['week_ending'], y=df_participation['consented'],
mode='lines+markers',
name='Consented',
hovertext=df_participation['consented_percent'].astype('str') + '%'))
fig_participation.add_trace(go.Scatter(x=df_participation['week_ending'], y=df_participation['declined'],
mode='lines+markers',
name='Declined',
hovertext=df_participation['declined_percent'].astype('str') + '%'))
fig_participation.add_trace(go.Scatter(x=df_participation['week_ending'], y=df_participation['undecided'],
mode='lines+markers', name='Undecided',
hovertext=df_participation['undecided_percent'].astype('str') + '%'))
fig_participation.update_layout(
title=f"Testing Participation (Total Eligible = {df_participation.iloc[-1]['consented'] + df_participation.iloc[-1]['declined'] + df_participation.iloc[-1]['undecided']})",
xaxis_title='Week Ending Date',
yaxis_title='Number',
legend_title='Status',
)
def make_signed(n):
if n < 0:
return str(n)
else:
return '+' + str(n)
app.layout = dbc.Container(className='container my-5 px-5 pt-5 pb-3', children=[
html.H1(children='University of Cambridge COVID-19 Dashboard'),
html.Div(children='''
Updated weekly. Last update: 08 December 2020
'''),
html.Div(children=[
'Unofficial student-run dashboard. Data from ',
html.A(
href='https://www.cam.ac.uk/coronavirus/stay-safe-cambridge-uni/data-from-covid-19-testing-service',
target='_blank',
children='University of Cambridge'
),
'.'
]
),
html.Div(className='my-5',
children=[
dbc.Row(
[
dbc.Col(html.Div('Total Confirmed Cases')),
dbc.Col(html.Div('Weekly Asymptomatic Tests')),
dbc.Col(html.Div('Weekly Asymptomatic Positivity Rate')),
]
),
dbc.Row(
[
dbc.Col(html.Div(html.H5(
f"{latest_cases['total_confirmed']} ({make_signed(latest_cases['week_confirmed'])})"))),
dbc.Col(html.Div(html.H5(
f"{df_testing.iloc[-1]['asym_screened']} ({make_signed(df_testing.iloc[-1]['asym_screened'] - df_testing.iloc[-2]['asym_screened'])})"))),
dbc.Col(html.Div(html.H5(
f"{df_testing.iloc[-1]['asym_positive_rate']}% ({make_signed(round(df_testing.iloc[-1]['asym_positive_rate'] - df_testing.iloc[-2]['asym_positive_rate'], 2))}%)"))),
]
),
]
),
dcc.Graph(
id='test-positive-graph',
figure=fig_positives
),
dcc.Graph(
id='cases-graph',
figure=fig_cases
),
# html.Div(className='my-5',
# children=[
# html.H4(className='mb-4', children='Breakdown by College'),
# table_colleges,
# ]
# ),
dcc.Graph(
id='test-screening-graph',
figure=fig_screening
),
dcc.Graph(
id='test-positivity-graph',
figure=fig_positivity_rate
),
dcc.Graph(
id='participation-graph',
figure=fig_participation
),
html.Div(className='my-3', children=[
html.H4(className='mb-3', children='Notes'),
html.Ul([
html.Li([
html.Strong('20 October 2020: '),
'The University has stopped providing a breakdown of students and staff cases, therefore only the total number of cases is available from the week ending 18th October 2020.']
)
])
]),
html.Div(className='mt-5',
children=[
dbc.Row(
children=dbc.Col(
className='d-flex justify-content-center',
children=html.A(href='https://github.com/fpervaiz/UniversityOfCambridge-COVID-19-Dashboard',
target='_blank', children='GitHub'))
),
dbc.Row(
children=dbc.Col(
className='d-flex justify-content-center',
children=html.A(href='https://faizaanpervaiz.me',
target='_blank', children='Faizaan Pervaiz'))
),
dbc.Row(
children=dbc.Col(
className='d-flex justify-content-center',
children=html.A(href='https://phil.cdc.gov/Details.aspx?pid=23311',
target='_blank', children='Image credits: CDC PHIL'))
)
])
])
if __name__ == '__main__':
app.run_server(debug=os.getenv('FLASK_ENV') == 'development')