Using st.cache_data for user choices

Can st.cache_data be used for storing user choices/preferences longer than a session_state (which if I understand correctly(?) resets when a tab refreshes?). I’ve had an experience on mobile browser (Firefox) where leaving the tab inactive has triggered a refresh in the browser app, which has then reset the session state. Using cache seems like a more ‘permanent’ solution, but I’m not quite sure how to go about making it work, if it’s even possible. Any help appreciated

hey @mrkthmpsn!

So st.cache_data is not going to be particularly helpful there. You’re right, session_state lives only “as long as the tab is open and connected to the Streamlit server.” What it sounds like you’re looking for is a persistent state for your app! I have seen many different flavors of this, some people integrate google sheets, other folks use firestore, even others use sqlite. It all depends on what you want to store, but if you’re looking to store user choices in a more persistent way than session_state, you’ll probably need to go in this direction

1 Like

Ah, thanks!

A follow-up, not sure if this is a stupid question, but would I be right in thinking that in those implementations you’d need to store some kind of user identifier? Would they need to login?

Yes that’s a good point. One way of doing this would be to ask users in the app (e.g. in an expander) if they want to save their place in the app, and then if they say yes ask for their username and store the current data inputs
Then, have another place in your app (like in the sidebar) that just says “want to start this app from a stored input state?”, have them enter their username, and pull the data using the username.

There are more complicated but also more secure options, like @brianmhess’s cool new feature with oath and snowflake or streamlit-keycloak.

Hope that helps

For future reference of anyone finding this thread, I’ve turned to the experimental_query_params for a couple of my simpler use cases