KeyError: 'st.session_state has no key "$$GENERATED_WIDGET_ID--None". Did you forget to initialize it? More info:

Hey there!

I confirm this key error. Added some debug comments through the code. The error happens when importing streamlit_authenticator, as seen in the logs.

To raise the error, I did login, stopped the app and restarted it.

print('*** before streamlit import')

import streamlit as st

print('*** before streamlit_authenticator import')

import streamlit_authenticator as stauth

import yaml
from yaml.loader import SafeLoader

print('*** before yaml.load')

with open('config.yaml') as file:
    config = yaml.load(file, Loader=SafeLoader)

# hash passwords
users = config['credentials']['usernames']
for user in config['credentials']['usernames']:
    users[user]['password'] = stauth.Hasher([users[user]['password']]).generate()[0]

print('*** before stauth.Authenticate')

authenticator = stauth.Authenticate(
    config['credentials'],
    config['cookie']['name'],
    config['cookie']['key'],
    config['cookie']['expiry_days'],
    config['preauthorized']
)

print('*** before authenticator.login')

name, authentication_status, username = authenticator.login('Login', 'main')

print('*** after authenticator.login, name = ', name, ', authentication_status = ', authentication_status, ', username = ', username)

if authentication_status:
    authenticator.logout('Logout', 'main', key='unique_key')
    st.write(f'Welcome *{name}*')
    st.title('Some content')
elif authentication_status is False:
    st.error('Username/password is incorrect')
elif authentication_status is None:
    st.warning('Please enter your username and password')
streamlit run main.py 

  You can now view your Streamlit app in your browser.

  Local URL: http://localhost:8501
  Network URL: http://172.19.209.86:8501

*** before streamlit import
*** before streamlit_authenticator import
2023-05-24 17:56:56.384 Uncaught app exception
Traceback (most recent call last):
  File "/home/junior/dev/python/authenticator-example/venv/lib/python3.10/site-packages/streamlit/runtime/state/session_state.py", line 376, in __getitem__
    return self._getitem(widget_id, key)
  File "/home/junior/dev/python/authenticator-example/venv/lib/python3.10/site-packages/streamlit/runtime/state/session_state.py", line 421, in _getitem
    raise KeyError
KeyError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/junior/dev/python/authenticator-example/venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 561, in _run_script
    self._session_state.on_script_will_rerun(rerun_data.widget_states)
  File "/home/junior/dev/python/authenticator-example/venv/lib/python3.10/site-packages/streamlit/runtime/state/safe_session_state.py", line 68, in on_script_will_rerun
    self._state.on_script_will_rerun(latest_widget_states)
  File "/home/junior/dev/python/authenticator-example/venv/lib/python3.10/site-packages/streamlit/runtime/state/session_state.py", line 480, in on_script_will_rerun
    self._compact_state()
  File "/home/junior/dev/python/authenticator-example/venv/lib/python3.10/site-packages/streamlit/runtime/state/session_state.py", line 298, in _compact_state
    self._old_state[key_or_wid] = self[key_or_wid]
  File "/home/junior/dev/python/authenticator-example/venv/lib/python3.10/site-packages/streamlit/runtime/state/session_state.py", line 378, in __getitem__
    raise KeyError(_missing_key_error_message(key))
KeyError: 'st.session_state has no key "$$WIDGET_ID-1a8279410f90d35adda3725d5aedbfc0-init". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization'
*** before yaml.load
*** before stauth.Authenticate
*** before streamlit import
*** before streamlit_authenticator import
*** before yaml.load
*** before stauth.Authenticate
*** before authenticator.login
*** after authenticator.login, name =  None , authentication_status =  None , username =  
*** before streamlit import
*** before streamlit_authenticator import
*** before yaml.load
*** before stauth.Authenticate
*** before authenticator.login
*** after authenticator.login, name =  Junior Vidotti , authentication_status =  True , username =  jrvidotti