Hi.
I want to build three selectboxes in the sidebar.
I want the user to be allowed to choose only one item from the whole three selectboxes.
How can i do that?
Hi.
I want to build three selectboxes in the sidebar.
I want the user to be allowed to choose only one item from the whole three selectboxes.
How can i do that?
Hello @AnonC0DEr, Welcome to the Streamlit community.
I have written some code similar to what youโre trying to achieve. I have extracted the following snippet to answer your question:
import streamlit as st
import time
form = st.sidebar.form("form", clear_on_submit=True)
with form:
form.write("**`Choose a box :`**")
tick_boxes = form.columns(3)
col_labels = [col.write(f"`Option {idx + 1}`") for idx, col in enumerate(tick_boxes)]
values = [
col.checkbox(f"{idx + 1}", key=f"{idx+1}", value=False)
for idx, col in enumerate(tick_boxes)
]
form.write("")
submit = form.form_submit_button("Submit")
st.info("**Choose an option using the boxes in the sidebar.**")
if submit:
list_values = [int(i) for i in values]
if sum(list_values) == 0:
st.error("**Choose an option!**")
time.sleep(1)
st.experimental_rerun()
elif sum(list_values) > 1:
st.error("**You can only tick one box!**")
time.sleep(1)
st.experimental_rerun()
elif sum(list_values) == 1:
# --->
# the 'value' variable is the one you then use in if-statements:
# >>> if value == 1:
value = list_values.index(1) + 1
# --->
st.success(f"**Player choose option << {value} >>.**")
The value
variable is the one you then use in if-statements
such as:
if value == 1:
pass
You can find the snippet at this gist.
Cheers.
Thanks for your answer.
Dose it work for selectbox method too?
I have a lot of items, So I should use selectbox method.
Again thank you for your answer.
Hello @AnonC0DEr, I misunderstood your question actually. I somehow read โcheckboxโ instead of โselectboxโ .
Hereโs the same code adapted to work with selectboxes:
form = st.sidebar.form("form", clear_on_submit=True)
num_boxes = 3
with form:
form.write("**`Dropdowns :`**")
values = [
st.selectbox(f"Options Set {i+1} :", options[i], index=0, key=f"set_{i}")
for i in range(num_boxes)
]
# st.write(values, values.count(blank_choice))
form.write("")
submit = form.form_submit_button("Submit")
st.info("**Choose an option using the dropdowns in the sidebar.**")
if submit:
if values.count(blank_choice) == num_boxes:
st.error("**Choose an option!**")
elif values.count(blank_choice) < (num_boxes - 1):
st.error("**You can only tick one box!**")
elif values.count(blank_choice) == (num_boxes - 1):
# the 'final_choice' variable is the one you then use in subsequent code:
final_choice = [i for i in values if i != blank_choice][-1]
st.success(f"**Player choose option << {final_choice} >>.**")
You can find the full code at this gist .
Cheers.
Thanks, It works very well now.
Cheers.
Ah. Nice. You could mark that as an answer then. Makes it easier for others locate.
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.
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.
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.
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.