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.)