I am using streamlit and I call a function in a separate function file from a login script, the function.py queries SQL and loads a bunch of data in the st.session as follows:
st.session_state["company_id"] = company_id
Once the user has logged in , I want to reference that st.session_state[“company_id”] from the rest of my application in all the other .py files, I have tried to output st.session_state[“company_id”] and all I get is:
KeyError: 'st.session_state has no key “company_id”.Did you forget to initialize it?
I am running streamlit on local machine - python v3.11.5 / streamlit v1.28.0
There must be something else going on that is altering the session state, or the code that is setting company_id isn’t actually running. Here is an example of a main streamlit page, a sample1.py script that is imported, and a second page called pages/another_page.py
When I do streamlit run app37.py, I see 42, and when I switch to “another page”, I also see 42.
Can you show a simplified example of your code which shows you setting the session state variable and then not seeing it when you try to use it from another file?
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.