St.session_state.widget cannot be modified after the widget with key widget is instantiated


I’m trying to clear a text box after hitting submit, so I’m following this: Clear input box after hitting enter which does work in this code:

import streamlit as st
if 'something' not in st.session_state:
    st.session_state.something = ''

def submit():
    st.session_state.something = st.session_state.widget
    st.session_state.widget = ''

st.text_input('Something', key='widget')
st.button('Submit', on_click=submit)
st.write(f'Last submission: {st.session_state.something}')

For some reason though, in my actual application the same thing doesn’t work.

Steps to reproduce

Code snippet:

if 'name' not in st.session_state: = ''

def submit_data(): = st.session_state.widget
    st.session_state.widget = ''
                policy_name = st.text_input(key='widget')

The error I get is: StreamlitAPIException: st.session_state.widget cannot be modified after the widget with key widget is instantiated.

It’s super weird because I can’t see what I did different from the first app and the actual application. Am I doing something wrong? Same Streamlit version for both apps.

Thank you :pray:

You need to call submit_data as a callback or before calling text_input.

Ohh okay that makes sense, is that not what I’m doing here?

Your snippet is not really python code and I am not sure how to fill the gaps, but by the look of it I would say no. You seem to be calling submit_data() after text_input() and not as a callback.

but this seems like a general problem. when a default value is being modified the whole session state is being cleared. there is a detailed SOF python - Session state is reset in Streamlit multipage app - Stack Overflow. Is there a simpler way to resolve this.

It is not the case that the whole session state is being cleared when a default value is being modified and the SO question you linked is not about that. It is about state being cleared when a widget is not rendered.

There have been discussions (that I can’t find right now) about changing this behavior, but in my understanding they were inconclusive. This is a hard problem to solve:

  • Backwards compatibility is important.
  • It is not crystal clear what the behavior should be (in some cases forgetting the state is just the right thing to do, in other cases it is clearly wrong).

Right now the only ways around this are the ones proposed in the accepted answer.

That said, this seems totally unrelated to the issue described in the OP.

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