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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.