I have a multiselectbox in the sidebar of my streamlit app. Without loss of generality:
- I have 10 options to put in this multiselectbox (these are the numbers 1...10)
- The user may not select both
1
and2
simultaneously
I would therefore like to remove 2
from the list of possible selections if 1
is selected, but streamlit seems to have no capability for this, so I tried to wrap it in a loop, which also fails (DuplicateWidgetID: There are multiple identical st.multiselect widgets with the same generated key.
):
options = [1,2,3,4,5,6,7,8,9,10]
space = st.sidebar.empty()
answer, _answer = [], None
while True:
if answer != _answer:
answer.append(space.multiselectbox("Pick a number",
options,
default=answer
)
)
options = [o for o in options if o not in answer]
if 1 in options:
if 2 in options: options.remove(2)
if 2 in options:
if 1 in options: options.remove(1)
_answer = answer[:]
Any thoughts on how I could achieve this?
Method 1
Here is one approach, provide a help on the widget, and just allow the user to select both 1 and 2 but then we have to filter out 2 if both are selected. Then you can just use the validated selection.
Code
Output
Hover mouse on the
?
to show the help.Method 2
Use radio button for options 1 or 2 and the rest for multiselect.
Code
Output
Method 3
When 1 is selected remove the 2 and then rerun to update the options. Likewise when 2 is selected remove the 1 and rerun to update the options.
Code
Output