Form submit works only on odd tries

Form submit works not on every click. Here is example:

import streamlit as st

with st.form(key="input_form"):
    v = st.session_state.get("v", "")
    out = st.text_area(label="input", value=v)

    sbutton = st.form_submit_button('Show')
    if sbutton:
        st.session_state["v"] = out

st.json(st.session_state)

input: 1
result:

{
    "v":"1"
    "FormSubmitter:input_form-Show":true
}

input: 12
result:

{
    "v":"1"
    "FormSubmitter:input_form-Show":true
}

in form text == 1

Same problem found in this sample
On even tries - data simply lost

@anant_raj

There’s a simpler solution to tie a text input to an entry in session_state, which is to set the key on the input, like this:

import streamlit as st

with st.form(key="input_form"):
    out = st.text_area(label="input", key="v")

    sbutton = st.form_submit_button('Show')

st.json(st.session_state)

This works without the issue you described.

1 Like