I’ve got a nice running Streamlit app, and i’m using it to submit data (via the Uploader) to a backend Flask API. However, most times this Flask API can take 30 seconds to 10 minutes to complete its processing! After I submit the Uploaded file, the UI freezes until complete.
There more complicated way i suppose I could solve this is workers on the backend which could provide a response quickly saying “processing” back to Streamlit. Another approach (which is why i’m asking this question), is there a way within Streamlit to make that asyn call? Here is my code:
def post_submission(payload: dict):
api_url = f"api_url_here"
# it can take 10 min to get a response!
response = requests.post(api_url, json=payload)
return response
response = post_submission(payload)
if response.status_code == 200:
st.success("Data successfully submitted")
else:
st.warning("api is probably down")
st.write(response)
st.write(response.status_code)
As you imagined, this is a bit of a difficult scenario with the Streamlit execution model. If the UI refreshes, we start from the top and re-run the script, so as you mentioned, we’d have to have a secondary thread running to deal with the API upload. But then you’d also have to consider what the behavior should be if a user were to upload a second call.
@ari - this is a tricky one, yeah. You can make your upload call asynchronous by spinning up a new thread and calling post_submission from within that thread.
The problem you’d run into there, though, is in telling Streamlit “ok, this async call is complete - re-run the script so we can report the status to the user.” There is no way to do this within Streamlit; Streamlit will only re-run your app script when the user interacts with a piece of UI.
I’d love to have some st.rerun_script_for_user API, but it doesn’t currently exist
(Edit: I’ve created a feature request so we can get this on our radar.)
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.