I am trying create a multi-user sign-up/login based app. But getting error. I have shared the below snippet of initialize code. The error i’m getting is
KeyError: 'st.session_state has no key "loggedin". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization'
Code snippet:
# Initialize session_state
if "loggedin" not in st.session_state:
st.session_state.update({"loggedin": False, "user_id": None})
I am using spyder and latest version of streamlit.
Session state initializes fine for me on Streamlit 1.21.0 with that snippet. Are there other aspects to your code? Are those keys ever assigned to widgets (which can remove them when the widgets go away)?
import streamlit as st
# Initialize session_state
if "loggedin" not in st.session_state:
st.session_state.update({"loggedin": False, "user_id": None})
st.write(st.session_state)
This one runs fine for me, too. Is there some missing information? Is the error happening for you within the script you have provided or do you encounter an error somewhere else in your app? What steps are needed to trigger the error you see?
(Note that since you don’t use a callback and don’t include st.experimental_rerun nested inside your Login and Logout buttons, you do end up needing “two clicks” to advance the screen, but that’s not the key error mentioned in the first post.)
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.