Hey Moiz! I think the st.session_state
gets us most of the way there, but I’m having trouble understanding how it functions (the initial reason I thought it was not applicable).
Take the following code:
import time
import streamlit as st
def dummy_upload():
key = "can_upload"
if st.session_state.get(key, True):
st.session_state[key] = False
print("Uploading!")
time.sleep(2)
st.session_state[key] = True
print("Upload done!")
if __name__ == "__main__":
with st.form("uploader"):
submit = st.form_submit_button("Upload now")
if submit:
dummy_upload()
print(st.session_state)
If I click, wait for the “Upload Done!” message, and then click again, it works great. If I double click, it stops the second flick from doing the fake upload. But for some reason I’m unsure about, if the user double clicks, it resets the session state.
Here’s the output from the prints if I do a “Click, wait enough, Click, wait enough, spam click”
Uploading!
Upload done!
{'can_upload': True, 'FormSubmitter:uploader-Upload now': True}
Uploading!
Upload done!
{'can_upload': True, 'FormSubmitter:uploader-Upload now': True}
Uploading!
{'can_upload': False, 'FormSubmitter:uploader-Upload now': True}
{'can_upload': False, 'FormSubmitter:uploader-Upload now': True}
{'can_upload': False, 'FormSubmitter:uploader-Upload now': True}
{'can_upload': False, 'FormSubmitter:uploader-Upload now': True}
Upload done!
{}
{'can_upload': False, 'FormSubmitter:uploader-Upload now': True}
For some reason the session state can_upload
state is not being reset to true, as if the second click causes a copy of the state and then the original “Set it back to true” line has no effect.
Any ideas how to get around this?