Spaces:
Sleeping
Sleeping
File size: 4,724 Bytes
34dc5ef f58ae1f 34dc5ef 730eec8 34dc5ef 730eec8 9d09d99 730eec8 7e14a30 730eec8 8439960 f58ae1f e5f48b3 e0c0f6b 9135b01 6fe8e54 9135b01 4c9be4b 730eec8 9d09d99 730eec8 23ac442 9d09d99 255eb64 3ca51c4 726137a f58ae1f b268c6a e07ecfe b268c6a f58ae1f b268c6a 167abad f58ae1f 236b2b2 f58ae1f 10ec335 f58ae1f 10ec335 f58ae1f 236b2b2 7e14a30 236b2b2 167abad 236b2b2 | 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 | 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"])
|