Skip to content

added PreventUpdate exception and attached handler to Flask server #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions dash/dash.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import print_function

import sys
import collections
import importlib
import json
Expand Down Expand Up @@ -53,6 +56,12 @@ def __init__(
# gzip
Compress(self.server)

@self.server.errorhandler(exceptions.PreventUpdate)
def _handle_error(error):
"""Handle a halted callback and return an empty 204 response"""
print(error, file=sys.stderr)
return ('', 204)

# static files from the packages
self.css = Css()
self.scripts = Scripts()
Expand Down
4 changes: 4 additions & 0 deletions dash/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ class IDsCantContainPeriods(CallbackException):

class CantHaveMultipleOutputs(CallbackException):
pass


class PreventUpdate(CallbackException):
pass
48 changes: 48 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dash_core_components as dcc
from dash import Dash
from dash.dependencies import Input, Output
from dash.exceptions import PreventUpdate
from .IntegrationTests import IntegrationTests
from .utils import assert_clean_console, invincible, wait_for

Expand Down Expand Up @@ -64,3 +65,50 @@ def update_output(value):
)

assert_clean_console(self)

def test_aborted_callback(self):
"""Raising PreventUpdate prevents update and triggering dependencies"""

initial_input = 'initial input'
initial_output = 'initial output'

app = Dash(__name__)
app.layout = html.Div([
dcc.Input(id='input', value=initial_input),
html.Div(initial_output, id='output1'),
html.Div(initial_output, id='output2'),
])

callback1_count = Value('i', 0)
callback2_count = Value('i', 0)

@app.callback(Output('output1', 'children'), [Input('input', 'value')])
def callback1(value):
callback1_count.value = callback1_count.value + 1
raise PreventUpdate("testing callback does not update")
return value

@app.callback(Output('output2', 'children'), [Input('output1', 'children')])
def callback2(value):
callback2_count.value = callback2_count.value + 1
return value

self.startServer(app)

input_ = self.wait_for_element_by_id('input')
input_.clear()
input_.send_keys('x')
output1 = self.wait_for_element_by_id('output1')
output2 = self.wait_for_element_by_id('output2')

# callback1 runs twice (initial page load and through send_keys)
self.assertEqual(callback1_count.value, 2)

# callback2 is never triggered, even on initial load
self.assertEqual(callback2_count.value, 0)

# double check that output1 and output2 children were not updated
self.assertEqual(output1.text, initial_output)
self.assertEqual(output2.text, initial_output)

assert_clean_console(self)