-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschool_lister.py
189 lines (139 loc) · 6.03 KB
/
school_lister.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
import json
import streamlit as st
import pandas as pd
import os
import postcodes_io_api
from timezonefinder import TimezoneFinder
from geopy.geocoders import Nominatim
st.title("List of schools")
countries = ["Australia", "New Zealand", "United Kingdom", "Wales"]
api = postcodes_io_api.Api(debug_http=False)
tf = TimezoneFinder()
geoLoc = Nominatim(user_agent="GetLoc")
@st.cache
def get_lat_long_from_postcode(postcode):
postcode_info = api.get_postcode(postcode)
lat = postcode_info["result"]["latitude"]
long = postcode_info["result"]['longitude']
return lat, long
@st.cache
def get_address_from_location(lat, long):
try:
locname = geoLoc.reverse(f"{lat}, {long}")
return locname.address
except Exception as e:
print(e)
return "No address found"
@st.cache
def get_timezone(lat, long):
try:
return tf.timezone_at(lng=float(long), lat=float(lat))
except:
return "No timezone found"
@st.cache
def get_schools_AU():
with st.spinner('Waiting...'):
if os.path.exists("schools_AU.pkl"):
return pd.read_pickle("schools_AU.pkl")
data = pd.read_excel("schools.xlsx", sheet_name='Australia')
data.rename(columns = {'Latitude':'latitude', 'Longitude':'longitude'}, inplace = True)
data['Address'] = data.apply(lambda row: get_address_from_location(row['latitude'], row['longitude']), axis=1)
data['Timezone'] = data.apply(lambda row: get_timezone(row['latitude'], row['longitude']), axis=1)
data.to_pickle("schools_AU.pkl")
return data
@st.cache
def get_schools_NZ():
with st.spinner('Waiting...'):
if os.path.exists("schools_NZ.pkl"):
return pd.read_pickle("schools_NZ.pkl")
data = pd.read_excel("schools.xlsx", sheet_name='New Zealand')
data.rename(columns = {'Latitude':'latitude', 'Longitude':'longitude', "Org_Name": "School Name", "Add1_City": "State", "Add2_Suburb": "Suburb"}, inplace = True)
data['Address'] = data.apply(lambda row: get_address_from_location(row['latitude'], row['longitude']), axis=1)
data['Timezone'] = data.apply(lambda row: get_timezone(row['latitude'], row['longitude']), axis=1)
data.to_pickle("schools_NZ.pkl")
return data
@st.cache
def get_schools_UK():
with st.spinner('Waiting...'):
if os.path.exists("schools_UK.pkl"):
data = pd.read_pickle("schools_UK.pkl")
return data
data = pd.read_excel("schools.xlsx", sheet_name='United Kingdom', )
data.rename(columns = { "Establishment name": "School Name", "Region": "State", "Local authority (name)": "Suburb"}, inplace = True)
latitudes = []
longitudes = []
addresses = []
timezones = []
def chunk(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
for i, postcode_chunk in enumerate(chunk(data['Postcode'], 50)):
print(i*50)
postcodes = api.get_bulk_postcodes(list(postcode_chunk))
for postcode in postcodes['result']:
if not postcode['result']:
latitudes.append(0)
longitudes.append(0)
addresses.append("No address found")
timezones.append("No timezone found")
else:
latitudes.append(postcode["result"]["latitude"])
longitudes.append(postcode["result"]["longitude"])
addresses.append(get_address_from_location(postcode["result"]["latitude"], postcode["result"]["longitude"]))
timezones.append(get_timezone(postcode["result"]["latitude"], postcode["result"]["longitude"]))
data['latitude'] = latitudes
data['longitude'] = longitudes
data['Address'] = addresses
data['Timezone'] = timezones
data.to_pickle("schools_UK.pkl")
return data
@st.cache
def get_schools_Wales():
with st.spinner('Waiting...'):
if os.path.exists("schools_Wales.pkl"):
return pd.read_pickle("schools_Wales.pkl")
data = pd.read_excel("schools.xlsx", sheet_name='Wales', )
data.rename(columns = { "School Name": "School Name", "Local Authority": "Suburb"}, inplace = True)
data.to_pickle("schools_Wales.pkl")
return data
country = st.sidebar.selectbox("Countries", sorted(countries))
if country == "Australia":
schools = get_schools_AU()
state_label = 'State'
elif country == "New Zealand":
schools = get_schools_NZ()
state_label = 'City'
elif country == "United Kingdom":
schools = get_schools_UK()
state_label = "Region"
elif country == "Wales":
schools = get_schools_Wales()
state_label = "Local authority"
st.markdown(f"There are *{len(schools)}* in **{country}** schools to choose from.")
school = st.sidebar.selectbox("Schools", options = schools[["School Name"]])
schools_by_name = schools[schools["School Name"] == school]
states = schools_by_name['State']
state = st.sidebar.selectbox(state_label, options = list(states), index=0)
suburbs = schools_by_name[schools_by_name['State'] == state]
suburb = st.sidebar.selectbox("Suburb", options = list(suburbs["Suburb"]))
result = suburbs[suburbs["Suburb"] == suburb]
if country == "United Kingdom":
try:
result['Address'] = get_address_from_location(
float(result["latitude"].astype(float)), float(result["longitude"].astype(float)))
except:
result['Address'] = "No address found"
elif country == 'Wales':
location = get_lat_long_from_postcode(result["Postcode"].values[0])
if location:
result['latitude'] = location[0]
result['longitude'] = location[1]
result['Address'] = get_address_from_location(location[0], location[1])
result['timezone'] = tf.timezone_at(lng=location[1], lat=float(location[0]))
# result
# st.markdown(result.Address.values[0])
st.markdown(f"**{result['School Name'].values[0]}**")
st.json(result.to_json(orient="records"))
try:
st.map(result)
except:
pass