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:
st.session_state.name = ''
def submit_data():
st.session_state.name = st.session_state.widget
st.session_state.widget = ''
...
policy_name = st.text_input(key='widget')
...
submit_data()
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.
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()aftertext_input() and not as a callback.
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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.