# Introducing Submit button and Forms 📃

**URL:** <https://discuss.streamlit.io/t/introducing-submit-button-and-forms/12401>\
**Category:** Show the Community!\
**Created:** [April 29, 2021, 6:02pm UTC](https://discuss.streamlit.io/t/introducing-submit-button-and-forms/12401 "2021-04-29T18:02:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![streamlitbot](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/streamlitbot/32/1621_2.png) [@streamlitbot](https://discuss.streamlit.io/u/streamlitbot)\
**Post date:** [April 29, 2021, 6:02pm UTC](https://discuss.streamlit.io/t/introducing-submit-button-and-forms/12401/1 "2021-04-29T18:02:23Z")

</div>

Have you ever tried to build an app around a complex Machine Learning model, and found that rerunning the model every time the user changed an input value led to a less than ideal user experience? If so, it's likely because the parameters being supplied to your model use input widgets like `st.text_input`, `st.number_input` or `st.slider` and any time you change a widget the entire app is re-run.

To help solve this we're introducing a pair of commands called `st.form` and `st.form_submit_button`. This lets you batch input widgets together and submit the widget values with the click of a button — triggering only a single rerun of the entire app!

Check out this [sample app](https://share.streamlit.io/streamlit/release-demos/0.81/0.81/streamlit_app.py?page=forms_demo) which shows the new commands in action, but make sure to keep reading if you want to learn more about how it works.

## Getting started

Forms are like any other Streamlit container and can be declared using the `with` statement:

```
# Using the "with" syntax
with st.form(key='my_form'):
	text_input = st.text_input(label='Enter some text')
	submit_button = st.form_submit_button(label='Submit')

```

Or, if you prefer, you can also use object notation:

```
# Declare a form and call methods directly on the returned object
form = st.form(key='my_form')
form.text_input(label='Enter some text')
submit_button = form.form_submit_button(label='Submit')

```

In your app, this creates a form with a submit button as follows:

### **What this does**

Outside of forms, any time a user interacts with a widget the app's script is rerun. What `st.form` does is make it so users can interact with the widgets as much as they want, _without causing a rerun!_ Instead, to update the app, the user should click on the form's submit button.

## Form submit button

In the example above, notice that `st.form_submit_button` is in some ways similar to `st.button`, but differs in others:

### **Similarities**

Just like the regular `st.button`, the submit button for a form, `st.form_submit_button` returns a boolean to indicate whether the form was submitted or not. This allows for building additional logic upon submit button. For e.g.

```
form = st.form(key='my-form')
name = form.text_input('Enter your name')
submit = form.form_submit_button('Submit')
st.write('Press submit to have your name printed below')
if submit:
    st.write(f'hello {name}')

```

### **Differences**

- A **Form submit button** is a special button which **_batch submits_** the state of the widgets contained in the form.
- A form must have an associated `st.form_submit_button` otherwise Streamlit throws an error.

## Form Features

`st.form` can be placed anywhere in a Streamlit app, you can even put columns inside of forms, or forms inside of columns, and everything will work as expected!

### Forms inside columns

```
col1, col2 = st.beta_columns(2)
with col1:
    with st.form('Form1'):
        st.selectbox('Select flavor', ['Vanilla', 'Chocolate'], key=1)
        st.slider(label='Select intensity', min_value=0, max_value=100, key=4)
        submitted1 = st.form_submit_button('Submit 1')
with col2:
    with st.form('Form2'):
        st.selectbox('Select Topping', ['Almonds', 'Sprinkles'], key=2)
        st.slider(label='Select Intensity', min_value=0, max_value=100, key=3)
        submitted2 = st.form_submit_button('Submit 2')

```

Changing the contents of a form in the left column or submitting the left form does not impact the form on the right and vice versa.

## Columns inside forms

```
with st.form(key='columns_in_form'):
    cols = st.beta_columns(5)
    for i, col in enumerate(cols):
        col.selectbox(f'Make a Selection', ['click', 'or click'], key=i)
    submitted = st.form_submit_button('Submit')

```

Changing the contents of any one of the select boxes does not impact the other select boxes and a rerun is triggered upon clicking the submit button.

### Helpful errors and warnings

To help developers in certain situations where it detects an out-of-place or missing submit button, Streamlit will shows a warning or throws an exception in the following cases:

- If a `st.form_submit_button` is defined without a form scope, Streamlit will throw an exception and stop execution.
- If no submit button is defined, Streamlit will show a warning without interrupting the app flow.

Above: A warning about a missing Submit button in a form.
## Limitations and other notes

- An &nbsp;`st.form` cannot be embedded inside another `st.form`.
- Every form must have an associated `st.form_submit_button`.
- By definition, `st.button`s do not make much sense within a form. Forms are all about batching widget state together, but buttons are inherently stateless. So declaring an `st.button` inside a form will lead to an error.
- Also by definition, interdependent widgets within a form are unlikely to be particularly useful. If you pass the **_output_** of `widget1` into the **_input_** for `widget2` inside a form, then `widget2` will only update to `widget1`'s value when the form is submitted.
- We are currently working on functionality that allows you to programmatically reset all widgets when the submit button is clicked. This should be added in an upcoming release.

## Wrapping up

You can now add `st.form` and `st.form_submit_button` to your apps to help make them more responsive in just two lines of code!

So go ahead and upgrade Streamlit to version 0.81.0 today!

```
pip install --upgrade streamlit

```

We're excited to see how you'll use these new commands, so make sure to come share what you create on [our forum](https://discuss.streamlit.io) or on [Twitter](https://twitter.com/streamlit). If you have any questions about these (or about Streamlit in general) let us know on the forum or in the comments below! 🎈

## Resources

* * *
This is a companion discussion topic for the original entry at [https://blog.streamlit.io/introducing-submit-button-and-forms/](https://blog.streamlit.io/introducing-submit-button-and-forms/)

---

<div class="post-metadata">

**Author:** ![Igor](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/igor/32/8249_2.png) [@Igor](https://discuss.streamlit.io/u/Igor)\
**Post date:** [August 23, 2021, 1:49pm UTC](https://discuss.streamlit.io/t/introducing-submit-button-and-forms/12401/2 "2021-08-23T13:49:50Z")

</div>

Actually you can not make columns inside of a form which is inside of a column ☹

---

<div class="post-metadata">

**Author:** ![LostGoatOnHill](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/lostgoatonhill/32/5779_2.png) [@LostGoatOnHill](https://discuss.streamlit.io/u/LostGoatOnHill)\
**Post date:** [November 26, 2021, 2:22pm UTC](https://discuss.streamlit.io/t/introducing-submit-button-and-forms/12401/3 "2021-11-26T14:22:09Z")

</div>

Does anyone know if a key can be bound to the form\_submit\_button, to get the form to submit on press of for example? Thanks!

---

<div class="post-metadata">

**Author:** ![rattlesnakey](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/rattlesnakey/32/12072_2.png) [@rattlesnakey](https://discuss.streamlit.io/u/rattlesnakey)\
**Post date:** [December 4, 2021, 1:38pm UTC](https://discuss.streamlit.io/t/introducing-submit-button-and-forms/12401/4 "2021-12-04T13:38:20Z")

</div>

how can I check every blank are filled while user click submit? thx!

---

<div class="post-metadata">

**Author:** ![walikyu\_yudin](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/walikyu_yudin/32/16237_2.png) [@walikyu\_yudin](https://discuss.streamlit.io/u/walikyu_yudin)\
**Post date:** [February 8, 2023, 7:10am UTC](https://discuss.streamlit.io/t/introducing-submit-button-and-forms/12401/5 "2023-02-08T07:10:59Z")

</div>

Hai , it is very nive feature, however sometimes when I clicked the button, the form do nothing just clear form. then I tried again, it worked, it is like nearly every two clicked it stop working after press the button

---

<div class="post-metadata">

**Author:** ![Awais](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/awais/32/31253_2.png) [@Awais](https://discuss.streamlit.io/u/Awais)\
**Post date:** [August 26, 2023, 4:55pm UTC](https://discuss.streamlit.io/t/introducing-submit-button-and-forms/12401/6 "2023-08-26T16:55:58Z")

</div>

Is there some method to change the position of Submit button? Now, it is in the beginning of left side, what if I want to move to center or right end of the form?

---

<div class="post-metadata">

**Author:** ![marduk](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/marduk/32/11690_2.png) [@marduk](https://discuss.streamlit.io/u/marduk)\
**Post date:** [August 26, 2023, 7:01pm UTC](https://discuss.streamlit.io/t/introducing-submit-button-and-forms/12401/7 "2023-08-26T19:01:33Z")

</div>

Hi Awais,

You can use the `use_container_width` parameter of the `st.form_submit_button` to make it occupy the full width of the form. Otherwise, if I remember correctly it should be possible to create multiple columns inside the form, and then assign the `st.form_submit_button` to one of them.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![Awais](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/awais/32/31253_2.png) [@Awais](https://discuss.streamlit.io/u/Awais)\
**Post date:** [August 27, 2023, 3:27pm UTC](https://discuss.streamlit.io/t/introducing-submit-button-and-forms/12401/8 "2023-08-27T15:27:03Z")

</div>

I tried these 2 solutions: [Number 1](https://discuss.streamlit.io/t/how-to-have-2-columns-on-a-form/20997/3) and [Number 2](https://discuss.streamlit.io/t/how-to-use-multiple-columns-in-forms-so-that-the-input-is-side-by-side-instead-of-below-each/21035/4).  
Both of theses solutions did not work. buttons will keep showing left aligned.  
Below is the code, I tried to bring buttons in different columns:

```auto
    form = st.form(key='Home Page')
    c1, c2, c3, c4, c5 = st.columns(5)
    with c1:
        employee_login = form.form_submit_button(label='Employee Log in')
    with c2:
        xxxx = form.text_input(label='Anything')
    with c3:
        admin_login = form.form_submit_button(label='Admin Log in')
    with c4:
        reg_new_user = form.form_submit_button(label='New User? Register here')
    with c5:
        yyyyy = form.text_input(label='Anything2')

```

Result:

 ![image](https://global.discourse-cdn.com/streamlit/original/3X/f/1/f1f42b156fdbae07c85f0bdb53407ba89b7f2109.png)

---

<div class="post-metadata">

**Author:** ![marduk](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/marduk/32/11690_2.png) [@marduk](https://discuss.streamlit.io/u/marduk)\
**Post date:** [August 27, 2023, 6:26pm UTC](https://discuss.streamlit.io/t/introducing-submit-button-and-forms/12401/9 "2023-08-27T18:26:47Z")

</div>

Hi again @Awais ,

You are very close to implementing it correctly 🙂

See below two equivalent ways of writing the example:

```auto
import streamlit as st

form = st.form(key='Home Page')

### One way of writing this example
c1, c2, c3, c4, c5 = form.columns(5)
employee_login = c1.form_submit_button(label='Employee Log in')
xxxx = c2.text_input(label='Anything')
admin_login = c3.form_submit_button(label='Admin Log in')
reg_new_user = c4.form_submit_button(label='New User? Register here')
yyyyy = c5.text_input(label='Anything2')

### Another equivalent way of writing it
# with form:
# c1, c2, c3, c4, c5 = st.columns(5)
# with c1:
# employee_login = st.form_submit_button(label='Employee Log in')
# with c2:
# xxxx = st.text_input(label='Anything')
# with c3:
# admin_login = st.form_submit_button(label='Admin Log in')
# with c4:
# reg_new_user = st.form_submit_button(label='New User? Register here')
# with c5:
# yyyyy = st.text_input(label='Anything2')

```

---

<div class="post-metadata">

**Author:** ![Shivam\_Singhal](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/shivam_singhal/32/27912_2.png) [@Shivam\_Singhal](https://discuss.streamlit.io/u/Shivam_Singhal)\
**Post date:** [August 27, 2023, 9:24pm UTC](https://discuss.streamlit.io/t/introducing-submit-button-and-forms/12401/10 "2023-08-27T21:24:28Z")

</div>

Hi,

I m using forms, and I am getting the data displayed on my app on submitting the form.  
Can it be stopped, from writing the data on app, when form is submitted  
Thanks
