-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
289 lines (253 loc) · 13.1 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
from covid_india import states
import streamlit as st
import plotly.graph_objects as go
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import rcParams
import plotly_express as px
import requests
from bs4 import BeautifulSoup
import seaborn as sns
from lang import *
from datetime import date, timedelta
import json
from pandas.io.json import json_normalize
rcParams.update({'figure.autolayout': True})
SUBHEAD_TITLE = "EpiSight"
SUBHEAD_CREDITS = "Made by Epochalypse Team for HackJaipur 2020"
def get_ui_for_data(data_input):
column_properties = "margin:3%; float:right; width:15%; text-align: right"
base_card_styling = "margin:1%; padding:1%; " \
"text-align:center;" \
"width:16%; " \
"box-shadow:0 4px 8px 0 rgba(0,0,0,0.2);"
return f"<div class='row'>" \
f"<h5 style='color:black; {column_properties}'>{data_input[0]}</h5>" \
f"<h5 style='color:blue; {base_card_styling}'><h6>Total</h6>{data_input[1]}</h5> " \
f"<h5 style='color:red; {base_card_styling}'><h6>Active</h6>{data_input[2]}</h5> " \
f"<h5 style='color:green; {base_card_styling}'><h6>Saved</h6>{data_input[3]}</h5> " \
f"<h5 style='color:orange; {base_card_styling}'><h6>Deaths</h6>{data_input[4]}</h5> " \
f"</div>"
@st.cache
def load_global_death_data():
data = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv', error_bad_lines=False)
data.drop(['Province/State', 'Lat', 'Long'], axis=1, inplace=True)
data = data.groupby(['Country/Region']).sum()
return data
def date_convert(df):
df_tran = df.transpose().reset_index()
df_tran.rename({'index': 'Date'}, axis=1, inplace=True)
df_tran['Date'] = pd.to_datetime(df_tran['Date'])
return df_tran
def tidy_death_data(df,group):
df_tidy = pd.melt(df, id_vars=['Date'])
df_tidy.drop(df_tidy[df_tidy['value'] < 10].index, inplace=True) # Drop all dates and countries with less than 10 recorded deaths
df_tidy = df_tidy.assign(Days=df_tidy.groupby(group).Date.apply(lambda x: x - x.iloc[0]).dt.days) # Calculate # of days since 10th death by country
df_tidy['daily_change'] = df_tidy.sort_values([group,'Days']).groupby(group)['value'].diff()
df_tidy['daily_pct_change'] = df_tidy.sort_values([group,'Days']).groupby(group)['value'].pct_change() * 100
df_tidy['daily_roll_avg'] = df_tidy.groupby(group)['daily_change'].rolling(7).mean().round().reset_index(0,drop= True)
df_tidy['daily_pctchange_roll_avg'] = df_tidy.groupby(group)['daily_pct_change'].rolling(7).mean().round().reset_index(0,drop= True)
df_tidy['daily_change'].fillna(0, inplace=True)
df_tidy['daily_pct_change'].fillna(0, inplace=True)
df_tidy['daily_roll_avg'].fillna(df_tidy['daily_change'], inplace=True)
df_tidy['daily_pctchange_roll_avg'].fillna(df_tidy['daily_pct_change'], inplace=True)
return df_tidy
def global_plot_create(data, x, y, title, xaxis, yaxis):
fig = px.line(data, x=x, y=y, color='Country/Region', width=800, height=600)
fig.update_layout(title=title,
xaxis_title= xaxis,
yaxis_title = yaxis,
legend_title_text='Countries',
yaxis_type="log",
yaxis_tickformat = 'f',
xaxis_gridcolor = 'LightBlue',
yaxis_gridcolor = 'LightBlue',
paper_bgcolor='rgba(0,0,0,0)',
plot_bgcolor='rgba(0,0,0,0)')
return fig
def load_data_and_return_dataframe():
all_cases = states.getdata()
data = []
for state in all_cases:
temp = []
temp.append(state)
for key in all_cases[state]:
temp.append(all_cases[state][key])
data.append(temp)
return pd.DataFrame(data, columns=["State", "Total", "Active", "Cured", "Death"])
df = load_data_and_return_dataframe()
def main():
page = st.sidebar.selectbox("Choose a feature", ['Homepage', 'Guidelines', 'Statistics-Global' ,'Statistics-India', 'Hospitals-India', 'Map Visualization'])
if page == 'Homepage':
st.title("EpiSight")
st.header("Exploration of COVID-19 related deaths around the world")
st.subheader("Use the Selection Panel on the left sidebar, to navigate to respective features available.")
st.markdown("This is a Covid-19 Dashboard, made purely using Python and Streamlit. Streamlit makes it super easy to make any sort of Python apps like Machine Learning and Data Science Apps.")
st.write("We have explored the Indian as well Global representation of Covid-related statistics and the effects of the pandemic since January 2020.")
st.subheader("Contributing:")
st.markdown("The project is Open-Sourced on [GitHub](https://github.com/Xacrolyte/MLHJepoch), under the MIT Licence. Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.")
elif page =='Guidelines':
langugaes=['English','हिंदी','Française','Deutsche']
lang=st.selectbox('Choose Language',langugaes)
st.subheader('WHO Guidelines')
if(lang=='English'):
st.markdown(english())
elif(lang=='Française'):
st.markdown(french())
elif(lang=='हिंदी'):
st.markdown(hindi())
elif(lang=='Deutsche'):
st.markdown(french())
elif page == 'Statistics-Global':
global_data = load_global_death_data()
global_data = date_convert(global_data)
global_deaths = tidy_death_data(global_data, group = 'Country/Region')
st.title('Global COVID-19 Deaths')
st.header('Daily COVID-19 deaths, sorted by country, from Jan 22, 2020 - Present.')
st.write("Raw Data:", global_data)
cols = list(global_data[global_data.columns.difference(['Date'])])
countries = st.multiselect('Select countries to display', cols, ["US", "India", "China"])
global_deaths.set_index('Country/Region', inplace=True)
data_plot = global_deaths.loc[countries]
data_plot.reset_index(inplace=True)
cols = ['Total Confirmed Deaths', 'Deaths per Day','Daily Percentage Change']
variable = st.selectbox('Select Statistic to be displayed:', cols)
if variable == 'Total Confirmed Deaths':
fig=global_plot_create(data = data_plot,
x = 'Days',
y = 'value',
title = 'Global COVID-19 Deaths - Total',
xaxis = 'Number of days since 10th death',
yaxis = 'Confirmed Deaths')
st.plotly_chart(fig)
elif variable == 'Deaths per Day':
fig=global_plot_create(data = data_plot,
x = 'Days',
y = 'daily_roll_avg',
title = 'Average death rate worldwide',
xaxis = 'Number of days',
yaxis = 'Average Deaths')
st.plotly_chart(fig)
elif variable == 'Daily Percentage Change':
fig=global_plot_create(data = data_plot,
x = 'Days',
y = 'daily_pctchange_roll_avg',
title = 'Daily percentage change globally',
xaxis = 'Number of days',
yaxis = 'Change in percentage')
st.plotly_chart(fig)
elif page == 'Statistics-India':
st.title("COVID19 India DashBoard")
st.header("COVID19 Dashboard made using Python and Streamlit")
st.subheader("Check any State Status")
sel_state = st.multiselect(label="Select State", options=df["State"])
if sel_state:
states = []
for c in sel_state:
states.append(df[df["State"] == c].values)
for arr in states:
data = arr[0]
st.markdown(get_ui_for_data(data), unsafe_allow_html=True)
st.markdown("<h2>Top 10 States affected</h2>", unsafe_allow_html=True)
top_10 = df.nlargest(10, ["Total"])
fig = go.Figure(data = [
go.Bar(name="Total", x=top_10["State"], y=top_10["Total"]),
go.Bar(name="Active", x=top_10["State"], y=top_10["Active"]),
go.Bar(name="Cured", x=top_10["State"], y=top_10["Cured"]),
go.Bar(name="Deaths", x=top_10["State"], y=top_10["Death"])
])
st.plotly_chart(fig, use_container_width=True)
for data in df.nlargest(10, ["Total"]).values:
st.markdown(get_ui_for_data(data), unsafe_allow_html=True)
st.markdown("<h3> All States Data</h3>", unsafe_allow_html=True)
st.dataframe(df.style.background_gradient(cmap='Blues',subset=["Total"])\
.background_gradient(cmap='Reds',subset=["Active"])\
.background_gradient(cmap='Greens',subset=["Cured"])\
.background_gradient(cmap='Purples',subset=["Death"]))
locations = {
"Kerala" : [10.8505,76.2711],
"Maharashtra" : [19.7515,75.7139],
"Karnataka": [15.3173,75.7139],
"Telangana": [18.1124,79.0193],
"Uttar Pradesh": [26.8467,80.9462],
"Rajasthan": [27.0238,74.2179],
"Gujarat":[22.2587,71.1924],
"Delhi" : [28.7041,77.1025],
"Punjab":[31.1471,75.3412],
"Tamil Nadu": [11.1271,78.6569],
"Haryana": [29.0588,76.0856],
"Madhya Pradesh":[22.9734,78.6569],
"Jammu and Kashmir":[33.7782,76.5762],
"Ladakh": [34.1526,77.5770],
"Andhra Pradesh":[15.9129,79.7400],
"West Bengal": [22.9868,87.8550],
"Bihar": [25.0961,85.3131],
"Chhattisgarh":[21.2787,81.8661],
"Chandigarh":[30.7333,76.7794],
"Uttarakhand":[30.0668,79.0193],
"Himachal Pradesh":[31.1048,77.1734],
"Goa": [15.2993,74.1240],
"Odisha":[20.9517,85.0985],
"Andaman and Nicobar Islands": [11.7401,92.6586],
"Puducherry":[11.9416,79.8083],
"Manipur":[24.6637,93.9063],
"Mizoram":[23.1645,92.9376],
"Assam":[26.2006,92.9376],
"Meghalaya":[25.4670,91.3662],
"Tripura":[23.9408,91.9882],
"Arunachal Pradesh":[28.2180,94.7278],
"Jharkhand" : [23.6102,85.2799],
"Nagaland": [26.1584,94.5624],
"Sikkim": [27.5330,88.5122],
"Dadra and Nagar Haveli":[20.1809,73.0169],
"Lakshadweep":[10.5667,72.6417],
"Daman and Diu":[20.4283,72.8397]
}
map_data = pd.DataFrame.from_dict(locations, orient='index',
columns=['latitude','longitude'])
st.map(map_data)
elif page == 'Map Visualization':
st.title("Live Map Visualization")
st.header("Please drag the pointer over the map to scroll. Hover over a region for related info:")
st.write("Scroll for Zoom")
today = date.today()
yesterday = today - timedelta(days=1)
d1 = yesterday.strftime("%m-%d-%Y")
file_url = 'https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/' + d1 + '.csv'
dfg = pd.read_csv(file_url)
data = dfg[['Confirmed','Lat','Long_','Country_Region','Recovered','Deaths']]
data.rename( columns={'Lat':'lat','Long_':'lon'},inplace = True)
data = data.dropna()
fig = px.scatter_mapbox(data,lat="lat", lon="lon", hover_name="Country_Region", hover_data=["Confirmed",'Recovered','Deaths'],
color_discrete_sequence=["firebrick"], zoom=3, height=300)
fig.update_layout(mapbox_style="open-street-map")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
st.plotly_chart(fig)
elif page == 'Hospitals-India':
st.title("Hospitals-India Map Visualization")
st.header("Please drag the pointer over the map to scroll. Hover over a region for related info:")
st.write("Scroll for Zoom")
dfh='https://api.rootnet.in/covid19-in/stats/hospitals'
client=requests.get(dfh)
data=client.content
data=json.loads(data)
st.write("Total Number of Hospitals = ",data['data']['summary']['totalHospitals'])
st.write("Total Number of Beds = ", data['data']['summary']['totalBeds'])
st.write("Number of urban Hospitals = ", data['data']['summary']['urbanHospitals'])
st.write("Number of rural Hospitals = ", data['data']['summary']['ruralHospitals'])
dfh=json_normalize(data['data']['regional'])
choice = st.selectbox(label="Select State", options=dfh["state"])
value=dfh.loc[dfh["state"]==choice]
st.table(value)
st.write("Raw Data: ", dfh)
hide_streamlit_style = """
<style>
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
</style>
"""
st.markdown(hide_streamlit_style, unsafe_allow_html=True)
st.sidebar.title(SUBHEAD_TITLE)
st.sidebar.subheader(SUBHEAD_CREDITS)
if __name__ == '__main__':
main()