Spaces:
Sleeping
Sleeping
import streamlit as st | |
import json | |
import pandas as pd | |
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 | |
def form_dialog(): | |
sex = st.radio("Sex", ("Male", "Female", "Other")) | |
city = st.radio("City", ("Oakland", "Berkeley", "SF")) | |
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?", ("A year or more", "A couple of months", "A month or less")) | |
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 = { | |
"Sex": sex, | |
"City": city, | |
"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() | |
shelters = pd.read_csv("Database.csv") | |
filtered_shelters = shelters[(shelters['Sex'] == data['Sex']) & (shelters['City'] == data['City']) & (shelters['LGBTQ'] == data['LGBTQ']) & (shelters['Domestic Violence'] == data['Domestic Violence'])] | |
st.table(filtered_shelters) | |
# 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 = [ | |
{"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) | |
# Display the current shelter information | |
shelter = shelters[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) - 1: | |
st.session_state.shelter_index += 1 | |
st.experimental_rerun() | |