Is there a way to disable st.form_submit_button after input?

Hi @Ronnie_Nolan,

Welcome to the Streamlit Community! :wave: :partying_face:

The st.form_submit_button docs say the command has two relevant parameters:

  • on_click (callable): An optional callback invoked when this button is clicked.
  • disabled (bool): An optional boolean, which disables the button if set to True. The default is False. This argument can only be supplied by keyword.

We can make use of both of them to achieve the behavior you want. In this example:

  1. We first initialize a session state variable called disabled to False and pass that to the form submit button’s disabled parameter.
  2. Next, we define a function disable() that sets st.session_state.disabled to True.
  3. Pass the disable function to the on_click parameter of the form submit button, so that when the button is clicked, the disable() function is executed, and that in-turn disables the button

Code

import streamlit as st

# Disable the submit button after it is clicked
def disable():
    st.session_state.disabled = True

# Initialize disabled for form_submit_button to False
if "disabled" not in st.session_state:
    st.session_state.disabled = False

with st.form("myform"):
    # Assign a key to the widget so it's automatically in session state
    name = st.text_input("Enter your name below:", key="name")
    submit_button = st.form_submit_button(
        "Submit", on_click=disable, disabled=st.session_state.disabled
    )

    if submit_button:
        st.info("You have entered: " + st.session_state.name)

Output

disable-form-submit-button

Happy Streamlit-ing! :balloon:
Snehan

1 Like