Selection Widgets with Streamlit

Radio, Selectbox and Select Slider

These widgets are used when you want users to make just one selection out of several listed.

radio_selection = st.radio(“Which of these insects is not a pest?”, [“Locust”, “Lacewing”, “Leafhopper”]) if st.button(“Submit your answer”, key = “Radio”): if radio_selection == “Lacewing”: st.write(“You are correct!”) else: st.write(“You are wrong. Try again.”)

selectbox_selection = st.selectbox(“Which of these insects is a beetle?”, [“Leafhopper”, “Ladybird”, “Locust”, “Lacewing”]) if st.button(“Submit your answer”, key = “Selectbox”): if selectbox_selection == “Ladybird”: st.write(“You are correct!”) else: st.write(“You are wrong. Try again.”)

select_slider_selection = st.select_slider(“Which of these insects is nocturnal?”, [“Leafhopper”, “Ladybird”, “Locust”, “Lacewing”]) if st.button(“Submit your answer”, key = “Select Slider”): if select_slider_selection == “Lacewing”: st.write(“You are correct!”) else: st.write(“You are wrong. Try again.”)

Notice that for the button widget, an additional parameter key has been added. If you don’t add it, the label (text immediately following the opening paranthesis) is used as the key for that widget. Each instance of the widget needs to have a unique key, thus if you have the same text for both, Streamlit will throw an error stating that the widget key is duplicated.

st.button(“Mine”) st.button(“Mine”)

Adding the key parameter and assigning a unique value for each instance resolves this issue. Multiselect

If you want your users to select more than one option, use the st.multiselect widget.

multiselect_selection = st.multiselect(“Which of these are pests?”, [“Lacewing”, “Locust”, “Leafhopper”, “Ladybird”]) if st.button(“Submit your answer”, key = “Multiselect”): if “Lacewing” in multiselect_selection or “Ladybird” in multiselect_selection: st.write(“You are wrong. Try again.”) else: st.write(“You are correct!”)

The following figure demonstrates the checkbox, button and selection widgets. Slider

We previously saw the st.select_slider widget. The difference between that widget and the st.slider widget is, the former can take on any data type, whereas the latter can take on only numeric or datetime data types.

The st.slider widget allows you to have numerical values that are either integers or floats, depending on how you specify the expected inputs.

To have integer values, the parameters defining the expected values are listed as whole numbers. For floats, the parameters are listed as numbers with decimal values.

You can also specify a default value that is displayed the moment your web app initially runs.

slider_integer_selection = st.slider(“Select a number”, key = “Integer”, min_value = 0, max_value = 100, value = 25) st.write(slider_integer_ selection, type(slider_integer_selection)) slider_float_selection = st.slider(“Select a number”, key = “Float”, min_value = 0.0, max_value = 100.0, value = 25.0) st.write(slider_float_selection, type(slider_float_selection))

Similar to the st.button widget, we have added an additional key parameter that uniquely identifies each widget, since the labels for both are the same.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy