forms-demo / app.py
eagle0504's picture
app updated
7e14a30
import streamlit as st
import pandas as pd
import json
# Function to validate zipcode
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():
gender = st.radio("Gender", ("Male", "Female", "Other"))
city = st.radio("City", ("Oakland", "Berkeley", "SF"))
zipcode = st.text_input("Zipcode (Optional)", value="")
urgency = st.radio("Urgency", ("Immediate", "High", "Moderate", "General Inquiry"))
duration = st.radio("Duration", ("Long Term", "Transitional", "Temporary"))
needs = st.text_area("Needs (describe needed services + ideal qualities of shelter)")
if st.button("Submit"):
if zipcode and not validate_zipcode(zipcode):
st.error("Please enter a valid 5-digit zipcode.")
else:
data = {
"Gender": gender,
"City": city,
"Zipcode": zipcode if zipcode else "N/A",
"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()
# Function to reset session state
def reset_session_state():
st.session_state.form_submitted = False
st.session_state.shelter_index = 0
st.experimental_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
# Load the master database
master_database = pd.read_csv("master_database.csv")
# Page config
st.set_page_config(
page_title="Ex-stream-ly Cool App",
page_icon="🧊",
layout="wide",
initial_sidebar_state="expanded",
menu_items={
'Get Help': 'https://www.extremelycoolapp.com/help',
'Report a bug': "https://www.extremelycoolapp.com/bug",
'About': "# This is a header. This is an *extremely* cool app!"
}
)
# Adding a title with an emoji
st.title("πŸ“ Information Form")
if not st.session_state.form_submitted:
if st.button("Open Form"):
form_dialog()
else:
st.success("Form submitted successfully!")
with open('data.json', 'r') as f:
data = json.load(f)
# Display
with st.expander("Expand/collapse:"):
st.success("Client info collected! Please see the following.")
st.json(data)
# Filter the master database
filtered_data = master_database[
(master_database['gender'] == data['Gender']) &
(master_database['city'] == data['City']) &
(master_database['duration'] == data['Duration']) &
(master_database['needs'].str.contains(data['Needs'], case=False))
]
# Create the client database
client_database = filtered_data.to_dict(orient='records')
# Debug print to see the structure of client_database
with st.expander("Expand/collapse:"):
st.write("Filtered client database:", client_database)
# Convert client_database to shelters format
shelters = [
{
"title": f"Shelter {i+1}",
"description": f"This is shelter {i+1}.",
"header": "Details",
"image_url": "https://static.streamlit.io/examples/cat.jpg" if i % 2 == 0 else "https://static.streamlit.io/examples/dog.jpg",
"info": row
}
for i, row in enumerate(client_database)
]
if len(shelters) == 0:
st.warning("No shelters found matching your criteria.")
else:
# Create two columns
col1, col2 = st.columns([1, 2])
with col1:
if st.button("Previous", key="previous"):
if st.session_state.shelter_index > 0:
st.session_state.shelter_index -= 1
st.experimental_rerun()
if st.button("Next", key="next"):
if st.session_state.shelter_index < len(shelters) - 1:
st.session_state.shelter_index += 1
st.experimental_rerun()
if st.button("Resubmit", key="resubmit"):
reset_session_state()
with col2:
# Display the current shelter information
shelter = shelters[st.session_state.shelter_index]
st.write(shelter["description"])
st.header(shelter["header"])
st.image(shelter["image_url"])
st.json(shelter["info"])