How do I enable for submit button after disabling?

Summary

I don’t want user to press button twice for some lengthy running job. I was able to disable the form submit button, however I am not able to enable it afterwards.

Steps to reproduce

def disable():
    st.session_state.disabled = True

def enable():
    if "disabled" in st.session_state and st.session_state.disabled == True:
        st.session_state.disabled = False
        print('true')

if "disabled" not in st.session_state:
    st.session_state.disabled = False
# Create the form
with st.form("my_form"):
    keyword = st.text_input('Product Name?')
    placeholder = st.empty()
    placeholder.text("")
    
    chk_scrap = st.checkbox('Fenix Scraping')
    chk_image = st.checkbox('Download Image')
    chk_fb = st.checkbox('Facebook')
    chk_caro = st.checkbox('Carousell')


    submitted = st.form_submit_button(
        "Submit", on_click=disable, disabled=st.session_state.disabled)



if submitted:    

    if chk_scrap:
        if keyword:
            try:
                Fenix.search_item(keyword)
            except Exception as e:
                st.warning(e)
        else:
            st.warning('Please enter product name!')

    if chk_image:
        if keyword:
            try:
                Fenix.get_image(keyword)
            except Exception as e:
                st.warning(e)
        else:
            st.warning('Please enter product name!')

    enable()

Seems like the placement of your enable function is the issue, it should be outside of the if submitted code
I tried running this and it works on my end

import time
import streamlit as st

def disable():
    st.session_state.disabled = True

def enable():
    if "disabled" in st.session_state and st.session_state.disabled == True:
        st.session_state.disabled = False

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

# Create the form
with st.form("my_form"):
    keyword = st.text_input('Product Name?')
    placeholder = st.empty()
    placeholder.text("")


    chk_scrap = st.checkbox('Fenix Scraping')
    chk_image = st.checkbox('Download Image')
    chk_fb = st.checkbox('Facebook')
    chk_caro = st.checkbox('Carousell')

    submitted = st.form_submit_button("Submit", on_click=disable, disabled=st.session_state.disabled)

if submitted:
    if chk_scrap:
        if keyword:
            try:
                # Simulating a lengthy running job
                time.sleep(5)
                st.success("Fenix Scraping completed successfully")
            except Exception as e:
                st.warning(e)
        else:
            st.warning('Please enter product name!')

    if chk_image:
        if keyword:
            try:
                # Simulating a lengthy running job
                time.sleep(5)
                st.success("Image download completed successfully")
            except Exception as e:
                st.warning(e)
        else:
            st.warning('Please enter product name!')

# Enable the submit button after the jobs have completed
enable()

Hope it helps!

thank you for your help!

However, it is still not working my end.

Not sure what 's wrong here.

You won’t see the change until the next rerun, but there won’t be a rerun until you interact with the UI. Call st.experimental_rerun() after enabling the button to force a rerun.

1 Like

i already clicked rerun , clear cache and even put your code to another file, still not working.

may i know what ver you are using, i am using streamlit==1.23.1

Just to be clear, the st.experimental_rerun() that @Goyo mentioned is a function–a line of code to add to your script.

i see what you mean now, it works now, thank you for the explanation.
@ Goyo, thank you so much

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.