Created
August 27, 2021 12:49
-
-
Save Outsiders17711/01ab78f3d3ef623823f5fc5a78b52d9b to your computer and use it in GitHub Desktop.
A simple Streamlit form that provides three sets options using selectboxes limited to one choice in total.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit as st | |
blank_choice = "<----->" | |
options = [ | |
[ | |
blank_choice, | |
"Option 1", | |
"Option 2", | |
"Option 3", | |
], | |
[ | |
blank_choice, | |
"Option 4", | |
"Option 5", | |
"Option 6", | |
], | |
[ | |
blank_choice, | |
"Option 7", | |
"Option 8", | |
"Option 9", | |
], | |
] | |
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} >>.**") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment