-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2903 from plotly/feat/on-error
Add callback on_error handler
- Loading branch information
Showing
9 changed files
with
197 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from dash import Dash, html, Input, Output, set_props | ||
|
||
|
||
def test_cber001_error_handler(dash_duo): | ||
def global_callback_error_handler(err): | ||
set_props("output-global", {"children": f"global: {err}"}) | ||
|
||
app = Dash(on_error=global_callback_error_handler) | ||
|
||
app.layout = [ | ||
html.Button("start", id="start-local"), | ||
html.Button("start-global", id="start-global"), | ||
html.Div(id="output"), | ||
html.Div(id="output-global"), | ||
html.Div(id="error-message"), | ||
] | ||
|
||
def on_callback_error(err): | ||
set_props("error-message", {"children": f"message: {err}"}) | ||
return f"callback: {err}" | ||
|
||
@app.callback( | ||
Output("output", "children"), | ||
Input("start-local", "n_clicks"), | ||
on_error=on_callback_error, | ||
prevent_initial_call=True, | ||
) | ||
def on_start(_): | ||
raise Exception("local error") | ||
|
||
@app.callback( | ||
Output("output-global", "children"), | ||
Input("start-global", "n_clicks"), | ||
prevent_initial_call=True, | ||
) | ||
def on_start_global(_): | ||
raise Exception("global error") | ||
|
||
dash_duo.start_server(app) | ||
dash_duo.find_element("#start-local").click() | ||
|
||
dash_duo.wait_for_text_to_equal("#output", "callback: local error") | ||
dash_duo.wait_for_text_to_equal("#error-message", "message: local error") | ||
|
||
dash_duo.find_element("#start-global").click() | ||
dash_duo.wait_for_text_to_equal("#output-global", "global: global error") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from dash import Dash, Input, Output, html, set_props | ||
from tests.integration.long_callback.utils import get_long_callback_manager | ||
|
||
long_callback_manager = get_long_callback_manager() | ||
handle = long_callback_manager.handle | ||
|
||
|
||
def global_error_handler(err): | ||
set_props("global-output", {"children": f"global: {err}"}) | ||
|
||
|
||
app = Dash( | ||
__name__, long_callback_manager=long_callback_manager, on_error=global_error_handler | ||
) | ||
|
||
app.layout = [ | ||
html.Button("callback on_error", id="start-cb-onerror"), | ||
html.Div(id="cb-output"), | ||
html.Button("global on_error", id="start-global-onerror"), | ||
html.Div(id="global-output"), | ||
] | ||
|
||
|
||
def callback_on_error(err): | ||
set_props("cb-output", {"children": f"callback: {err}"}) | ||
|
||
|
||
@app.callback( | ||
Output("cb-output", "children"), | ||
Input("start-cb-onerror", "n_clicks"), | ||
prevent_initial_call=True, | ||
background=True, | ||
on_error=callback_on_error, | ||
) | ||
def on_click(_): | ||
raise Exception("callback error") | ||
|
||
|
||
@app.callback( | ||
Output("global-output", "children"), | ||
Input("start-global-onerror", "n_clicks"), | ||
prevent_initial_call=True, | ||
background=True, | ||
) | ||
def on_click_global(_): | ||
raise Exception("global error") | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(debug=True) |
Oops, something went wrong.