import streamlit as st import json import pandas as pd import requests def get_coordinates(address: str, api_key: str) -> list: """ Get the coordinates (latitude and longitude) of an address using the OpenCage Data Geocoding API. Parameters: address (str): The address to geocode. api_key (str): Your OpenCage Data API key. Returns: list: A list containing the latitude and longitude of the address. """ # Encode the address for the URL encoded_address = requests.utils.quote(address) # Construct the API URL url = f"https://api.opencagedata.com/geocode/v1/json?q={encoded_address}&key={api_key}" # Make the API request response = requests.get(url) if response.status_code == 200: # Parse the JSON response data = response.json() # Check if there are any results if data['results']: # Extract the latitude and longitude from the first result latitude = data['results'][0]['geometry']['lat'] longitude = data['results'][0]['geometry']['lng'] return [latitude, longitude] else: raise ValueError("No results found for the given address.") else: raise Exception(f"API request failed with status code {response.status_code}") def haversine(lat1, lon1, lat2, lon2): R = 6371 # Earth radius in kilometers. Use 3956 for miles. dlat = math.radians(lat2 - lat1) dlon = math.radians(lon2 - lon1) a = math.sin(dlat / 2) ** 2 + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(dlon / 2) ** 2 c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) distance = R * c return distance def validate_zipcode(zipcode): try: if len(zipcode) == 5 and zipcode.isdigit(): return True else: return False except: return False # Function to handle form submission @st.experimental_dialog("Fill out the form") def form_dialog(): city = st.radio("City", ("San Fransisco", "Oakland", "Berkeley")) sex = st.radio("Sex", ("Male", "Female", "Other")) lgbtq = st.radio("Do you identify as LGBTQ+ (some shelters serve specifically this community", ("Yes", "No")) domestic_violence = st.radio("Are you experiencing domestic violence (some shelters serve these individuals specifically", ("Yes", "No")) zipcode = st.text_input("Zipcode (Optional)", value="") urgency = st.radio("How quickly do you need help?", ("Today", "In the next few days", "In a week or more")) 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")) needs = st.text_area("Needs (Optional - tell us what you need and how we can help)", value="") if st.button("Submit"): if zipcode and not validate_zipcode(zipcode): st.error("Please enter a valid 5-digit zipcode.") else: data = { "City": city, "Sex": sex, "LGBTQ": lgbtq, "Domestic Violence": domestic_violence, "Zipcode": zipcode, "Urgency": urgency, "Duration": duration, "Needs": needs } with open('data.json', 'w') as f: json.dump(data, f) st.session_state.form_submitted = True st.session_state.data = data st.rerun() # Initialize session state if 'form_submitted' not in st.session_state: st.session_state.form_submitted = False if 'shelter_index' not in st.session_state: st.session_state.shelter_index = 0 shelters_def = [ {"title": "Shelter 1", "description": "This is the 1st shelter",}, {"title": "Shelter 2", "description": "This is the 2nd shelter.",}, {"title": "Shelter 3", "description": "This is the 3rd shelter.",} ] # Page config st.set_page_config( page_title="ShelterSearch", layout="wide", ) st.title("ShelterSearch") if not st.session_state.form_submitted: if st.button("Open Form"): form_dialog() else: with open('data.json', 'r') as f: data = json.load(f) # Display st.json(data) shelters = pd.read_csv("database.csv") # filter sex shelters = shelters[(shelters['Sex'] == data['Sex']) | (shelters['Sex'] == 'All')] # filter city shelters = shelters[(shelters['City'] == data['City'])] # filter lgbtq if data['LGBTQ'] == 'No': shelters = shelters[(shelters['LGBTQ'] == "No")] # filter domestic violence if data['Domestic Violence'] == 'No': shelters = shelters[(shelters['Domestic Violence'] == "No")] # calculate distances between zipcodes api_key = os.environ['OpenCage_API_KEY'] shelters_coordinates = shelters.apply(lambda row: get_coordinates(row['Zip Code'], api_key), axis=1).tolist() user_coordinates = get_coordinates(data['Zip Code']) distances = [] for coordinates in shelters_coordinates: distances.append(haversine(coordinates[0], coordinates[1], user_coordinates[0], user_coordinates[1])) shelters['zipcode_score'] = distances # urgency # duration # services st.table(shelters) # Display the current shelter information shelter = shelters_def[st.session_state.shelter_index] st.write(shelter["description"]) # Create two columns col1, col2 = st.columns([1,1]) # Add buttons to each column with col1: if st.button("Previous"): if st.session_state.shelter_index > 0: st.session_state.shelter_index -= 1 st.experimental_rerun() with col2: if st.button("Next"): if st.session_state.shelter_index < len(shelters_def) - 1: st.session_state.shelter_index += 1 st.experimental_rerun()