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()
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.