Submit Form Button not working

Summary

Cannot seem to figure out how to get the submit form button to actually work.

Steps to reproduce

Code snippet:


if st.sidebar.button("Add User"):
    with form_container:
        with st.form("add user"):
            st.write("Add User")
            email = st.text_input("Email")
            customers = get_customers()
            customer = st.selectbox("Customer",customers)
            password = st.text_input("Password")
            submit_button = st.form_submit_button(label="Submit")
            if submit_button:
                print("Submit button pressed")
                create_user(email,customer,password)

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

Should print โ€œSubmit button pressedโ€, and call function.

Actual behavior:

Pressing the Submit button does clear the form, but nothing I do seems to actually set submit_button to True.

Debug info

  • Streamlit version: (get it with $ streamlit version)
  • Python version: (get it with $ python --version)
  • Using Conda? PipEnv? PyEnv? Pex?
  • OS version:
  • Browser version:

Requirements file

Using Conda? PipEnv? PyEnv? Pex? Share the contents of your requirements file here.
Not sure what a requirements file is? Check out this doc and add a requirements file to your app.

Links

  • Link to your GitHub repo:
  • Link to your deployed app:

Additional information

If needed, add any other context about the problem here.

When you have nested buttons, you have to be careful. Buttons donโ€™t remember they were clicked, they will only return True the first page load after they were clicked. Hence, when you click the inner button, the outer one will become False and you wonโ€™t re-enter the conditional.

There are multiple different ways to deal with this. You will need callbacks and session_state. I have an introductory video here: Session State Introduction

Here is one possible solution. (It may need a few extra checks to safeguard it from a key error depending on what other things there are on the page to click on.)

import streamlit as st

form_container = st.sidebar.container()
def get_customers():
    return [f'Customer {i+1}' for i in range(10)]

def submitted():
    st.session_state.submitted = True
def reset():
    st.session_state.submitted = False


if st.sidebar.button("Add User", on_click=reset):
    with form_container:
        with st.form("add user"):
            st.write("Add User")
            email = st.text_input("Email", key='new_email')
            customers = get_customers()
            customer = st.selectbox("Customer",customers, key='new_customer')
            password = st.text_input("Password", key='new_password')
            st.form_submit_button(label="Submit", on_click=submitted)
if 'submitted' in st.session_state:
    if st.session_state.submitted == True:
        print("Submit button pressed")
        new_user = {'email':st.session_state.new_email,
                     'customer':st.session_state.new_customer,
                     'password':st.session_state.new_password}
        st.sidebar.write(new_user)
        reset() # Prevents rerunning new user creation on next page load

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.