Skip to content

Instantly share code, notes, and snippets.

@Outsiders17711
Created August 27, 2021 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Outsiders17711/01ab78f3d3ef623823f5fc5a78b52d9b to your computer and use it in GitHub Desktop.
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.
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