Streamlit Forms
This is a container that allows you to visually group several widgets together. It has a submit button, which, when pressed, will have all values from the widgets sent to Streamlit in a batch.
The following example shows two widgets in a form, along with the submitted values within the same form. Outside the form we have the checkbox widget.
with st.form(“Sample Form”): st.write(“Gathering insect information inside the form”) select_slider_value = st.select_slider(“Select an insect”, [“Lacewing”, “Ladybird”, “Leafhopper”, “Locust”]) number_input_value = st.number_input(“How many insects have we studied?”, min_value = 0)
submitted = st.form_submit_button("Submit")
if submitted:
st.write("You selected the {}. We studied {} insects. This information is within the form.".format(select_slider_value, number_input_value))
checkbox_value = st.checkbox(“Click. Or not. The choice is yours!”) st.write(“You clicked the checkbox:”, checkbox_value, “This information is outside the form.”) st.write(“These values, {} and {}, that were set inside the form are accessible outside the form”.format(select_slider_value, number_input_value))
To submit the items within the form, use the form_submit_button widget. You cannot use that widget outside a form. You cannot use the regular button widget within a form.
The values submitted within the form can be accessed outside the form, as demonstrated in the figure above.
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy