Retain user input values in multipage app when changing pages

Hello,

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 any help with this.

2 Likes

Hey @Jeff,

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…

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.