Use nested dictionary define widget key

Hi all!

I wonder if I can define a widget’s key uses nested session state?

Example:

st.session_state[“page_1”] = {“widget_a”: 1, “widget_b”: 2}

and then in my widget:

st.widget(key = st.session_state[“page_1”] [“widget_a”])

So this way, i can easily manage all all session state within a page/session.

I know I can use prefix to kind of group them together, but can I somehow use nested dictionary?

Thanks!

If you set

st.session_state[“page_1”] = {“widget_a”: 1, “widget_b”: 2}
st.text_input("some label", key = st.session_state[“page_1”] [“widget_a”])

Then the key of the text input widget would be "1" and Streamlit would register st.session_state["1"]="" when the widget is initialized. The key parameter is for the key; the value is stored in Session State associated to that key. So while you could create a dictionary or list of keys associated to a single key in Session State, Streamlit would still take those keys as you use them to register them at the top level of Session State.

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