I think this is a simple problem that probably has a simple solution that I just can’t figure out for some reason.
I have a multipage app. Page 1 takes user inputs, Page 2 displays outputs based on the Page 1 inputs. This works as expected. When a user puts values into Page 1, they populate as they should in Page 2 through the use of session states.
My issue is that if a user wants to go back to make a change to their inputs on Page 1 at a later time, all of the values are now missing from that page (and there are a lot of them). I want those values to be retained for the user so that if something changes in the future and they have to update a single field or two, they can update just those individual fields and not the entire data entry Page 1.
How can I allow a user to at time t = 0 enter information onto Page 1 and then navigate away from that to Page 2 where they receive the formatted output from their entries. At some point in the future t + n, the user wants to make a single change to their inputs so they navigate back to Page 1 only to find all of the fields empty. They now have to reenter lots of information if they want to make the single update rather than just updating the one field.
Thanks for sharing this question! Have you checked out session state? You can use session state to store the inputs from Page 1 and re-populate them when the user comes back to Page 1, provided that they’re still within the same session.
I have a keep-retrieve pattern I use when I want to save widget data between pages:
Alternatively, you can also assign a key to every widget you want to keep data for, save all those keys into a list all_my_widget_keys_to_keep then add at the top of every page:
if all_my_widget_keys_to_keep[0] not in st.session_state: # Check if your widget keys exist already
set_default_values() # Some function that initializes all your values for your widgets
for key in all_my_widget_keys_to_keep:
st.session_state[key] = st.session_state[key]
This hacky little thing interrupts the widget cleanup process which causes widget keys to get deleted when you navigate away from them. You will also see a simpler:
for key in st.session_state:
st.session_state[key] = st.session_state[key]
This bypasses the initialization problem, but beware of having certain widgets with keys that don’t like manual assignment like buttons and file uploaders…
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.