-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathapp.py
182 lines (152 loc) · 6.49 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
import datetime
import json
import numpy as np
import requests
import pandas as pd
import streamlit as st
from copy import deepcopy
from fake_useragent import UserAgent
from footer_utils import image, link, layout, footer
# browser_header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'}
# browser_header = {'User-Agent': 'Mozilla/5.0 (Linux; Android 10; ONEPLUS A6000) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.99 Mobile Safari/537.36'}
st.set_page_config(layout='wide',
initial_sidebar_state='collapsed',
page_icon="https://www.cowin.gov.in/favicon.ico",
page_title="CoWIN Vaccination Slot Availability")
@st.cache(allow_output_mutation=True, suppress_st_warning=True)
def load_mapping():
df = pd.read_csv("district_mapping.csv")
return df
def filter_column(df, col, value):
df_temp = deepcopy(df.loc[df[col] == value, :])
return df_temp
def filter_capacity(df, col, value):
df_temp = deepcopy(df.loc[df[col] > value, :])
return df_temp
@st.cache(allow_output_mutation=True)
def Pageviews():
return []
mapping_df = load_mapping()
rename_mapping = {
'date': 'Date',
'min_age_limit': 'Minimum Age Limit',
'available_capacity': 'Available Capacity',
'vaccine': 'Vaccine',
'pincode': 'Pincode',
'name': 'Hospital Name',
'state_name' : 'State',
'district_name' : 'District',
'block_name': 'Block Name',
'fee_type' : 'Fees'
}
st.title('CoWIN Vaccination Slot Availability')
st.info('The CoWIN APIs are geo-fenced so sometimes you may not see an output! Please try after sometime ')
valid_states = list(np.unique(mapping_df["state_name"].values))
left_column_1, center_column_1, right_column_1 = st.beta_columns(3)
with left_column_1:
numdays = st.slider('Select Date Range', 0, 100, 3)
with center_column_1:
state_inp = st.selectbox('Select State', [""] + valid_states)
if state_inp != "":
mapping_df = filter_column(mapping_df, "state_name", state_inp)
mapping_dict = pd.Series(mapping_df["district id"].values,
index = mapping_df["district name"].values).to_dict()
# numdays = st.sidebar.slider('Select Date Range', 0, 100, 10)
unique_districts = list(mapping_df["district name"].unique())
unique_districts.sort()
with right_column_1:
dist_inp = st.selectbox('Select District', unique_districts)
DIST_ID = mapping_dict[dist_inp]
base = datetime.datetime.today()
date_list = [base + datetime.timedelta(days=x) for x in range(numdays)]
date_str = [x.strftime("%d-%m-%Y") for x in date_list]
temp_user_agent = UserAgent()
browser_header = {'User-Agent': temp_user_agent.random}
final_df = None
for INP_DATE in date_str:
URL = "https://cdn-api.co-vin.in/api/v2/appointment/sessions/public/calendarByDistrict?district_id={}&date={}".format(DIST_ID, INP_DATE)
response = requests.get(URL, headers=browser_header)
if (response.ok) and ('centers' in json.loads(response.text)):
resp_json = json.loads(response.text)['centers']
if resp_json is not None:
df = pd.DataFrame(resp_json)
if len(df):
df = df.explode("sessions")
df['min_age_limit'] = df.sessions.apply(lambda x: x['min_age_limit'])
df['vaccine'] = df.sessions.apply(lambda x: x['vaccine'])
df['available_capacity'] = df.sessions.apply(lambda x: x['available_capacity'])
df['date'] = df.sessions.apply(lambda x: x['date'])
df = df[["date", "available_capacity", "vaccine", "min_age_limit", "pincode", "name", "state_name", "district_name", "block_name", "fee_type"]]
if final_df is not None:
final_df = pd.concat([final_df, df])
else:
final_df = deepcopy(df)
else:
st.error("No rows in the data Extracted from the API")
# else:
# st.error("Invalid response")
if (final_df is not None) and (len(final_df)):
final_df.drop_duplicates(inplace=True)
final_df.rename(columns=rename_mapping, inplace=True)
left_column_2, center_column_2, right_column_2, right_column_2a, right_column_2b = st.beta_columns(5)
with left_column_2:
valid_pincodes = list(np.unique(final_df["Pincode"].values))
pincode_inp = st.selectbox('Select Pincode', [""] + valid_pincodes)
if pincode_inp != "":
final_df = filter_column(final_df, "Pincode", pincode_inp)
with center_column_2:
valid_age = [18, 45]
age_inp = st.selectbox('Select Minimum Age', [""] + valid_age)
if age_inp != "":
final_df = filter_column(final_df, "Minimum Age Limit", age_inp)
with right_column_2:
valid_payments = ["Free", "Paid"]
pay_inp = st.selectbox('Select Free or Paid', [""] + valid_payments)
if pay_inp != "":
final_df = filter_column(final_df, "Fees", pay_inp)
with right_column_2a:
valid_capacity = ["Available"]
cap_inp = st.selectbox('Select Availablilty', [""] + valid_capacity)
if cap_inp != "":
final_df = filter_capacity(final_df, "Available Capacity", 0)
with right_column_2b:
valid_vaccines = ["COVISHIELD", "COVAXIN"]
vaccine_inp = st.selectbox('Select Vaccine', [""] + valid_vaccines)
if vaccine_inp != "":
final_df = filter_column(final_df, "Vaccine", vaccine_inp)
table = deepcopy(final_df)
table.reset_index(inplace=True, drop=True)
st.table(table)
else:
st.error("Unable to fetch data currently, please try after sometime")
# footer="""<style>
# a:link , a:visited{
# color: blue;
# background-color: transparent;
# text-decoration: underline;
# }
# a:hover, a:active {
# color: red;
# background-color: transparent;
# text-decoration: underline;
# }
# .footer {
# position: fixed;
# left: 0;
# bottom: 0;
# width: 100%;
# background-color: white;
# color: black;
# text-align: center;
# }
# </style>
# <div class="footer">
# <p>Developed with ❤ by <a style='display: block; text-align: center;' href="https://github.com/bhattbhavesh91" target="_blank">Bhavesh Bhatt</a></p>
# </div>
# """
# st.markdown(footer,unsafe_allow_html=True)
# st.markdown("_- Bhavesh Bhatt_")
pageviews=Pageviews()
pageviews.append('dummy')
pg_views = len(pageviews)
footer(pg_views)