Question on saving and retrieving widget values shared across pages?

I am developing a web application using Streamlit for a multi-page design. I understand that the widget values input on each page can be shared across other pages by using session state to preserve the value. I spent two nights writing and testing code and studying its behavior. I found a good article that explains widget behavior in detail at Widget behavior - Streamlit Docs.

I followed the example codes to play around but accidentally triggered an error that I haven’t been able to fix so far. Maybe I still don’t understand the session state concept very well. I ran the following code found in the article, which talks about “Save widget values in Session State to preserve them between pages”:
def save_value():
st.session_state[“my_key”] = st.session_state[“_my_key”]

st.session_state[“_my_key”] = st.session_state[“my_key”]
st.number_input(“Number of filters”, key=“_my_key”, on_change=save_value)

What I understand is that once I input a number on the widget, the callback function save_value is triggered and it saves the value of ‘_my_key’ from the widget to another key ‘my_key’, which is saved for use across other pages. When I switch to another page and then switch back to this page, the saved value in ‘my_key’ will be copied back to the widget with the assigned key ‘_my_key’, preserving the widget’s value to avoid losing it after leaving the page.

However, I encountered an error following these steps:

Step 1) Open the page via the Streamlit command, the page launches. Do not input and leave it empty. On the upper right corner menu, click “Clear cache” and then click “Rerun”. The page initializes without any error and displays normally.

Step 2) After step 1, without making any changes on the page again, repeat step 1. Click “Clear cache” and then click “Rerun” on the menu. I get the error: KeyError: ‘st.session_state has no key “_my_key”. Did you forget to initialize it?’

This error is not difficult to understand because both keys have not been initialized. So, I added initialization code for both keys to ensure they exist at the very beginning while the page is loading:

if ‘my_key’ not in st.session_state:
st.session_state.my_key = 0

if ‘_my_key’ not in st.session_state:
st.session_state._my_key = 0

def save_value():
st.session_state[“my_key”] = st.session_state[“_my_key”]

st.session_state[“_my_key”] = st.session_state[“my_key”]
st.number_input(“Number of filters”, key=“_my_key”, on_change=save_value)

Step 3) I repeated steps 1 and 2, but I still got the same error. It seems that Streamlit can’t find the key “_my_key”.

Step 4) I tried replacing st.session_state[“_my_key”] with 1 in def save_value(), now it looked like st.session_state[“my_key”] = 1. Repeated steps 1 and 2, and no error occurred. However, I understand this is not a solution, but find out the error trigger point.

Maybe I still don’t fully understand the session state behavior. In the above test, I still don’t understand why Streamlit can’t find the key ‘_my_key’ even though I initialized it at the very beginning, the error is triggered when 2nd time clearing cache & rerun. Could anyone try the code and advise? Thank you very much!

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