Skip to content

Commit

Permalink
Add handling of PreventUpdate exceptions (#208)
Browse files Browse the repository at this point in the history
* Handle the raising of dash.exceptions.PreventUpdate

* Missing demo file and minor formatting tweaks

* Add in missing copyright notice
  • Loading branch information
delsim authored and GibbsConsulting committed Dec 4, 2019
1 parent f5369ea commit ae0ddd4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
Empty file.
13 changes: 8 additions & 5 deletions demo/demo/plotly_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

import dpd_components as dpd

from dash.exceptions import PreventUpdate

from django_plotly_dash import DjangoDash
from django_plotly_dash.consumers import send_to_pipe_channel

Expand Down Expand Up @@ -336,8 +338,8 @@ def callback_show_timeseries(internal_state_string, state_uid, **kwargs):
dash.dependencies.Output('output-two', 'children'),
dash.dependencies.Output('output-three', 'children')
],
[dash.dependencies.Input('button','n_clicks'),
dash.dependencies.Input('dropdown-color','value'),
[dash.dependencies.Input('button', 'n_clicks'),
dash.dependencies.Input('dropdown-color', 'value'),
])
def multiple_callbacks_one(button_clicks, color_choice):
return ("Output 1: %s %s" % (button_clicks, color_choice),
Expand Down Expand Up @@ -365,12 +367,13 @@ def multiple_callbacks_one(button_clicks, color_choice):
dash.dependencies.Output('output-two', 'children'),
dash.dependencies.Output('output-three', 'children')
],
[dash.dependencies.Input('button','n_clicks'),
dash.dependencies.Input('dropdown-color','value'),
[dash.dependencies.Input('button', 'n_clicks'),
dash.dependencies.Input('dropdown-color', 'value'),
])
def multiple_callbacks_two(button_clicks, color_choice, **kwargs):
if color_choice == 'green':
raise PreventUpdate
return ["Output 1: %s %s" % (button_clicks, color_choice),
"Output 2: %s %s" % (button_clicks, color_choice),
"Output 3: %s %s [%s]" % (button_clicks, color_choice, kwargs)
]

5 changes: 5 additions & 0 deletions demo/demo/templates/demo_ten.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ <h1>Multiple Callback Values</h1>
Both standard and extended callbacks can return multiple responses. This example instantiates
two example applications that use multiple return values for both types of callback.
</p>
<p>
Note that the second application will not update if the color choice is set to Green. This is
intentional; the callback raises a <b>dash.exceptions.PreventUpdate</b> exception when this
color choice is in effect.
</p>
<div class="card bg-light border-dark">
<div class="card-body">
<p><span>{</span>% load plotly_dash %}</p>
Expand Down
21 changes: 21 additions & 0 deletions demo/demo/tests/test_dpd_demo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
'''
Test django_plotly_dash functionality by use of demo pages
Copyright (c) 2018 Gibbs Consulting and others - see CONTRIBUTIONS.md
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''

import pytest
Expand Down
8 changes: 8 additions & 0 deletions django_plotly_dash/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect

from dash.exceptions import PreventUpdate

try:
from dash.fingerprint import check_fingerprint
except:
Expand Down Expand Up @@ -68,6 +70,12 @@ def layout(request, ident, stateless=False, cache_id=None, **kwargs):
content_type=mimetype)

def update(request, ident, stateless=False, **kwargs):
try:
return _update(request, ident, stateless, **kwargs)
except PreventUpdate:
return HttpResponse(status=204)

def _update(request, ident, stateless=False, **kwargs):
'Generate update json response'
dash_app, app = DashApp.locate_item(ident, stateless)

Expand Down

0 comments on commit ae0ddd4

Please sign in to comment.