Clear input box after hitting enter

Hi everyone,
Iā€™m a newbie, sorry if my question is rather stupid, but I searched before asking this question.
In terms of the input box, I want to write the input, and once I hit enter, it clears the input box. Currently the input remains in the box. I donā€™t want to use a submit button because I donā€™t want the user every time move the mouse to click on it. I wanted to create a shortkey that when I hit enter, runs the ā€˜submit buttonā€™ because I saw in the streamlit extras you can create a short key to go to a specific URL. But yet again I failed. Thank you!

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}')
11 Likes

Thank you so much @mathcastand.
Iā€™ll give it a try :slight_smile:

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