I’m self-hosting a streamlit app and have users complain that the session state is lost if they step away from the computer for some time. What is a good way for dealing with this?
I thought about saving the session_state to disk, but some of the entries are not serializable.
Then there is the st_autorefresh component. Is that the recommended way?
Or can I just change some configuration paramter, such that the session stays alive for longer?
What platform/OS/service are you using to self-host? If I run a Streamlit app locally, I can leave it overnight without losing state. Although, some elements, like st.file_uploader can cause a little mayhem when trying to keep a session running.
Also, your users’ browser settings can come into play. If a websocket disconnects and doesn’t quickly reconnect, then the session will be lost. This is especially common on mobile devices or if a device goes to sleep. (If you scale horizontally by creating multiple Streamlit servers and don’t have custom routing, a websocket reconnect could reset even an active session if it doesn’t coincidentally reconnect to the exact same server.)
Besides st_autorefresh, you can also define a trivial fragment with run_every defined. st_autorefresh will rerun the app, but a fragment could rerun a basically empty function for efficiency. Depending on your and your users’ precise situation, those may or may not address your problem, though.
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.