KeshavRa commited on
Commit
b5edb7c
·
verified ·
1 Parent(s): a2f10fd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -0
app.py CHANGED
@@ -157,6 +157,159 @@ else:
157
  # Create two columns
158
  col1, col2 = st.columns([1,1])
159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  # Add buttons to each column
161
  with col1:
162
  if st.button("Previous"):
 
157
  # Create two columns
158
  col1, col2 = st.columns([1,1])
159
 
160
+ # Add buttons to each column
161
+ with col1:
162
+ if st.button("Previous"):
163
+ if st.session_state.shelter_index > 0:
164
+ st.session_state.shelter_index -= 1
165
+ st.experimental_rerun()
166
+
167
+ with col2:
168
+ if st.button("Next"):
169
+ if st.session_state.shelter_index < len(shelters) - 1:
170
+ st.session_state.shelter_index += 1
171
+ st.experimental_rerun()
172
+
173
+ import streamlit as st
174
+ import json
175
+ import pandas as pd
176
+ import requests
177
+ import os
178
+ import math
179
+
180
+ def get_zip_codes(city, state):
181
+ url = f'http://api.zippopotam.us/us/{state}/{city}'
182
+ response = requests.get(url)
183
+ if response.status_code == 200:
184
+ data = response.json()
185
+ return [place['post code'] for place in data['places']]
186
+ else:
187
+ return []
188
+
189
+ def get_coordinates(address: str, api_key: str) -> list:
190
+ """
191
+ Get the coordinates (latitude and longitude) of an address using the OpenWeather Geocoding API.
192
+
193
+ Parameters:
194
+ zipcode (str): The zipcode to geocode.
195
+ api_key (str): Your OpenWeather API key.
196
+
197
+ Returns:
198
+ list: A list containing the latitude and longitude of the address.
199
+ """
200
+
201
+ def haversine(lat1, lon1, lat2, lon2):
202
+ R = 6371 # Earth radius in kilometers. Use 3956 for miles.
203
+ dlat = math.radians(lat2 - lat1)
204
+ dlon = math.radians(lon2 - lon1)
205
+ a = math.sin(dlat / 2) ** 2 + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(dlon / 2) ** 2
206
+ c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
207
+ distance = R * c
208
+ return distance
209
+
210
+ def validate_zipcode(zipcode):
211
+ try:
212
+ if len(zipcode) == 5 and zipcode.isdigit():
213
+ return True
214
+ else:
215
+ return False
216
+ except:
217
+ return False
218
+
219
+ # Function to handle form submission
220
+ @st.experimental_dialog("Fill out the form")
221
+ def form_dialog():
222
+ city_zipcodes = {
223
+ "San Francisco": get_zip_codes("San Francisco", "CA"),
224
+ "Oakland": get_zip_codes("Oakland", "CA"),
225
+ "Berkeley": get_zip_codes("Berkeley", "CA")
226
+ }
227
+ print(city_zipcodes)
228
+
229
+ city = st.selectbox("City", list(city_zipcodes.keys()))
230
+ zipcode = st.selectbox("Zipcode", ['Unsure'] + city_zipcodes[city])
231
+
232
+ sex = st.radio("Sex", ["Male", "Female", "Other"])
233
+ lgbtq = st.radio("Do you identify as LGBTQ+ (some shelters serve this community specifically)", ["No", "Yes"])
234
+ domestic_violence = st.radio("Have you experienced domestic violence (some shelters serve these individuals specifically", ["No", "Yes"])
235
+
236
+ urgency = st.radio("How quickly do you need help?", ("Today", "In the next few days", "In a week or more"))
237
+ duration = st.radio("How long do you need a place to stay?", ("Overnight", "A month of less", "A couple of months", "A year or more"))
238
+ needs = st.text_area("Optional - Needs (tell us what you need and how we can help)", value="")
239
+
240
+ if st.button("Submit"):
241
+ data = {
242
+ "City": city,
243
+ "Zip Code": zipcode,
244
+ "Sex": sex,
245
+ "LGBTQ": lgbtq,
246
+ "Domestic Violence": domestic_violence,
247
+ "Urgency": urgency,
248
+ "Duration": duration,
249
+ "Needs": needs
250
+ }
251
+
252
+ with open('data.json', 'w') as f:
253
+ json.dump(data, f)
254
+
255
+ st.session_state.form_submitted = True
256
+ st.session_state.data = data
257
+ st.rerun()
258
+
259
+ # Initialize session state
260
+ if 'form_submitted' not in st.session_state:
261
+ st.session_state.form_submitted = False
262
+
263
+ if 'shelter_index' not in st.session_state:
264
+ st.session_state.shelter_index = 0
265
+
266
+ # Page config
267
+ st.set_page_config(
268
+ page_title="ShelterSearch",
269
+ layout="wide",
270
+ )
271
+
272
+ st.title("ShelterSearch")
273
+
274
+ if not st.session_state.form_submitted:
275
+ if st.button("Open Form"):
276
+ form_dialog()
277
+ else:
278
+ with open('data.json', 'r') as f:
279
+ data = json.load(f)
280
+ st.json(data)
281
+
282
+ shelters = pd.read_csv("database.csv")
283
+
284
+ # filter city
285
+ shelters = shelters[(shelters['City'] == data['City'])]
286
+
287
+ # filter sex
288
+ shelters = shelters[(shelters['Sex'] == data['Sex']) | (shelters['Sex'] == 'All')]
289
+
290
+ # filter lgbtq
291
+ if data['LGBTQ'] == 'No':
292
+ shelters = shelters[(shelters['LGBTQ'] == "No")]
293
+
294
+ # filter domestic violence
295
+ if data['Domestic Violence'] == "No":
296
+ shelters = shelters[(shelters['Domestic Violence'] == "No")]
297
+
298
+ st.table(shelters)
299
+
300
+ shelters = [
301
+ {"title": "Shelter 1", "description": "This is the 1st shelter",},
302
+ {"title": "Shelter 2", "description": "This is the 2nd shelter.",},
303
+ {"title": "Shelter 3", "description": "This is the 3rd shelter.",}
304
+ ]
305
+
306
+ # Display the current shelter information
307
+ shelter = shelters[st.session_state.shelter_index]
308
+ st.write(shelter["description"])
309
+
310
+ # Create two columns
311
+ col1, col2 = st.columns([1,1])
312
+
313
  # Add buttons to each column
314
  with col1:
315
  if st.button("Previous"):