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.)