Clear text_input

Hello. Is there a way to clear a “text_input”?

I created a “Question-Answering” App (this consists of loading a “context” and asking a question, an already trained HuggingFace model returns one or more answers). In my App the context (which is a document) is chosen with a “selectbox” and the question is defined in a “text_input”, after pressing “Enter” a “st.form” is executed showing several answers, each one accompanied of a “checkbox” so that the user can validate the result and update a dataset internally (which will help in the future to create a better model to our specific area). Validation is registered when clicking on “st.form_submit_button”.

I would like that after clicking on “st.form_submit_button” the “text_input” can be cleared for a new query. I tried to achieve that with various approaches but was unsuccessful. I tried putting a “key” to the “text_input” so that its value is modified to empty ("") when the button “st.form_submit_button” was executed with a “callback”, this “callback” would modify the “key” of “text_input” to: “”, but it doesn’t work. I tried also using “st.empty” it also doesn’t work correctly, clean once and not clean the next and so on.

I would appreciate any suggestions. Thank you.

This other question is related to the one I placed above: The example “Mirrored Widgets using Session State” (Session State for Streamlit. You can now store information across… | by abhi saini | Towards Data Science) is not working anymore as shown in the gift? I am not getting the same result on my PC, is there currently a way to reproduce the same result? Thank you!

I think the code below sums up my question. By printing the “session_state.text” I can see that it is actually updating during the callback, however the text in the “text_input” appears despite the session_state.text = “”.

import streamlit as st
import pandas as pd

def get_save():

	st.session_state.text = ""

	st.write('Entered the callback')

	# Creating a csv file
	df = pd.DataFrame({'col': 1}, index = [0])
	df.to_csv("Testing.csv", mode = "a", index = False, header = None)	

	st.write('2 st.session_state.text = ', st.session_state.text)

question = st.text_input(label = 'Question:', key = 'text')
st.write('1 st.session_state.text = ', st.session_state.text)

if question:

	with st.form(key = 'my_form', clear_on_submit = False):			

		st.write('Something is made here')					
		submit_button = st.form_submit_button(label = 'Submit', on_click = get_save)	

Captura de tela de 2021-11-06 23-32-41

to clear text_input, you can do like this:

import time
import streamlit as st
text = st.empty()
text.text_input("input something", value="", key="1")
text.text_input("input something", value="", key="2")

you input something first in st.text_input, and wait 5 seconds, you will see that st.text_input is cleared.

Thank you @BeyondMyself. I had already incorporated that approach into my actual code (it would be similar to the code below) and it doesn’t work correctly. When it is executed for the first time “apparently” it works fine, but in the second time the text that is put in “text_input” disappears after giving enter (you can also see it in your example) and, in my case, it cannot create the file.

import streamlit as st
import pandas as pd
import time

placeholder = st.empty()
question = placeholder.text_input(label = 'Question', key = '1')

if question:

	placeholder_2 = st.empty()

	with placeholder_2.form(key = 'my_form', clear_on_submit = False):			

		st.write('Something is made here')					

 		submit_button = st.form_submit_button(label = 'Submit')	
		if submit_button:

			# Creating a csv file
			df = pd.DataFrame({'col': question}, index = [0])
			df.to_csv("Testing_2.csv", mode = "a", index = False, header = None)
			st.write('File created!')
			question = placeholder.text_input(label = 'Question', key = '2')

@jgcaceresr Hi, did you resolve this? I have same issue, whereby I have form and an additional “clear” button where I’d like to clear all text input. Having different keys for same input I have same issue where entered text disappears (as it’s replaced by the other keyed version from session state).

EDIT: Raised as issue:

Posted a working example of form with clear button to clear input fields here:

In your case, this would not help?: Use “st.form” and “st.form_submit_button”, the former with the parameter: clear_on_submit = True (st.form - Streamlit Docs , st.form_submit_button - Streamlit Docs )

Unfortunately not, as my use case is a form for search terms applied to document search. Terms are iteratively modified to adjust results list. Do not want to clear them on every submit.

This seems to be the cleanest way to do this

I think you should be able to do this by using st.session_state and callbacks:

See here for more info on session state. Btw the callback above is needed so the clearing happens before the text input widget is rendered (callbacks are always executed before the script). I guess you could also solve this with a placeholder.

Lmk if this doesn’t work or you want something else!