# Using st.cache\_data for user choices

**URL:** <https://discuss.streamlit.io/t/using-st-cache-data-for-user-choices/41268>\
**Category:** Using Streamlit\
**Created:** [April 13, 2023, 7:59pm UTC](https://discuss.streamlit.io/t/using-st-cache-data-for-user-choices/41268 "2023-04-13T19:59:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrkthmpsn](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/mrkthmpsn/32/14723_2.png) [@mrkthmpsn](https://discuss.streamlit.io/u/mrkthmpsn)\
**Post date:** [April 13, 2023, 7:59pm UTC](https://discuss.streamlit.io/t/using-st-cache-data-for-user-choices/41268/1 "2023-04-13T19:59:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Tyler](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/tyler/32/5397_2.png) [@Tyler](https://discuss.streamlit.io/u/Tyler)\
**Post date:** [April 13, 2023, 8:56pm UTC](https://discuss.streamlit.io/t/using-st-cache-data-for-user-choices/41268/2 "2023-04-13T20:56:13Z")

</div>

hey @mrkthmpsn!

So st.cache\_data is not going to be particularly helpful there. You’re right, [session\_state](https://docs.streamlit.io/library/advanced-features/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](https://docs.streamlit.io/knowledge-base/tutorials/databases/private-gsheet), other folks use [firestore](https://blog.streamlit.io/streamlit-firestore/), even others use [sqlite](https://docs.python.org/3/library/sqlite3.html#sqlite3-tutorial). 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

---

<div class="post-metadata">

**Author:** ![mrkthmpsn](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/mrkthmpsn/32/14723_2.png) [@mrkthmpsn](https://discuss.streamlit.io/u/mrkthmpsn)\
**Post date:** [April 13, 2023, 11:51pm UTC](https://discuss.streamlit.io/t/using-st-cache-data-for-user-choices/41268/4 "2023-04-13T23:51:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Tyler](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/tyler/32/5397_2.png) [@Tyler](https://discuss.streamlit.io/u/Tyler)\
**Post date:** [April 14, 2023, 12:45am UTC](https://discuss.streamlit.io/t/using-st-cache-data-for-user-choices/41268/5 "2023-04-14T00:45:01Z")

</div>

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](https://medium.com/snowflake/snowflake-oauth-for-streamlit-e95e4cb3de6c) or [streamlit-keycloak](https://github.com/bleumink/streamlit-keycloak).

Hope that helps

---

<div class="post-metadata">

**Author:** ![mrkthmpsn](https://sea2.discourse-cdn.com/streamlit/user_avatar/discuss.streamlit.io/mrkthmpsn/32/14723_2.png) [@mrkthmpsn](https://discuss.streamlit.io/u/mrkthmpsn)\
**Post date:** [April 14, 2023, 11:06pm UTC](https://discuss.streamlit.io/t/using-st-cache-data-for-user-choices/41268/6 "2023-04-14T23:06:24Z")

</div>

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

> **[st.experimental\_set\_query\_params - Streamlit Docs](https://docs.streamlit.io/library/api-reference/utilities/st.experimental_set_query_params)**
>
> st.experimental\_set\_query\_params sets query parameters shown in the browser's URL bar.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/streamlit/original/3X/c/f/cf4c2ee0d938358f82448c5fe4b9c3c91d7ce19c.svg) [@system](https://discuss.streamlit.io/u/system)\
**Post date:** [October 11, 2023, 11:07pm UTC](https://discuss.streamlit.io/t/using-st-cache-data-for-user-choices/41268/7 "2023-10-11T23:07:06Z")

</div>

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