Streamlit-Authenticator
Streamlit-Authenticator copied to clipboard
Need instruction for avoiding TypeError of credentials loaded by streamlit.secrets
If we use streamlit.secrets to read credentials from "secrets" in the streamlit community cloud or "./streamlit/secrets.toml" in local, it raises TypeError.
TypeError: Secrets does not support item assignment.
Traceback:
-----------------
File "/Users/user/project/.venv/lib/python3.12/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 589, in _run_script
exec(code, module.__dict__)
File "/Users/user/project/src/main.py", line 12, in <module>
main()
File "/Users/user/project/src/main.py", line 8, in main
sign_up(run_gui)
File "/Users/user/project/src/project/gui.py", line 45, in sign_up
authenticator.login()
File "/Users/user/project/.venv/lib/python3.12/site-packages/streamlit_authenticator/views/authentication_view.py", line 222, in login
self.authentication_controller.login(token=token)
File "/Users/user/project/.venv/lib/python3.12/site-packages/streamlit_authenticator/controllers/authentication_controller.py", line 181, in login
return self.authentication_model.login(username, password, max_concurrent_users,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/user/project/.venv/lib/python3.12/site-packages/streamlit_authenticator/models/authentication_model.py", line 277, in login
self.credentials['usernames'][token['username']]['logged_in'] = True
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
File "/Users/user/project/.venv/lib/python3.12/site-packages/streamlit/runtime/secrets.py", line 110, in __setitem__
raise TypeError("Secrets does not support item assignment.")
----------
To avoid this error, we need to reconstruct credentials dict data to convert all 'streamlit.runtime.secrets.AttrDict' class to normal dict. Please add instruction to readme.
credentials = {
"usernames": {
k: dict(v) for k, v in st.secrets["credentials"]["usernames"].items()
},
}
authenticator = stauth.Authenticate(
credentials=credentials,
cookie_name=st.secrets["cookie"]["name"],
cookie_key=st.secrets["cookie"]["key"],
cookie_expiry_days=st.secrets["cookie"]["expiry_days"],
pre_authorized=dict(st.secrets["preauthorized"]),
)
Hi @sato1214syun, thank you for bring this to my attention.