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.