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
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.