Skip to content

Commit ae0ddd4

Browse files
delsimGibbsConsulting
authored andcommitted
Add handling of PreventUpdate exceptions (#208)
* Handle the raising of dash.exceptions.PreventUpdate * Missing demo file and minor formatting tweaks * Add in missing copyright notice
1 parent f5369ea commit ae0ddd4

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

demo/demo/assets/example_file_to_copy

Whitespace-only changes.

demo/demo/plotly_apps.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141

4242
import dpd_components as dpd
4343

44+
from dash.exceptions import PreventUpdate
45+
4446
from django_plotly_dash import DjangoDash
4547
from django_plotly_dash.consumers import send_to_pipe_channel
4648

@@ -336,8 +338,8 @@ def callback_show_timeseries(internal_state_string, state_uid, **kwargs):
336338
dash.dependencies.Output('output-two', 'children'),
337339
dash.dependencies.Output('output-three', 'children')
338340
],
339-
[dash.dependencies.Input('button','n_clicks'),
340-
dash.dependencies.Input('dropdown-color','value'),
341+
[dash.dependencies.Input('button', 'n_clicks'),
342+
dash.dependencies.Input('dropdown-color', 'value'),
341343
])
342344
def multiple_callbacks_one(button_clicks, color_choice):
343345
return ("Output 1: %s %s" % (button_clicks, color_choice),
@@ -365,12 +367,13 @@ def multiple_callbacks_one(button_clicks, color_choice):
365367
dash.dependencies.Output('output-two', 'children'),
366368
dash.dependencies.Output('output-three', 'children')
367369
],
368-
[dash.dependencies.Input('button','n_clicks'),
369-
dash.dependencies.Input('dropdown-color','value'),
370+
[dash.dependencies.Input('button', 'n_clicks'),
371+
dash.dependencies.Input('dropdown-color', 'value'),
370372
])
371373
def multiple_callbacks_two(button_clicks, color_choice, **kwargs):
374+
if color_choice == 'green':
375+
raise PreventUpdate
372376
return ["Output 1: %s %s" % (button_clicks, color_choice),
373377
"Output 2: %s %s" % (button_clicks, color_choice),
374378
"Output 3: %s %s [%s]" % (button_clicks, color_choice, kwargs)
375379
]
376-

demo/demo/templates/demo_ten.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ <h1>Multiple Callback Values</h1>
99
Both standard and extended callbacks can return multiple responses. This example instantiates
1010
two example applications that use multiple return values for both types of callback.
1111
</p>
12+
<p>
13+
Note that the second application will not update if the color choice is set to Green. This is
14+
intentional; the callback raises a <b>dash.exceptions.PreventUpdate</b> exception when this
15+
color choice is in effect.
16+
</p>
1217
<div class="card bg-light border-dark">
1318
<div class="card-body">
1419
<p><span>{</span>% load plotly_dash %}</p>

demo/demo/tests/test_dpd_demo.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
'''
22
Test django_plotly_dash functionality by use of demo pages
3+
4+
Copyright (c) 2018 Gibbs Consulting and others - see CONTRIBUTIONS.md
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
324
'''
425

526
import pytest

django_plotly_dash/views.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
from django.http import HttpResponse, HttpResponseRedirect
3030
from django.shortcuts import redirect
3131

32+
from dash.exceptions import PreventUpdate
33+
3234
try:
3335
from dash.fingerprint import check_fingerprint
3436
except:
@@ -68,6 +70,12 @@ def layout(request, ident, stateless=False, cache_id=None, **kwargs):
6870
content_type=mimetype)
6971

7072
def update(request, ident, stateless=False, **kwargs):
73+
try:
74+
return _update(request, ident, stateless, **kwargs)
75+
except PreventUpdate:
76+
return HttpResponse(status=204)
77+
78+
def _update(request, ident, stateless=False, **kwargs):
7179
'Generate update json response'
7280
dash_app, app = DashApp.locate_item(ident, stateless)
7381

0 commit comments

Comments
 (0)