For a project I needed to build multi-page app with a settings page. As I’ve seen this topic requesting the feature, I decided to share the solution I came up with. It features a custom version of @thiago’s SessionState.
It still relies on hacks, but it should work in most cases. It was tested with Streamlit version 0.61.0 (and now 0.65).
Things you must be aware of before using it:
You must call state.sync() at the end of your app if you set a widget’s default value to a state variable. This function triggers a rerun if you store a new state value or update an already stored object. This is how I fix rollback issues when you interact with widgets bound to a state value. In my demo I call it at the end of main().
If you use state.sync(), you won’t be able to store objects that Streamlit cannot hash, just like when you use @st.cache and you get a UnhashableTypeError.
If I start to drag a slider, 1 second later the mouse is released, probably because of my rerun
I’ve updated the session state implementation in the gist. Instead of doing a naive value comparison, I normalize them first using an internal streamlit function used in their caching system. Now, every object supported by st.cache should work fine.
I’ve also added the possibility to access state values like so : state["my_input"]. This can be useful when you need to assign values dynamically.
Thanks a lot for this. This is very helpful. I’ll definitely provide more feedback as I continue to implement this code in the application I’m building.
The session state implementation is working great so far. However, I was wondering if you would be able to implement something where all stored inputs could be reset after a certain action? for example for my application, if I upload a new dataset, I would like to have all inputs reset. I dont want the end user to have to refresh the page every time.
Programmable state is on our roadmap (after custom components and one-click deploy), and this example will definitely provide inspiration. I can see better why you want to tie the state to a page.
We are not implementing (or even designing) yet, but we’d love to collaborate at some point. In particualr, I’d be curious to know if there are any features in Streamlit which would unblock progress on programmable state for you, or any other thoughts you have.
How would I be able to create custom hash functions for specific objects? For example, I need to define a custom hash function for XGBRegressor in order to store in the state.
That’s something I need to add, indeed. I’ll update the snippet in a few hours. After the modification you’ll be able to pass hash functions to _get_state({...}) in a dictionary as you do with @st.cache.
Hi @okld I’ve started playing with this afternoon. This truly is an outstanding template that will benefit many, well done!
Now, having pasted some of my code, I have the following issue:
UnboundLocalError: local variable 'st' referenced before assignment
Traceback:
File "c:\users\charly\desktop\multi-sessions-app\venv\lib\site-packages\streamlit\ScriptRunner.py", line 319, in _run_script
exec(code, module.__dict__)
File "C:\Users\Charly\Desktop\Multi-sessions-app\st_demo_settings.py", line 400, in <module>
main()
File "C:\Users\Charly\Desktop\Multi-sessions-app\st_demo_settings.py", line 22, in main
pages[page](state)
File "C:\Users\Charly\Desktop\Multi-sessions-app\st_demo_settings.py", line 29, in cloud_nlp
st.title(":chart_with_upwards_trend: Dashboard page")
Would fixing the issue be as easy as re-arranging some blocks/functions within the code?
Out of interest, is it possible to retain the state/results of these 2 added tabs?
Currently, if I trigger results in 1 tab, switch to another tab then come back, these results are gone. Not a deal breaker but it would be super nice to retain their states as well!
If you’re using the page structure implemented in my gist, I pass to each page function a state object as argument into which you can retain any value you want.
I’ll update my post but you need to call that state.sync() function only if you’re binding a widget to a state value.
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.