Dear Streamlit community, I dared to write my first multipage app (https://amfupapp.streamlit.app). In fact it is my first streamline app ever, so please apologize if I missed something obvious. I observe an error regarding a session state variable I use. I already studied the documentation, but without success.
I set up the app as described in the streamlit documentation: main file in the top folder and all the other pages in folder „pages“. In the main file „Willkommen.py“ I initialized the session state variable „last_topic“ right at the beginning of the file with a default value ‘X‘:
if 'last_topic' not in st.session_state:
st.session_state.last_topic = 'X'
In the files of the multi-pages - located in folder „pages“ - the value of “last_topic” is referred. Here the problem arises: sometimes an error is thrown, e.g. in file „04 Technische Kenntnisse Klasse E.py“ (in folder pages) with the following error text (parts extracted below):
KeyError: 'st.session_state has no key "last_topic". Did you forget to
initialize it? More info:
https://docs.streamlit.io/develop/concepts/architecture/session-state#initializa
tion'
During handling of the above exception, another exception occurred:
────────────────────── Traceback (most recent call last) ───────────────────────
/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptru
nner/exec_code.py:88 in exec_func_with_error_handling
/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/scriptru
nner/script_runner.py:579 in code_to_exec
/mount/src/amfup/pages/04 Technische Kenntnisse Klasse E.py:667 in <module>
664 │ │ st.write('Um die falsch beantworteten Fragen zu exportieren, w
665
666 if __name__ == '__main__':
❱ 667 │ main()
668
/mount/src/amfup/pages/04 Technische Kenntnisse Klasse E.py:43 in main
40 │ st.markdown('# Klasse E')
41 │
42 │ # User switched from other page (e.g. from A)
❱ 43 │ if st.session_state.last_topic != 'E':
44 │ │ st.session_state.reset = False
45 │ │ st.session_state.last_topic = 'E'
46
/home/adminuser/venv/lib/python3.12/site-packages/streamlit/runtime/state/se
ssion_state_proxy.py:131 in __getattr__
────────────────────────────────────────────────────────────────────────────────
AttributeError: st.session_state has no attribute "last_topic". Did you forget
to initialize it? More info:
https://docs.streamlit.io/develop/concepts/architecture/session-state#initializa
tion
The error is - unfortunately - not reproducible and appears from time to time on local machine as well as deployed in cloud. From this message I would conclude that “last_topic” was not initialized, but it was (in Willkommen.py) as described above.
I found a hint in Stack Overflow (python - Session state is reset in Streamlit multipage app - Stack Overflow) and included the following line at the very beginning of every page (directly behind: import streamlit as st):
for k, v in st.session_state.to_dict().items():
st.session_state[k] = v
However, in my case, this „hack“ did not solve my problem. I would be very glad for any assistance. Thank you in advance.