Hey folks! What’s up?
I’m trying to create a code where, when the user clicks the button, it disables for a short moment or until they leave the page.
I want to do this because I’m using the button to upload information to a database, and the double-click on the same record messes things up.
I’m trying to implement this logic, but it’s not working…
Note: I’m using the latest version of Streamlit and Python 3.11.6.
Hi @Patrick-Barbosa, you could probably deactivate the button for the time your other processing is taking place? Here’s some sample code:
import time as tm # just for testing / demo, to be deleted later
# create a session variable and set initial value to false
if "oneclick" not in st.session_state: st.session_state.oneclick = False
if "click_knt" not in st.session_state: st.session_state.click_knt = 0 # just for testing / demo, to be deleted later
def Callback():
st.session_state.click_knt += 1 # just for testing / demo, to be deleted later
st.session_state.oneclick = True
tm.sleep(3) # just for testing / demo, to be deleted later
# process your other code here...
st.session_state.oneclick = False
st.write(f"You clicked me: {st.session_state.click_knt} times") # just for testing / demo, to be deleted later
st.button("Click Me", on_click=Callback, disabled=st.session_state.oneclick)
Well, I tried that example, and it didn’t work either. I imagine that Streamlit has a dedicated thread only for the front-end, allowing the user to click even when another process is running. I tried with multiple clicks, and it always registers two or more clicks.
Facing the same issue here - trying different things for about a week now. I tried disabling the button via an on-click function call, but that still lets a second click come if if it gets there before the page refreshes. Also, from what I can see, the changes to the session state are not taken into consideration untill both executions/threads finish their respective tasks. I’m royally stumped.
My solution I hacked together to a similar problem.
I had to attach a volume disk so I could write out files.
At the beginning of the script, when the page first loads, I assign the person a random ID:
import string
import random
if "random_id" not in st.session_state:
FILE_LEN = 32
st.session_state["random_id"] = ''.join(random.choices(string.ascii_uppercase +string.digits, k=FILE_LEN))
Then, when I am about to send off my API call I check the following:
and if send_api is True, I make the API call. some_id is just some other unique ID i choose to write into the file. this should work with an empty file though. writing or creating a small file is so fast that no double click would be faster - also, Python locks accessing the file to a single thread at a time I believe - but dont quote me on that.
This is obviously imperfect, but it works. I also have a method that will remove any ID.txt files that are older than an hour to avoid worrying about space. One other downside is that as this is executing, you can still double click the button and if so the whole process takes even longer - but still only 1 API call is ever made (as far as my testing would indicate)
I am ready for someone to tell me this was stupid, but I would love to hear a better way.
Anyone find a solution? I’ve got a form submit that uses a callback function. If user double-clicks, form gets submitted twice.
The sample code provided above does not work.
Very annoying.
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.