Tantalos / utils /allergy_input.py
DrDomedag's picture
Minor UI cleanup.
d131310
import streamlit as st
from utils.allergens import *
from utils.variables import *
st.markdown(
"""
<style>
.reportview-container {
background: 35bafc
}
.sidebar .sidebar-content {
background: 35bafc
}
</style>
""",
unsafe_allow_html=True
)
def create_allergy_input(user_id):
import streamlit as st
#details_link = f"[Go to this user's event participation page {user_id}](?page=event%5Fparticipation&user_id={user_id})"
#details_link = f"[Go to this user's event participation page {user_id}](?page=event%5Fparticipation)"
#st.markdown(details_link, unsafe_allow_html=True)
# Button to display the selected options
#if st.button("Show Selections"):
#for item in allergens:
# Access the radio button and checkbox values using session_state
#if st.session_state[f"{user_id}_{item}_radio"] != "None":
# selected_sensitivity = st.session_state[f"{user_id}_{item}_radio"]
# can_eat_if_cooked = st.session_state[f"{user_id}_{item}_checkbox"]
# Display the selected options for each item
#st.write(f"**{item.capitalize()}**")
#st.write(f"Sensitivity Level: {selected_sensitivity}")
#st.write(f"Can Eat If Cooked: {'Yes' if can_eat_if_cooked else 'No'}")
with st.expander("Show Selections"):
for i in user_list[user_id].keys():
if user_list[user_id][i][0] > 0:
st.write(f"**{i}**")
st.write(f"Sensitivity Level: {sensitivities[user_list[user_id][i][0]]}")
st.write(f"Can Eat If Cooked: {'Yes' if user_list[user_id][i][1] else 'No'}")
#st.write(f"Can Eat Traces: {'Yes' if user_list[user_id][i][2] else 'No'}")
if st.button("Done"):
st.switch_page(f"pages/user_{user_id}_event_participation.py")
#st.write("Your allergies:")
#for i in user_list[user_id].keys():
# if user_list[user_id][i][0] > 0:
# st.write(f"{i}, allergy degree: {user_list[user_id][i][0]}, can eat if cooked: {user_list[user_id][i][1]}")
# Text input for the search query
query = st.text_input("Search for an allergy:")
# Filter the list based on the search query
if query:
filtered_items = [allergen for allergen in allergens if query.lower() in allergen.lower()]
else:
filtered_items = allergens
# Display the filtered list
st.write("Results:") #Bara visa denna om query != ""?
# Initialize session state to store titles and selections for each expander
if f"{user_id}_expander_titles" not in st.session_state:
st.session_state[f"{user_id}_expander_titles"] = {item: item.capitalize() for item in filtered_items}
if "{user_id}_expander_selections" not in st.session_state:
st.session_state[f"{user_id}_expander_selections"] = {}
if "{user_id}_expanded_state" not in st.session_state:
st.session_state[f"{user_id}_expanded_state"] = {item: True for item in filtered_items} # Keep all expanders open initially
# Function to update expander title based on selection
def update_title(item):
selections = st.session_state.get(f"{user_id}_{item}_radio", "None")
selection = "None"
if selections == "Extremely sensitive - " + sensitivity_options["Extremely sensitive"]:
selection = "Extremely sensitive❗❗"
user_list[user_id][item] = (3, st.session_state[f"{user_id}_{item}_checkbox"])
elif selections == "Sensitive - " + sensitivity_options["Sensitive"]:
selection = "Sensitive❗"
user_list[user_id][item] = (2, st.session_state[f"{user_id}_{item}_checkbox"])
elif selections == "Slightly sensitive - " + sensitivity_options["Slightly sensitive"]:
selection = "Slightly sensitive⚠️"
user_list[user_id][item] = (1, st.session_state[f"{user_id}_{item}_checkbox"])
else:
del user_list[user_id][item]
if selection != "None":
st.session_state[f"{user_id}_expander_titles"][item] = f"{item.capitalize()} - {selection}"
else:
st.session_state[f"{user_id}_expander_titles"][item] = item.capitalize() # Reset to default if "None" is selected
"""option = selection.split(" - ")
selection = option[1]"""
# Keep the expander open
st.session_state[f"{user_id}_expanded_state"][item] = True
def update_cook(item):
current_level, current_cook = user_list[user_id][item]
user_list[user_id][item] = (current_level, st.session_state[f"{user_id}_{item}_checkbox"])
#def update_traces(item):
# current_level, current_cook, current_traces = user_list[user_id][item]
# user_list[user_id][item] = (current_level, current_cook, st.session_state[f"{user_id}_{item}_traces"])
# Loop through each item
for item in filtered_items:
# Check if the item is in the user's allergen list
# if item in user_allergen_list:
# key=f"{item}_radio"
title = st.session_state[f"{user_id}_expander_titles"][item]
#selection = st.session_state.get(f"{user_id}_{item}_radio", "None")
#st.write(selection)
#selection = user_list[user_id][item][0]
with st.expander(title):
# Radio buttons for sensitivity level with a "None" option
sensitivity = st.radio(
f"Sensitivity level for {item.capitalize()}",
options=["None"] + [f"{level} - {description}" for level, description in sensitivity_options.items()],
key=f"{user_id}_{item}_radio",
on_change=lambda item=item: update_title(item) # Call update_title with the item argument
)
# Store the current selection in session_state for later access
st.session_state[f"{user_id}_expander_selections"][item] = sensitivity
# Checkbox to indicate if they can eat the ingredient if it's cooked
can_eat_if_cooked = st.checkbox(
"Can eat the ingredient if it is cooked",
key=f"{user_id}_{item}_checkbox",
on_change=lambda item=item: update_cook(item)
)
# can_eat_traces = st.checkbox(
# "Can eat traces of the ingredient",
# key=f"{user_id}_{item}_traces",
# on_change=lambda item=item: update_traces(item)
#)
"""else:
with st.expander(item.capitalize()):
# Radio buttons for sensitivity level with a "None" option
sensitivity = st.radio(
f"Sensitivity level for {item.capitalize()}",
options=["None"] + [f"{level} - {description}" for level, description in sensitivity_options.items()],
key=f"{item}_radio"
)
# Checkbox to indicate if they can eat the ingredient if it's cooked
can_eat_if_cooked = st.checkbox(
"Can eat the ingredient if it is cooked",
key=f"{item}_checkbox"
)
# If a sensitivity other than "None" is selected, mark the item for addition to the allergen list
if sensitivity != "None":
to_add.append(item)"""
# Update user_allergen_list based on selections
# user_allergen_list = [item for item in user_allergen_list if item not in to_remove] + to_add
# Display updated allergen list
# st.write("Updated allergen list:", to_remove)