Hello team , I have a streamlit app , when the user enters a prompt , I render 2 buttons for him to click , button used is st.button .
Now , when the button is clicked , I am making multiple api calls and this process takes time ( ~10 seconds ) .
I want to disable the button as soon as the user clicks on it .
you can use the property of st.button() called “disabled”, I suggest you to assign to the disabled property a st.session_state.* boolean variable to make a sort of on/off
Hi @Jeremy_Sapienza , I’m using a session state variable for disabling the button and the state is getting updated but the button is not getting disabled at the moment I click the button . I want the button to become disabled as soon as I click it . But my button is behaving differently .
I’ve attached code snippet below .
def handle_button_click():
print("button's state before clicking :",st.session_state.button_clicked.get(f"offer_{idx}",False))
st.session_state.button_clicked[f"offer_{idx}"] = True
print("button's state after clicking :",st.session_state.button_clicked[f"offer_{idx}"])
url = multipleAPICallingFunction("call Function")
print("url here :",url)
def buttonWasClicked(idx):
# Check if the button was clicked
val = st.session_state.button_clicked.get(f"offer_{idx}", False)
print("is button clicked",val)
return val
# Create a button and disable it if it was clicked
st.button(
f"Click me (offer {idx})",
on_click=handle_button_click,
disabled=buttonWasClicked(idx)
)
Inside handle_button_click function , there is another function multipleAPICallingFunction which is getting called . this multipleAPICallingFunction further calls 4 apis ( REST apis and blockchain rpc api calls as well ) .
import streamlit as st
import time
import streamlit as st
import time
# if 'run_button' in st.session_state and st.session_state.run_button == True:
# st.session_state.running = True
# else:
# st.session_state.running = False
if st.button('Do a thing', disabled=st.session_state.get("run_button", False), key='run_button'):
status = st.progress(0)
for t in range(10):
time.sleep(.2)
status.progress(10*t+10)
st.session_state.output = 'Selenium Output'
st.rerun()
if 'output' in st.session_state:
st.write(st.session_state.output)
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.