Every second user input ignored when using SessionState to set text input

I am trying to set the value of a text_input programatically using a button, similar to this post and this post, but I am using the SessionState workaround. Setting the value works, but when trying to input two different texts manually, the second input gets ignored and the value is reset to the previous value. Also the button has some weird behaviour.

Here is a minimal example of the problem:

state = SessionState.get(text='foo')
container = st.empty()
set_to_bar = st.button('set bar')

if set_to_bar:
    state.text = 'bar'

state.text = container.text_input('Enter text', value=state.text, key='text-in')
st.write(state.text)

To reproduce the problem, do the following:

  1. Enter a text into the text_input and press enter.
  2. Enter another text into the text_input and press enter.

Also, when entering a text and then pressing the button, the button click seems to get ignored. Any subsequent clicks get ignored as well until a new input is entered.

  1. Enter a text into the text_input and do not press enter.
  2. Press button.
  3. Repeat step 2 until sufficiently bored.
  4. Enter a text into the text_input and press enter.
  5. Repeat step 2.

The two behaviours seem to have some weird interplay as well sometimes, but I could not see a pattern.

Is this a bug or am I using Streamlit wrong?

2 Likes

I can replicate that behavior.