Streamlit Form
Streamlit Form
A form is a container that visually groups other elements and widgets together,
and contains a Submit button.
When the submit button is pressed, all widget values inside the form will be sent to Streamlit in a batch
To add elements to a form object, we can use the “with” notation (more preferred),
or just call methods directly on the form.
By default, Streamlit apps reruns top to bottom every time a widget changes.
But we don’t want that for forms because forms usually have multiple inputs and we dont want the app to submit until the user clicks a button
That’s where st.form comes in.
It allows grouping multiple inputs.
It runs only when the submit button is clicked.
It keeps the UI clean.
Forms have a few constraints:
Every form must contain a st.form_submit_button.
we cannot use st.button and st.download_button inside a form.
Need a Streamlit developer? Click Here
Basic Syntax
lets create a basic form
import streamlit as st
with st.form("Basic Form"):
st.write("This is the inside of the form")
#Input widgets
name = st.text_input("Enter your name")
age = st.number_input("Enter your age", min_value=0, step=1)
#Submit button
submitted = st.form_submit_button("Submit")
#what happens after submitting
if submitted:
st.write(f"Hello {name}, you are {age} years old")


Form identifier
Each form needs a unique key
import streamlit as st
with st.form(key="form1"):
st.write("Inside for 1")
st.form_submit_button("submit")
with st.form(key="form2"):
st.write("Inside for 2")
st.form_submit_button("submit")
with st.form(key="form3"):
st.write("Inside for 3")
st.form_submit_button("submit")

Multiple Forms in One App
we can have several independent forms
we can use st.columns to group them. refer to this for more on st.columns.
import streamlit as st
col1, col2 = st.columns(2)
with col1:
with st.form("login form"):
user = st.text_input("Username")
password = st.text_input("Password", type="password")
login = st.form_submit_button("Login")
with col2:
with st.form("feedback form"):
feedback = st.text_area("your feedback")
rating = st.slider("Rate us", 1, 5)
send = st.form_submit_button("Send")

Form Widgets
we can use almost any input widget inside form.
st.text_input, st.number_input, st.text_area
st.selectbox, st.multiselect, st.radio
st.slider, st.date_input, st.time_input
st.checkbox
import streamlit as st
with st.form("survey_form"):
st.write("Survey Form")
name = st.text_input("Name")
gender = st.radio("Gender", ["Male", "Female", "Other"])
hobbies = st.multiselect("Hobbies", ["Sports", "Music", "Coding", "Reading"])
agree = st.checkbox("I agree to the terms")
submitted = st.form_submit_button("Submit")
if submitted:
st.write("### Survey Results")
st.write(f"👤 Name: {name}")
st.write(f"⚧ Gender: {gender}")
st.write(f"🎯 Hobbies: {', '.join(hobbies)}")
st.write("✅ Agreed" if agree else "❌ Did not agree")

with st.form("calc_form"):
num1 = st.number_input("Enter first number")
num2 = st.number_input("Enter second number")
operation = st.selectbox("Choose operation", ["Add", "Subtract", "Multiply", "Divide"])
calculate = st.form_submit_button("Calculate")
if calculate:
if operation == "Add":
result = num1 + num2
elif operation == "Subtract":
result = num1 - num2
elif operation == "Multiply":
result = num1 * num2
else:
result = num1 / num2 if num2 != 0 else "Error: Division by zero"
st.success(f"Result = {result}")

Form with file upload
with st.form("upload form"):
name = st.text_input("Name")
file = st.file_uploader("Upload a CSV", type=["csv"])
submit = st.form_submit_button("Upload")
if submit:
if file is not None:
import pandas as pd
df = pd.read_csv(file)
st.write(f"Hello {name}, here is your file preview:")
st.dataframe(df.head())
else:
st.error("Please upload a file!")

Using st.session_state with Forms
refer to st.session_state here.
if "submitted_data" not in st.session_state:
st.session_state.submitted_data = None
with st.form("state_form"):
name = st.text_input("Name")
age = st.number_input("Age", 0, 120)
submit = st.form_submit_button("Save")
if submit:
st.session_state.submitted_data = {"name": name, "age": age}
# Show saved data even after reruns
if st.session_state.submitted_data:
st.write("### Stored Data")
st.json(st.session_state.submitted_data)
