-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeline.py
executable file
·147 lines (138 loc) · 5.26 KB
/
timeline.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
from modules import *
from web_scrape import *
from sidebar import *
import extras
external_stylesheets = [extras.theme]
colors = {
'background': '#111111',
'text': '#FFFFFF'
}
page_countries = eval(requests.get('https://cacheserver.pythonanywhere.com').content)
countries = pd.DataFrame.from_dict(page_countries)['Country'].values.tolist()
slugs = pd.DataFrame.from_dict(page_countries)['Slug'].values.tolist()
page = requests.get('https://cacheserver.pythonanywhere.com?c=india', verify=False)
dates = pd.DataFrame.from_dict(page.json())['Date'].values.tolist()
ddc = []
for i, country in enumerate(countries):
ddc.append({'label': country, 'value': slugs[i]})
datesF = []
for i in dates:
s = i.split('-')
a = int(s[0])
b = int(s[1])
c = int(s[2][:2])
datesF.append((a,b,c))
app = dash.Dash(__name__,
external_stylesheets=external_stylesheets,
requests_pathname_prefix='/timeline/'
)
CONTENT_STYLE = {
"margin-left": "1rem",
"margin-right": "1rem"
}
app.layout = html.Div([
html.Base(target="_parent"),
navbar("Timeline"),
html.Div([
html.Div(id = 'Fe_He', children=[
dcc.DatePickerRange(
id='dat_',
min_date_allowed=dt(datesF[0][0], datesF[0][1], datesF[0][2]),
max_date_allowed=dt(datesF[-1][0], datesF[-1][1], datesF[-1][2]),
initial_visible_month=dt(datesF[-1][0], datesF[-1][1], datesF[-1][2]),
start_date=dt(datesF[0][0], datesF[0][1], datesF[0][2]).date(),
end_date=dt(datesF[-1][0], datesF[-1][1], datesF[-1][2]).date(),
display_format='MMM|Do|YY'
)
]),
html.Div([
dcc.Dropdown(
id = 'drop',
options = ddc,
style=dict(width='100%'),
value='india'
)
]),
html.Div([
html.H4(f'Timeline', id='nono_', style={'color':'white'}),
dcc.Graph(id='update-graph'),
dcc.Interval(
id='interval-component',
interval=5000,
n_intervals=0
)
], className='box')],
style=CONTENT_STYLE)
]
)
@app.callback(Output('Fe_He', 'children'),
[Input('drop', 'value')])
def fnucs(val_d):
URL = 'https://cacheserver.pythonanywhere.com/?c=' + val_d
page = requests.get(URL, verify=False)
dates = pd.DataFrame.from_dict(page.json())['Date'].values.tolist()
global datesF
datesF = []
for i in dates:
s = i.split('-')
a = int(s[0])
b = int(s[1])
c = int(s[2][:2])
datesF.append((a,b,c))
return dcc.DatePickerRange(
id='dat_',
min_date_allowed=dt(datesF[0][0], datesF[0][1], datesF[0][2]),
max_date_allowed=dt(datesF[-1][0], datesF[-1][1], datesF[-1][2]),
initial_visible_month=dt(datesF[-1][0], datesF[-1][1], datesF[-1][2]),
start_date=dt(datesF[0][0], datesF[0][1], datesF[0][2]).date(),
end_date=dt(datesF[-1][0], datesF[-1][1], datesF[-1][2]).date(),
display_format='MMM|Do|YY'
)
@app.callback(Output('update-graph', 'figure'),
[Input('drop', 'value'),
Input('interval-component', 'n_intervals'),
Input('dat_', 'start_date'),
Input('dat_', 'end_date')
])
def plot_bar(val, n, start, end):
'''
Plots the collected data
'''
start = start.split('-')
end = end.split('-')
start_index = datesF.index((int(start[0]), int(start[1]), int(start[2])))
end_index = datesF.index((int(end[0]), int(end[1]), int(end[2]))) + 1
print(start_index, end_index)
page = requests.get('https://cacheserver.pythonanywhere.com/?c=' + val)
dfc = pd.DataFrame.from_dict(page.json())
fig = ms(rows = 2, cols=2,
specs=[[{}, {}],
[{"colspan": 2}, None]],
)
print(len(dfc['Date']))
fig.add_trace(go.Bar(x=dfc['Date'][start_index:end_index], y=dfc['Deaths'][start_index:end_index], name='Deaths'),1,1)
fig.add_trace(go.Scatter(x=dfc['Date'][start_index:end_index], y=dfc['Deaths'][start_index:end_index], name='Deaths'),1,1)
fig.add_trace(go.Bar(x=dfc['Date'][start_index:end_index], y=dfc['Recovered'][start_index:end_index], name='Recoveries'),1,2)
fig.add_trace(go.Scatter(x=dfc['Date'][start_index:end_index], y=dfc['Recovered'][start_index:end_index], name='Recoveries'),1,2)
fig.add_trace(go.Bar(x=dfc['Date'][start_index:end_index], y=dfc['Confirmed'][start_index:end_index], name='Confirmed Cases'),2,1)
fig.add_trace(go.Scatter(x=dfc['Date'][start_index:end_index], y=dfc['Confirmed'][start_index:end_index], name='Confirmed Cases'),2,1)
fig.update_layout(showlegend=False,
legend=dict(
font=dict(
color="white"
)
),
margin=dict(
l=0,
r=0,
t=0,
pad=0
),
paper_bgcolor='#0f2537')
fig.update_xaxes(tickangle=90, tickfont=dict(family='Rockwell', color='white'))
fig.update_yaxes(tickfont=dict(family='Rockwell', color='white'))
return fig
app.index_string = extras.ind_str
app.title = 'Corona Tracker'
if __name__ == '__main__':
app.run_server(debug=False)