I am trying to create a form where the user can select certain options with checkboxes. I want to then use these choices further on in my code.
The problem is that for some reason, nothing within the “if submit_button” loop is being executed. why?
with st.form(key="source_selection"): # Create a form
st.write("Please choose the options you want to use.")
for option in options:
st.checkbox(option, key='checkbox_' + option)
submit_button = st.form_submit_button(label="Submit")
if submit_button: # Check if submit button is clicked
print('SUBMITTED')
selected_options = [option for option in options if st.session_state['checkbox_' + option]]
st.write("Post-submit selected options:")
st.write(selected_options)
if selected_options:
# update session state with selected options
st.session_state.app_state.selected_options = selected_options
st.write(f"Selected Options: {', '.join(selected_options)}")
send_selected_sources_to_backend(selected_options)
else:
st.warning("Please select at least one option.")
It seems like it works fine to me – here’s my attempt to make a complete script
import streamlit as st
options = ["option1", "option2", "option3", "option4", "option5"]
if "app_state" not in st.session_state:
st.session_state.app_state = {}
st.session_state.app_state["selected_options"] = []
def send_selected_sources_to_backend(selected_options):
st.info(f"Sending selected options to backend: {selected_options}")
with st.form(key="source_selection"): # Create a form
st.write("Please choose the options you want to use.")
for option in options:
st.checkbox(option, key="checkbox_" + option)
submit_button = st.form_submit_button(label="Submit")
if submit_button: # Check if submit button is clicked
print("SUBMITTED")
selected_options = [
option for option in options if st.session_state["checkbox_" + option]
]
st.write("Post-submit selected options:")
st.write(selected_options)
if selected_options:
# update session state with selected options
st.session_state.app_state["selected_options"] = selected_options
st.write(f"Selected Options: {', '.join(selected_options)}")
send_selected_sources_to_backend(selected_options)
else:
st.warning("Please select at least one option.")
st.write("selected:")
st.write(st.session_state.app_state["selected_options"])
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.