When you click a form submit button, all of the widgets within the form will update. You have two options of retrieving the information:
- By having variables take in the output of the widgets
- By collecting it from session state
However, if the widgets do not exist on the page after a submit button is clicked, option 1 is out the window. Furthermore, you will be restricted on how you implement option 2 as the information (as natively stored) will only be available for one page load, then gone.
As such, a better workflow with a submit button if you are having the form disappear is to use a callback function to grab and store that information for later use.
Here’s a little toy case so you can see what happens to session state when a form disappears from view (Show/Hide button) and what happens with reloads/other interactions with the page (Page Reload button). In particular if you submit a form, hide it, then click the reload button, you’ll see the information associated directly to the widgets goes away, but the copy I made in the callback to other keys will stay.
import streamlit as st
st.session_state
def submit ():
st.session_state.last_A = st.session_state.A
st.session_state.last_B = st.session_state.B
if 'show' not in st.session_state:
st.session_state.show = True
def toggle():
st.session_state.show = not st.session_state.show
st.button('Show/Hide Form', on_click=toggle)
if st.session_state.show:
with st.form('my_form'):
st.text_input('Text', key='A')
st.number_input('Number', key='B')
st.form_submit_button('Submit', on_click = submit)
st.button('Page Reload')
st.session_state
And yes, the logic flow of the callback is coming in to play, namely that the values you want to pass as arguments are evaluated at page load and not at click. So you have to get “new information” from inside the callback, with the lookup happening to session state after the click.