Streamlit : 1.37
pycharm: 3.1.x
I am developing a multi-pages app, it is going to share different widgets values across pages by preserving those values in session states. Here is part of my code, I want to save the user input from multi-selectbox() in session state ‘symbol ‘ via def save_value(key), which shared across pages, when switch back to this page, the saved value in session state ‘symbol’ write back to session state ‘_symbol’ via def get_value(key) and put it back as default value to multi-selectbox(). As a result, user do not need to input again.
Apparently, the script works fine but when I clear the cache and rerun, it trigger an error “KeyError: 'st.session_state has no key “_symbol”. Did you forget to initialize it?“
As seen in script, I have initialized the session_state of ‘_symbol’ at very beginning, why still got uninitialized error message?
Very appreciated if anyone have idea, thanks!
(Script)
import streamlit as st
if ‘_symbol’ not in st.session_state:
st.session_state[‘_symbol’] = ‘C’
def save_value(key):
st.session_state[key] = st.session_state[“_” + key]
def get_value(key):
st.session_state[“_” + key] = st.session_state[key]
if ‘symbol’ in st.session_state:
get_value(‘symbol’)
selected_symbol = st.multiselect(“Stock Symbol”, [‘A’,‘B’,‘C’,‘D’,‘E’], key=‘_symbol’,
on_change=save_value, args=(‘symbol’,))
st.write(selected_symbol)
st.write(st.session_state) # for debugging
(Replicate the problem)
- Run the script, select something in multi-selectbox (works fine)
- Click upper right menu → click clear cache → click re-run → error triggered as following
(Error message)
KeyError: ‘st.session_state has no key “_symbol”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs’
Traceback:
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\scriptrunner\exec_code.py”, line 75, in exec_func_with_error_handling
result = func()
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\scriptrunner\script_runner.py”, line 543, in code_to_exec
self._session_state.on_script_will_rerun(
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\safe_session_state.py”, line 66, in on_script_will_rerun
self._state.on_script_will_rerun(latest_widget_states)
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\session_state.py”, line 515, in on_script_will_rerun
self._call_callbacks()
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\session_state.py”, line 528, in _call_callbacks
self.new_widget_state.call_callback(wid)
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\session_state.py”, line 272, in call_callback
callback(*args, **kwargs)
File “C:\Users\pc\PycharmProjects\klee\2_test.py”, line 8, in save_value
st.session_state[key] = st.session_state["" + key]
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\session_state_proxy.py”, line 98, in getitem
return get_session_state()[key]
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\safe_session_state.py”, line 94, in getitem
return self._state[key]
File “C:\Users\pc\AppData\Roaming\Python\Python310\site-packages\streamlit\runtime\state\session_state.py”, line 411, in getitem
raise KeyError(_missing_key_error_message(key))