forked from Beracle/06-COVID-19-DashBoard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
COVID-19-Dashboard-Test.py
233 lines (201 loc) · 8.66 KB
/
COVID-19-Dashboard-Test.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
import pandas as pd
pd.set_option('max_rows',20)
import plotly.express as px
import plotly.io as pio
pio.renderers.default = 'browser'
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
external_stylesheets = [dbc.themes.BOOTSTRAP]
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
app.title = 'Covid-19 Dashboard'
CONF_URL = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv'
DEAD_URL = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv'
RECV_URL = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv'
covid_conf_ts = pd.read_csv(CONF_URL)
covid_dead_ts = pd.read_csv(DEAD_URL)
covid_recv_ts = pd.read_csv(RECV_URL)
#get data in cleaned time series format for country
def process_data(data,cntry='China',window=3):
conf_ts = data
conf_ts_cntry = conf_ts[conf_ts['Country/Region']==cntry]
final_dataset = conf_ts_cntry.T[4:].sum(axis='columns').diff().rolling(window=window).mean()[40:]
df = pd.DataFrame(final_dataset,columns=['Total'])
return df
#get overall wordlwide total for confirmed, recovered and dead cases
def get_overall_total(df):
return df.iloc[:,-1].sum()
conf_overall_total = get_overall_total(covid_conf_ts)
dead_overall_total = get_overall_total(covid_dead_ts)
recv_overall_total = get_overall_total(covid_recv_ts)
#get total for confirmed, recovered and dead for country
def get_cntry_total(df,cntry='China'):
return df[df['Country/Region']==cntry].iloc[:,-1].sum()
cntry = 'China'
conf_cntry_total = get_cntry_total(covid_conf_ts,cntry)
dead_cntry_total = get_cntry_total(covid_dead_ts,cntry)
recv_cntry_total = get_cntry_total(covid_recv_ts,cntry)
#Generate Line Graph using Plotly
def fig_world_trend(cntry='China',window=3):
df = process_data(data=covid_conf_ts,cntry=cntry,window=window)
df.head(10)
if window==1:
yaxis_title = "Daily Cases"
else:
yaxis_title = "Daily Cases ({}-day MA)".format(window)
fig = px.line(df, y='Total', x=df.index, title='Daily confirmed cases trend for {}'.format(cntry),height=600,color_discrete_sequence =['maroon'])
fig.update_layout(title_x=0.5,plot_bgcolor='#F2DFCE',paper_bgcolor='#F2DFCE',xaxis_title="Date",yaxis_title=yaxis_title)
return fig
#Page Header
colors = {
'background': '#111111',
'bodyColor':'#F2DFCE',
'text': '#7FDBFF'
}
def get_page_heading_style():
return {'backgroundColor': colors['background']}
def get_page_heading_title():
return html.H1(children='COVID-19 Dashboard',
style={
'textAlign': 'center',
'color': colors['text']
})
def get_page_heading_subtitle():
return html.Div(children='Visualize Covid-19 data generated from sources all over the world.',
style={
'textAlign':'center',
'color':colors['text']
})
def generate_page_header():
main_header = dbc.Row(
[
dbc.Col(get_page_heading_title(),md=12)
],
align="center",
style=get_page_heading_style()
)
subtitle_header = dbc.Row(
[
dbc.Col(get_page_heading_subtitle(),md=12)
],
align="center",
style=get_page_heading_style()
)
header = (main_header,subtitle_header)
return header
#Select Country dropdown
def get_country_list():
return covid_conf_ts['Country/Region'].unique()
def create_dropdown_list(cntry_list):
dropdown_list = []
for cntry in sorted(cntry_list):
tmp_dict = {'label':cntry,'value':cntry}
dropdown_list.append(tmp_dict)
return dropdown_list
def get_country_dropdown(id):
return html.Div([
html.Label('Select Country'),
dcc.Dropdown(id='my-id'+str(id),
options=create_dropdown_list(get_country_list()),
value='China'
),
html.Div(id='my-div'+str(id))
])
#Graph Container for DASH
def graph1():
return dcc.Graph(id='graph1',figure=fig_world_trend('China'))
#Generate CARDS for overall numbers
def generate_card_content(card_header,card_value,overall_value):
card_head_style = {'textAlign':'center','fontSize':'150%'}
card_body_style = {'textAlign':'center','fontSize':'200%'}
card_header = dbc.CardHeader(card_header,style=card_head_style)
card_body = dbc.CardBody(
[
html.H5(f"{int(card_value):,}", className="card-title",style=card_body_style),
html.P(
"Worlwide: {:,}".format(overall_value),
className="card-text",style={'textAlign':'center'}
),
]
)
card = [card_header,card_body]
return card
def generate_cards(cntry='China'):
conf_cntry_total = get_cntry_total(covid_conf_ts,cntry)
dead_cntry_total = get_cntry_total(covid_dead_ts,cntry)
recv_cntry_total = get_cntry_total(covid_recv_ts,cntry)
cards = html.Div(
[
dbc.Row(
[
dbc.Col(dbc.Card(generate_card_content("Recovered",recv_cntry_total,recv_overall_total), color="success", inverse=True),md=dict(size=2,offset=3)),
dbc.Col(dbc.Card(generate_card_content("Confirmed",conf_cntry_total,conf_overall_total), color="warning", inverse=True),md=dict(size=2)),
dbc.Col(dbc.Card(generate_card_content("Dead",dead_cntry_total,dead_overall_total),color="dark", inverse=True),md=dict(size=2)),
],
className="mb-4",
),
],id='card1'
)
return cards
#DASH Slider for Moving Average Window
def get_slider():
return html.Div([
dcc.Slider(
id='my-slider',
min=1,
max=15,
step=None,
marks={
1: '1',
3: '3',
5: '5',
7: '1-Week',
14: 'Fortnight'
},
value=3,
),
html.Div([html.Label('Select Moving Average Window')],id='my-div'+str(id),style={'textAlign':'center'})
])
# Generate APP layout
def generate_layout():
page_header = generate_page_header()
layout = dbc.Container(
[
page_header[0],
page_header[1],
html.Hr(),
generate_cards(),
html.Hr(),
dbc.Row(
[
dbc.Col(get_country_dropdown(id=1), md=dict(size=4, offset=4))
]
),
dbc.Row(
[
dbc.Col(graph1(), md=dict(size=6, offset=3))
],
align="center",
),
dbc.Row(
[
dbc.Col(get_slider(), md=dict(size=4, offset=4))
]
),
], fluid=True, style={'backgroundColor': colors['bodyColor']}
)
return layout
app.layout = generate_layout()
# Assign DASH Callbacks
@app.callback(
[Output(component_id='graph1',component_property='figure'), #line chart
Output(component_id='card1',component_property='children')], #overall card numbers
[Input(component_id='my-id1',component_property='value'), #dropdown
Input(component_id='my-slider',component_property='value')] #slider
)
def update_output_div(input_value1,input_value2):
return fig_world_trend(input_value1,input_value2),generate_cards(input_value1)
if __name__ == '__main__':
app.run_server(debug=True)