I building an app that allows a user to input a sql query on one page and then edit the results of the query on another page. I use session state to share data between the pages which works as intended, but the issue I run into is the user is on page 2 working with the data but the navigates back to page 1 the view the source query and resulting data, the page has reset and there is nothing there. Is it possible to make the session data sticky on each page until the user is complete and inputs a new source query on page 1?
If you have some data in a widget with a key, the that key in session state will get removed when the widget disappears (either from conditional display or moving to a different page). You either have to use a gimmick to interfere with the widget cleanup process or duplicate the data to a different key in session state that isnβt tied to a widget.
Gimmick:
For some widget with key='my_key', you can put at the top of every page:
st.session_state.my_key = st.session_state.my_key
What this does is prevent the key from being deleted when you move away from the page containing that widget. It interrupts the widget cleanup process, basically. It needs to be on every page because it needs to be on the page you go to for the first time when leaving the page with the widget.
Duplicate the data
For the same widget with key='my_key, copy the data to another key _my_key, for example in a callback.
def copy_data(key):
st.session_state['_' + key] = st.session_state[key]
# Initialize data copy with default value
if '_my_key' not in st.session_state:
st.session_state._my_key = 'whatever default value you want'
# Use data copy as the source of truth
st.session_state.my_key = st.session_state._my_key
st.text_area('Label', key='my_key', on_change=copy_date, args=['my_key'])
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.