-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.py
executable file
·410 lines (385 loc) · 13.1 KB
/
table.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
from modules import *
from web_scrape import *
from sidebar import *
from werkzeug.wsgi import FileWrapper
from flask import Response
import extras
df = pd.DataFrame([[1,2,3],
[4,5,6]])
df.columns = ["a", "b", "c"]
start_table_df = pd.DataFrame(columns=[''])
color1 = '#48536D'
color2 = '#5A6D81'
colors = {
'background': '#111111',
'text': '#FFFFFF'
}
tab_style = {
'height': '20px',
'width': '100px',
'color': 'white',
'backgroundColor': 'black'
}
tab_selected_style = {
'height': '20px',
'width': '100px',
'color': 'red',
'backgroundColor': 'grey'
}
s1 = {
'height': '20px',
'width': '200px',
'color': 'white',
'backgroundColor': 'black'
}
s2 = {
'height': '20px',
'width': '200px',
'color': 'red',
'backgroundColor': 'grey'
}
CONTENT_STYLE = {
"margin-left": "1rem",
"margin-right": "1rem"
}
external_stylesheets = [extras.theme]
app = dash.Dash(__name__,
external_stylesheets=external_stylesheets,
requests_pathname_prefix='/datatable/'
)
app.layout = html.Div(children=[
html.Base(target="_parent"),
html.Div([
navbar("DataTable")
]),
html.Div([
html.Div(id='dropper', children=[
dcc.Dropdown(
id='drop',
options=[
{'label': 'World', 'value':'world'},
{'label': 'India', 'value':'india'}
],
value='world',
searchable=False,
clearable=False
)
]),
html.Div(id='tabs', children=[
dcc.Dropdown(id='tabs-table'),
dcc.Dropdown(id='india-tabs')
]),
html.Div(id='name99', style={'display':'none'}),
html.Div(id='name88', style={'display':'none'}),
html.Div(id='dropper-ind', children=[
dcc.Dropdown(
id='dhop'
)
]),
html.Div(id='testing'),
html.Div(id='tenten', style={'display':'none'}),
html.Div(id='table_div', children=[
dash_table.DataTable(
id='table',
data=start_table_df.to_dict('records'),
columns = [{'id': c, 'name': c} for c in start_table_df.columns],
style_header={'backgroundColor': 'rgb(30, 30, 30)','color':'white'},
style_cell={
'backgroundColor': color1,
'color': 'white'
},
style_data_conditional = [
{
'if': {'row_index': 'odd'},
'backgroundColor': color2
},
],
sort_mode="multi",
row_selectable="multi",
fixed_rows={'headers': True},
selected_rows=[],
page_current=0,
page_size=20,
style_table={
"overflowX": "auto",
"height": "80vh",
"borderRadius": "15px"},
filter_action="native",
sort_action="native",
),
dcc.Interval(
id='interval-com',
interval=10000000,
n_intervals=0
)
], className="spb"),
html.Div(id='tablet_div', children=[
dash_table.DataTable(
id='tablet',
style_header={'backgroundColor': 'rgb(30, 30, 30)','color':'white'},
style_cell={
'backgroundColor': color1,
'color': 'white'
},
fixed_rows={'headers': True},
page_size=20,
style_table={
"overflowX": "auto",
"height": "80vh",
"borderRadius": "15px"},
filter_action="native",
sort_action="native"
),
dcc.Interval(
id='interval-component',
interval=10000,
n_intervals=0
)
], className="spb"),
html.Div(id="bbbl")
], style=CONTENT_STYLE)
]
)
@app.callback(Output('bbbl', 'children'),
[Input('table', 'selected_rows')])
def useless(n):
return html.P("hi")
zone = 0
def call_name_time(fe):
global zone
zone = fe
def vst(v):
print(zone)
global ret
ret = v
def single_g(variable):
global varsingle
varsingle = variable
def multi_g(vars1, vars2):
global varmulti1
varmulti1 = vars1
global varmulti2
varmulti2 = vars2
def get_dist(value):
URL = 'https://api.covid19india.org/csv/latest/district_wise.csv'
page = requests.get(URL, verify=False).content
df = pd.read_csv(io.StringIO(page.decode('utf-8')))
df = df[['District', 'Confirmed', 'Active', 'Deceased', 'Recovered', 'State']]
df = df.fillna(0)
url = 'https://api.covid19india.org/zones.json'
districts = []
zones = []
data = pd.read_json(url)
for i in range(len(data)):
districts.append(list(data.xs(i))[0]['district'])
zones.append(list(data.xs(i))[0]['zone'])
hadf = pd.DataFrame()
hadf['District'] = districts
hadf['Zone'] = zones
df = df.merge(hadf)
bool_list = df['State'] == value
df = df[bool_list]
return df
@app.callback(Output('tabs', 'children'),
[Input('drop', 'value')])
def tab(value):
if value=='world':
return dcc.Dropdown(id='tabs-table',
value='All',
options=[
dict(label='All', value='All'),
dict(label='Europe', value='Europe'),
dict(label='North America', value='North America'),
dict(label='South America', value='South America'),
dict(label='Asia', value='Asia'),
dict(label='Africa', value='Africa'),
dict(label='Oceania', value='Oceania')
],
searchable=False,
clearable=False
)
else:
print('gone through')
return dcc.Dropdown(id='india-tabs',
value='Districts',
options=[
dict(label='States', value='States'),
dict(label='Districts', value='Districts')
],
searchable=False,
clearable=False
)
@app.callback(Output('dropper-ind', 'children'),
[Input('drop', 'value'),
Input('india-tabs', 'value'),
Input('interval-component', 'n_intevals')])
def dop(v1, v2, nernw):
if v1 == 'india' and v2 == 'Districts':
op = []
lst = ['Andaman and Nicobar Islands', 'Andhra Pradesh',
'Arunachal Pradesh', 'Assam', 'Bihar', 'Chandigarh',
'Chhattisgarh', 'Delhi',
'Dadra and Nagar Haveli and Daman and Diu', 'Goa', 'Gujarat',
'Himachal Pradesh', 'Haryana', 'Jharkhand', 'Jammu and Kashmir',
'Karnataka', 'Kerala', 'Ladakh', 'Lakshadweep', 'Maharashtra',
'Meghalaya', 'Manipur', 'Madhya Pradesh', 'Mizoram', 'Nagaland',
'Odisha', 'Punjab', 'Puducherry', 'Rajasthan', 'Sikkim',
'Telangana', 'Tamil Nadu', 'Tripura', 'Uttar Pradesh',
'Uttarakhand', 'West Bengal']
for n in lst:
op.append({'label':n, 'value':n})
return dcc.Dropdown(
id='dhop',
options=op,
value='Andaman and Nicobar Islands',
clearable=False
)
else:
pass
@app.callback([Output('table_div', 'style'),
Output('tablet_div', 'style'),
Output('name99', 'style'),
Output('name88', 'style')],
[Input('drop', 'value')])
def anakin(valet):
if valet == 'india':
return {'display':'none'}, {'display':'block'}, \
{'display':'none'}, {'display':'block'}
elif valet == 'world':
return {'display':'block'}, {'display':'none'}, \
{'display':'block'}, {'display':'none'}
@app.callback([Output('table', 'columns'),
Output('table', 'data')],
[Input('tabs-table', 'value'),
Input('drop', 'value'),
Input('interval-com', 'n_intervals')])
def hansolo(val, value, nwemmw):
print(value)
if value == 'world':
URL = "https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/latest/owid-covid-latest.json"
df1 = pd.read_json(requests.get(URL, verify=False).content).transpose()
country = df1["location"]
ncols = ['location', 'continent', 'total_cases', 'total_deaths', 'new_cases', 'new_deaths', 'total_tests', 'tests_per_case', 'total_vaccinations']
col_names = ["Country", "Continent", "Confirmed Case", "Confirmed Deaths",
"New Cases", "New Deaths", "Total Tests", "Tests / Case", "Total Vaccinations"]
df2 = df1[ncols]
df2.columns = col_names
vst(df2)
data = list(df2.to_dict(orient = 'records'))
def fun(val, ndf):
asia = []
x=0
for i in ndf['Continent']:
if i == val:
asia.append(x)
x+=1
ndf = ndf.reset_index()
dec = list(range(len(ndf['Country'])))
diff = list(set(dec) - set(asia))
ndf = ndf.drop('index', axis=1)
data1 = ndf.drop(diff)
return data1
if val != 'All':
data1 = fun(val, df2)
data = list(data1.to_dict(orient='records'))
vst(data1)
df2 = df2.drop("Continent", axis=1)
columns = [{'name':col, 'id':col} for col in df2.columns]
return columns, data
@app.callback([Output('tablet', 'columns'),
Output('tablet', 'data'),
Output('tablet', 'style_data_conditional')],
[Input('india-tabs', 'value'),
Input('dhop', 'value')])
def f1(i_tabs, i_drop):
print('entered')
print(i_tabs)
if i_tabs == 'States':
print('hellllllllllllofwrfkr3jgklr4tgkg45g4kglkl4')
URL = 'https://api.covid19india.org/csv/latest/state_wise.csv'
page = requests.get(URL, verify=False).content
df = pd.read_csv(io.StringIO(page.decode('utf-8')))
ncols = ['State', 'Confirmed', 'Recovered', 'Deaths', 'Active']
df = df[ncols]
vst(df)
ncols = [{'name': col, 'id': col} for col in df.columns]
data=list(df.to_dict("records"))
print(ncols, data)
return ncols, data, [
{
'if': {'row_index': 'odd'},
'backgroundColor': color2
},
]
elif i_tabs == 'Districts':
df = get_dist(i_drop)
vst(df)
data=list(df.to_dict("records"))
ncols = [{'name':col, 'id':col} for col in df.columns]
return ncols, data, [
{
'if': {'row_index': 'odd'},
'backgroundColor': color2
},
{
'if': {
'filter_query': '{Zone} = Red',
'columns_id': 'Zone'
},
'color': 'red'
},
{
'if': {
'filter_query': '{Zone} = Orange',
'columns_id': 'Zone'
},
'color': '#FFA500'
},
{
'if': {
'filter_query': '{Zone} = Green',
'columns_id': 'Zone'
},
'color': '#66ff00'
}
]
FILENAME = ""
@app.callback(Output('name99', 'children'),
[Input('drop', 'value'),
Input('tabs-table', 'value')])
def lint1(ur, utp):
global FILENAME
FILENAME = ur + utp + ".xlsx"
vent = time.time()
call_name_time(vent)
return dbc.Button('Download Data as xlsx', href = 'https://thunder2020.pythonanywhere.com/datatable/corona-report/urlToDownload?value={}'.format(str(vent)), target="_blank", color="dark", id="nen")
@app.callback(Output('name88', 'children'),
[Input('india-tabs', 'value'),
Input('dhop', 'value')])
def lint2(ur2, utp2):
global FILENAME
FILENAME = ur2 + utp2 + ".xlsx"
vent = time.time()
call_name_time(vent)
return dbc.Button('Download Data as xlsx', href = 'https://thunder2020.pythonanywhere.com/datatable/corona-report/urlToDownload?value={}'.format(str(vent)), target="_blank", color="dark", id="nen2")
def r():
return ret
@app.server.route('/corona-report/urlToDownload')
def ex():
kf = r()
print(kf)
print('ha!')
strIO = io.BytesIO()
excel_writer = pd.ExcelWriter(strIO, engine="xlsxwriter")
kf.to_excel(excel_writer, sheet_name="sheet1")
print("Ello")
excel_writer.close()
strIO.seek(0)
w = FileWrapper(strIO)
resp = Response(w, mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", direct_passthrough=True)
resp.headers["Content-Disposition"] = f"attachment; filename=\"{FILENAME}\""
return resp
app.index_string = extras.ind_str
app.title = 'Corona Tracker'
if __name__ == '__main__':
app.run_server(debug=False)