-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_test_scr.py
229 lines (214 loc) · 9.03 KB
/
python_test_scr.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
# dash for layout
import dash
import dash_table
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
from dash.dependencies import State, Input, Output
from dash.exceptions import PreventUpdate
import io
from flask import Flask, request, render_template, redirect, url_for, send_file
import plotly
import pandas as pd
import numpy as np
from random import random
from bs4 import BeautifulSoup
import requests
import plotly.express as px
# Plotly function to generate table plot
app = dash.Dash()
def table_creater(df):
df = df.dropna(subset=['Country'])
fig = go.Figure(data=[go.Table(
header=dict(values=list(df.columns),
line_color='rgb(60,60,60)',
fill_color='rgb(40,40,40)',
align='left',font=dict(color='white', size=17)),
cells=dict(values=df.T.values,
fill_color='rgb(20,10,30)',
line_color='rgb(20,10,30)',
align='left',font=dict(color='white', size=14)))
])
fig.update_layout(
autosize=False,
margin=dict(l=0,r=0,b=0,t=0,pad=0),
paper_bgcolor='rgb(20,10,30)',
)
return(fig.data,fig.layout)
##################################################################################
def get_corona_data():
url="https://www.worldometers.info/coronavirus/"
# Make a GET request to fetch the raw HTML content
html_content = requests.get(url).text
# Parse the html content
soup = BeautifulSoup(html_content, "lxml")
gdp_table = soup.find("table", id = "main_table_countries_today")
gdp_table_data = gdp_table.tbody.find_all("tr")
# Getting all countries names
dicts = {}
for i in range(len(gdp_table_data)):
try:
key = (gdp_table_data[i].find_all('a', href=True)[0].string)
except:
key = (gdp_table_data[i].find_all('td')[0].string)
value = [j.string for j in gdp_table_data[i].find_all('td')]
dicts[key] = value
live_data= pd.DataFrame(dicts).drop(0).T
live_data.columns = ["Total Cases","New Cases", "Total Deaths", "New Deaths", "Total Recovered","Active","Serious Critical",
"Tot Cases/1M pop"]
live_data.index.name = 'Country'
live_data.iloc[:,:5].to_csv("input_data/base_data.csv")
################################################################################################
def horigental_plots(df):
df['Active cases'] = df['Total Cases']- (df.iloc[:,2:].sum(1))
df = df.drop('Total Cases',1)
big_df = pd.DataFrame(columns= ['Country',"Total Cases","Type"])
for i in df.columns[1:]:
small_df = df[["Country",i]].rename(columns = {i:"Total Cases"}).reset_index(drop=True)
small_df["Type"] = i
big_df = big_df.append(small_df)
fig = px.bar(big_df, x="Total Cases", y="Country", color='Type', orientation='h',
title='Current count',opacity=.9,text="Total Cases")
fig.layout.yaxis.showtickprefix = 'first'
fig.update_layout(plot_bgcolor='rgb(20,10,30)',
paper_bgcolor='rgb(20,10,30)',
font=dict(family="Courier New, monospace",
color="white"),margin= dict(t=0,r=0,b=40,l=40),legend = dict(x=.5,y=1),
width=500,height=300)
fig.update_yaxes(ticks="inside",tickangle = -55)
return(fig.data,fig.layout)
###############################################################################################################################
def build_upper_left_panel():
return html.Div(
id="upper-left",
className="six columns",
children=[
html.P(
id="section-title",
children="SPREAD RATE OF CORONA AROUND THE WORLD LIVE",
),
html.Div(
className="control-row-1",
children=[
html.Div(
id="state-select-outer",
children=[
html.Div([
dcc.Interval(
id='interval-component-2',
interval=30*1000, # in milliseconds
n_intervals=0
),
dcc.Interval(
id='interval-component-1',
interval=590*1000, # in milliseconds
n_intervals=0
)
]),
html.Div(id="loading-outer-frame1",
children=[]),
html.Div(dcc.Loading(
id="loading3",
children=dcc.Graph(
id='graph3',
figure={
"data": [],
"layout": []
},
style={'width':'400%','hight':'400','margin':'0%'}),
),
)
]
),
],
)],
style={'width':'50%','hight':'120%','margin':'0%','display':'inline-block'})
#################################################################################################################
app.layout = html.Div(
className="container scalable",
children=[
html.Div(
id="banner",
className="banner",
children=[
html.H6("CORONAVIRUS EPIDEMIC CASES AROUND THE WORLD LIVE COUNT AND ANALYSIS")
],style={'color': 'white', 'fontSize': 100,'font-family' :'Arial Black'}
),
html.Div(
id="upper-container",
className="row",
children=[
build_upper_left_panel(),
html.Div(
id="geo-map-outer",
className="six columns",
children=[
html.P(
id="map-title",
children="CONFIRMED CASES AND DEATHS BY COUNTRY UPDATED EVERY 10 SECONDS",
),
html.Div(
id="loading-outer-frame",
children=[
dcc.Loading(
id="loading",
children=dcc.Graph(
id='graph1',
figure={
"data": [],
"layout": []
},
style={'width':'100%','hight':'100%','margin':'0%'}),
)
],
),
]
, style={'width':'46%','hight':'120%','margin':'0%','display':'inline-block','animation-name': 'example',
'animation-duration': '10s','animation-iteration-count': 'infinite'}),
],
)
],
style={'width':'100%','hight':'120%','margin':'0%'})
## Callback function to generate take input on dropdown and update the child checklist
@app.callback(
[Output('graph1', "figure"),
Output('graph3', "figure"),
Output("loading-outer-frame1", "children")],
[Input('interval-component-2', 'n_intervals')],
)
def update_region_dropdown(state_select):
df = pd.read_csv("input_data/base_data.csv")
df = df.loc[np.unique(np.random.randint(0,df.shape[0],(40)))]
for i in df.columns[1:]:
df[i] = df[i].apply(lambda x : x.replace(",","").replace(" ",""))
df[i] = df[i].apply(lambda x : int(x) if len(x)>0 else 0)
df = df.sort_values('Total Cases',ascending =False)
traces,layouts=table_creater(df)
traces_hori1,layouts_hori2=horigental_plots(df.iloc[:8,:])
return (
[{
'data':traces,
'layout':layouts
}
,
{
'data':traces_hori1,
'layout':layouts_hori2
},
html.Video(src='/static/2_5_1.webm',controls=True,autoPlay=True,loop=True,width="220%",height='400%')]
)
########################################################################################
@app.callback([Output('section-title', "children")],
[Input('interval-component-1', 'n_intervals')]
)
def download_data(state_select):
get_corona_data()
df = pd.read_csv("input_data/base_data.csv",index_col=[0])
for i in df.columns:
df[i] = df[i].apply(lambda x : x.replace(",","").replace(" ",""))
df[i] = df[i].apply(lambda x : int(x) if len(x)>0 else 0)
cc =df.sum()
return (
["TOTAL CASES- {}, NEW CASES- {} ...... TOTAL DEATHS- {}, NEW DEATHS-{} ...... TOTAL RECOVERED-{}".format(cc[0],cc[1],cc[2],cc[3],cc[4])])
if __name__=='__main__':
app.run_server()