St.session_state initialization error

Summary

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)

image

i have also tried this one. but no help. here is the code snippet for the streamlit multi-0user login feature:

def main():
    
    st.set_page_config(page_title='My App')
    session_state = st.session_state
    if not session_state.get('logged_in'):
        session_state['logged_in'] = False
    if not session_state.get('username'):
        session_state['username'] = ''

    st.sidebar.title('My App')
    if not session_state['logged_in']:
        st.sidebar.subheader('Login')
        username = st.sidebar.text_input('Username')
        password = st.sidebar.text_input('Password', type='password')
        if st.sidebar.button('Login'):
            if username == 'myusername' and password == 'mypassword':
                session_state['logged_in'] = True
                session_state['username'] = username
                st.sidebar.success('Logged in successfully')
            else:
                st.sidebar.error('Incorrect username or password')
    else:
        st.sidebar.subheader(f'Welcome, {session_state["username"]}')
        if st.sidebar.button('Logout'):
            session_state['logged_in'] = False
            session_state['username'] = ''
            st.sidebar.success('Logged out successfully')

    st.title('My App')
    st.write(f'Current user: {session_state["username"]}')
    st.write(f'Logged in: {session_state["logged_in"]}')
    st.write('App content goes here')



if __name__ == '__main__':
    main()

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?

image

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

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.