But No matter what you enter in the input text field the text isn’t saved and the values sent back are always the default values life the GIF shows below.
the return statement is executed as soon as the button is clicked and no changes being logged text input changes even after st.form_submit_button() click action
Inputs widgets inside forms do work. The piece of the puzzle missing from your code has to do with saving the output of the input widgets. Currently, you’re returning two submit buttons instead of the output of both text input widgets.
Session State provides the functionality to store variables across reruns. Widget state (i.e. the value of a widget) is also stored in a session. This convenience feature makes it super easy to read or write to the widget’s state anywhere in the app’s code. Session State variables mirror the widget value using the key argument.
Solution
In your code, we need to add the key argument to both the fname and lname text input widgets. The output of those widgets can then be accessed by st.session_state.key:
import streamlit as st
def main():
if "first" not in st.session_state:
st.session_state.first = "John"
st.session_state.last = "Doe"
st.button(
"Show Form",
on_click=username_form,
args=([st.session_state.first + " " + st.session_state.last]),
)
def show_names():
st.write(st.session_state.first, st.session_state.last)
def username_form(name):
with st.form(key="test", clear_on_submit=True):
col1, col2 = st.columns(2)
fname = col1.text_input("Firstname", name.split()[0], key="first")
lname = col2.text_input("Lastname", name.split()[-1], key="last")
submit = st.form_submit_button(
"Submit", on_click=show_names
)
if __name__ == "__main__":
main()
Output
Notes
This implementation, however, will persist values only across reruns for a session. The values will be lost when users exit the app or reload their browser tab. Learn more about Session State here.
If you want to store the values of fname and lname somewhere more permanent, you’ll need to replace the show_names() function that currently displays values to one that writes to a remote database. We have a number of tutorials on how to connect Streamlit to popular databases:
Wrote a helper function to copy all keys that start with a unique name and move them to a dict inside session_state and then delete the keys from session state. so I can access the needed data easier and also keep session_state clean at the same time.
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.