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 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)
@jgcaceresr
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")
time.sleep(5)
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!')
time.sleep(1)
question = placeholder.text_input(label = 'Question', key = '2')
placeholder_2.empty()
@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).
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.
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!
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.