How to overwrite the value user input

Ask the community or our support engineers for answers to questions.
is that a way to overwrite or clear user input value by click a button?


for example, by click the button, clear the text input value.

Yes, you can store the value of the click and if its true, set the value of the text input to blank.

import streamlit as st

placeholder = st.empty()

input = placeholder.text_input('text')
click_clear = st.button('clear text input', key=1)
if click_clear:
    input = placeholder.text_input('text', value='', key=1)

This works for me. Does it answer your q?

Dinesh

3 Likes

it works, thanks. another trick in streamlit. it is amazing. Thanks.

1 Like

In your case, I think you can 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)

Why are the keys for button and text_input equal? Does this create a link of some kind between the objects?
Also, help me understand how does do placeholder work in this case: if it didn’t exist we would get a DuplicateWidget exception for the text_input, is that right?

@victorccaldas
that code is old. I suggest using the st.form and st.form_submit_button as jgcaceresr suggested.