-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Description
Checklist
- I have searched the existing issues for similar issues.
- I added a very descriptive title to this issue.
- I have provided sufficient information below to help reproduce this issue.
Summary
I'm trying to use buttons to trigger steps in my app, using on_click callbacks.
Errors sometimes occur within the callbacks, which I would like to handle in a unified way on the frontend (e.g. displaying the error as well as some debug information, like the current st.sessionstate).
However, I can't figure out how to catch errors that are triggered in callbacks, which seem to be executed outside the normal flow of the app.
I could put error handling inside each callback, or write a decorator, but it would be much more convenient and intuitive if I could put the widget in a try:... except:... block and handle errors that way.
Reproducible Code Example
import streamlit as st
def raise_error():
raise RuntimeError("This is an error")
try:
if st.button("Expected behavior"):
raise_error()
except Exception as e:
st.text("This works, and the error can be handled here")
try:
st.button("Undesired behavior", on_click=raise_error)
except Exception as e:
st.text("This block is never executed, and the callback instead triggers Streamlit's default error handler")
Steps To Reproduce
- An error is raised in a callback (e.g. a function passed to a button's on_click)
- The widget that holds the callback is wrapped in a try:... except:... block
- Trigger the widget's callback (e.g. by clicking the button)
- The try:... except:... block is not executed, and instead the error in the callback is handled by Streamlit's default error handler
Expected Behavior
The error is caught by the except:... block and can be handled there.
Current Behavior
The callback is executed outside of the flow of the user written code and triggers the default error handling, ignoring the try:... except:...
Is this a regression?
- Yes, this used to work in a previous version.
Debug info
- Streamlit version: 1.23.1
- Python version: 3.10.2
- Operating System: OSX
- Browser: Chrome
- Virtual environment: pyenv
Additional Information
No response
Are you willing to submit a PR?
- Yes, I am willing to submit a PR!