Clear input box after hitting enter

You can use session state to store the value for the widget and clear it. Then, use the stored value in the rest of the script (not the “output” from the widget).

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', on_change=submit)

st.write(f'Last submission: {st.session_state.something}')
13 Likes