Setting text_input value reset the session

Hi folks,

I’m trying to do a super simple app but have some problems.
I have a text_input and a button widget.
Every time that a user inserts a text to the text_input widget, I st.write its input.
If he pressed the button, I st.write the button's text (and set the text_input value to the same as the button's).
The thing is that when the user presses the button and therefore inserts manual text (through the text_input), the page seems to be reset and to value comes to the st.write.

Here is my code:

import streamlit as st


if __name__ == '__main__':
    input = st.empty()
    txt = input.text_input("Insert text:")
    bt = st.button("Text01")

    if bt:
        txt = "Text01"
        input.text_input("Insert text:", value=txt)

    st.write(txt)

Here is a record of that scenario:

What do I wrong?

Thanks!

up

Hi,

I think what you are aiming for is:

import streamlit as st


if __name__ == '__main__':
    input = st.empty()
    txt = input.text_input("Insert text:")
    bt = st.button("Text01")
    st.write(txt)

With Streamlit you don’t have to worry about these kind of things. Let me know if it worked!

Regards,

Bart

Hi @bartvanes,

Unfortunately your solution doesn’t work for me - when I press the button I do not see it’s text value in the text_input box.

Let’s say I have 100 buttons, I want to project the user the buttons text at the text_input widget, In addition I want he’ll be able to insert their whatever he wants.

Thank you,
DanielJ.

Actually you don’t really need the button.

import streamlit as st

name_dict = {"Anord":"", "Bernald":""}

for k, v in name_dict.items():
    name_dict[k] = st.text_input(k, v)
    st.write(name_dict[k])

“Press Enter to Apply” is a far more efficient way.
BTW, button object is really tricky in streamlit. If button is a must, then I recommend you to search topic about streamlit SessionState.

@thiago - I’ll appreciate some help in this scenario and how can it be solved via appropriate usage of session state.

Many thanks!
Daniel.