Although session-state was designed like a dictionary, its behavior is not totally identical to a dictionary.
You want to implement functionalities separately from the frontend, and you only use the st.session-state from st in your backend to share the data.
Your backend is totally independent from the streamlit frontend, and you can write the unittests for the functionalities. Unfortunately, it won’t work because of some logic behind st.session-state.
This can be solved by having
### shared.py
db = {}
In every script, you can use db instead of st.session-state,
from shared import db
db["key"] = "some data"
then, db will mimic the st.session-state experience, and it won’t cause an error in your isolated unittests.
It is not a proper solution. I believe this issue must be handled by improving the st.session-state module.
KeyError: 'st.session_state has no key "messages". Did you forget to initialize it?
When I mentioned ‘mimic’, I just mentioned it as a dictionary shared globally. I might be not true because I don’t know about session-state perfect.
The point is that, if we use st.session-state as a data holder when we develop backend,
it causes an error during unittest. It is possible that I develop all backend with the shared.db, and then, in app.py, I always update st.session-state using shared.db.
However, if I were you, who already know very well how st.session-state work, I would touch st.session-state a bit to enhance the extensibility.
Or not. I am not sure if it is a reasonable approach because I didn’t look up the source code of st.session-state.
Ok, I see. So st.session_state doesn’t work in code like this because it needs an actual streamlit application running. I am not familiar with the code either, but I guess there are good reasons for that. I don’t think it would be productive discussing them here and I might be totally wrong anyway, so let’s just accept that it is how it is and it isn’t going to change anyime soon.
Now, looking at your SUT, it seems to me like a bit or domain logic that doesn’t need to depend on streamlit and just got that dependency by chance. You don’t need anything streamlit to manipulate a list of messages. So this is what I would do.
In that case, I would try to make any backend more independent on streamlit.
It would be actually better practice, because adding frontend dependency to backend is already a bad signal.
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.