Submit Form Button in Streamlit

Question:

I made a simple app which will take user input and save it into SQL database.
The form will be displayed once the user will click the Form button after log-in.

The problem is with the st.form_submit_button, even tho it is clicked it show’s false and doesn’t perform any task which is supposed to perform.

if st.button("Form"):
    st.subheader("User Info Form")

    # name = st.text_input("Name")
    with st.form(key = 'user_info'):
        st.write('User Information')

        name = st.text_input(label="Name  ")
        age = st.number_input(label="Age  ")
        email = st.text_input(label="Email  ")
        phone = st.text_input(label="Phone  ")
        gender = st.radio("Gender  ", ("Male", "Female", "Prefer Not To Say"))

        submit_form = st.form_submit_button(label="Register", help="Click to register!")

        st.write(submit_form)

        # Checking if all the fields are non empty
        if submit_form:
            st.write(submit_form)

            if name and age and email and phone and gender:
                # add_user_info(id, name, age, email, phone, gender)
                st.success(id + "n" + age + "n" + email + "n" + phone + "n" + gender)
            else:
                st.warning("Please fill all the fields")

Here is the code.
Thank You!

Asked By: RAO.exe

||

Answers:

The problem is from the if st.button("Form"): not st.form_submit_button. Initialize a session state for the first button. Streamlit button has no callbacks, meaning all the entered data will be lost after user clicks on submit_form, because the page reruns.

Also take a look at age, st.number_input() will accept a float by default and that is not appropriate in your case. age should accept only an integer, the catch is to add value=0 as a second parameter to age = st.number_input(label="Age ") to convert the age input to an integer.

formbtn = st.button("Form")

if "formbtn_state" not in st.session_state:
    st.session_state.formbtn_state = False

if formbtn or st.session_state.formbtn_state:
    st.session_state.formbtn_state = True
    
    st.subheader("User Info Form")
    # name = st.text_input("Name")
    with st.form(key = 'user_info'):
        st.write('User Information')
    
        name = st.text_input(label="Name  ")
        age = st.number_input(label="Age  ", value=0)
        email = st.text_input(label="Email  ")
        phone = st.text_input(label="Phone  ")
        gender = st.radio("Gender  ", ("Male", "Female", "Prefer Not To Say"))
    
        submit_form = st.form_submit_button(label="Register", help="Click to register!")
    
        # Checking if all the fields are non empty
        if submit_form:
            st.write(submit_form)
    
            if name and age and email and phone and gender:
                # add_user_info(id, name, age, email, phone, gender)
                st.success(
                            f"ID:  n Name: {name}  n Age: {age}  n Email: {email}  n Phone: {phone}  n Gender: {gender}"
                        )
            else:
                st.warning("Please fill all the fields")

st.success(id + "n" + age + "n" + email + "n" + phone + "n" + gender) will throw TypeError: unsupported operand type(s) for +: 'float' and 'str'.

So I suggest you use f-string like I did.
Note: To write a new line in streamlit you need double-whitespace before n
OUTPUT:
enter image description here

Answered By: Jamiu Shaibu
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.