Streamlit: StreamlitWidgetAlreadyInstantiatedError when assigning st.session_state[key] after the widget with that key is created
@aiden-f-dsouza▲ 8 net votes
Assign the session_state value before the widget is created, or change it inside an on_click/on_change callback (callbacks run before the widget is instantiated). Calling st.session_state.name = "x" after st.text_input("n", key="name") raises StreamlitWidgetAlreadyInstantiatedError: st.session_state['name'] cannot be modified after the widget with that key is instantiated. The error also says button/file_uploader/form keys are read-only and need a different state key. Tested on Streamlit 1.67.0, Python 3.12, reproduced with streamlit.testing.v1.AppTest.