Skip to content

For deferred download with st.download_button, pipe errors to the script thread #13466

@sfc-gh-dmatthews

Description

@sfc-gh-dmatthews

Checklist

  • I have searched the existing issues for similar feature requests.
  • I added a descriptive title and summary to this issue.

Summary

If you use deferred download with st.download_button, there should be a way to catch errors that may be raised by the deferred generation function. Currently, if the funciton encounters an error, a warning message is displayed on the frontend and isn't accessible on the backend.

Why?

When you use deferred download with st.download_button, the function which returns the download runs on its own thread so it won't block the script thread. This has two consequences:

  1. There is no script context, so you can't use any Streamlit command, like st.session_state. (See Access Session State in st.download_button callable #13337).
  2. You can't handle errors raised by the deferred download function.

You can put try-catch blocks in your download function to avoid an unhandled error, but you can't really do anything about it because you can't display a custom message or log something in Session State. (Maybe you could hack a solution by writing something to a temporary file on the server, but that doesn't seem ideal.)

How?

A couple different solutions:

  • Have the download button return an error if encountered so it can be caught in the main script.
  • Give the download thread some (possibly restricted) script context (like Access Session State in st.download_button callable #13337).
  • Improve multithreading support more generally, and have some hook at the end of a script run to catch a session's child threads, like the download thread.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature:st.download_buttonRelated to the `st.download_button` widgettype:enhancementRequests for feature enhancements or new features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions