Hi, I am new to Streamlit. I want to make a multiple-choice user input that looks like radio-buttons. But I want to select a maximum of 3 options out of 4 options.
I have tried with the dropdown feature of multiselect
.
option = streamlit.multiselect('Select three known variables:', ['initial velocity (u)', 'final velocity (v)', 'acceleration (a)', 'time (t)'])
It works. But I don’t like the visuals of it. I prefer the looks of checkboxes like the `radio’ buttons:
option = streamlit.radio('Select three known variables:', ['initial velocity (u)', 'final velocity (v)', 'acceleration (a)', 'time (t)'])
But using radio
, I can’t select more than one option. So, how can I edit it such that I can display it as checkboxes and select maximum of 3 options?
I haven’t found any streamlit
function that fulfills my need properly. So, I am using the function streamlit.checkbox
as anilewe suggested and adding some extra conditional statements such that the user must select any 3 checkboxes.
Here’s my code:
import streamlit as st
st.write('Select three known variables:')
option_s = st.checkbox('displacement (s)')
option_u = st.checkbox('initial velocity (u)')
option_v = st.checkbox('final velocity (v)')
option_a = st.checkbox('acceleration (a)')
option_t = st.checkbox('time (t)')
known_variables = option_s + option_u + option_v + option_a + option_t
if known_variables <3:
st.write('You have to select minimum 3 variables.')
elif known_variables == 3:
st.write('Now put the values of your selected variables in SI units.')
elif known_variables >3:
st.write('You can select maximum 3 variables.')
Still, I am wishing to get a function that looks like checkbox
or radio
buttons but with a built-in feature to limit a specific number of selections. In a word, I think my desired function can be called “multiselect-radio”. Thus I won’t have put the additional conditions on limiting selection by myself.