Skip to content

Commit 8332282

Browse files
author
Mark Gibbs
committed
Refactoring dispatch for callback enhancements
1 parent 5a59976 commit 8332282

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

django_plotly_dash/dash_wrapper.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ def __init__(self, name_root, app_pathname, **kwargs):
124124
nd_apps[self._uid] = self
125125

126126
self._adjust_id = False
127+
self._dash_dispatch = False
128+
129+
def use_dash_dispatch(self):
130+
# TODO make this be a function of using kwargs in callbacks
131+
return self._dash_dispatch
127132

128133
def flask_app(self):
129134
return self._flask_app
@@ -181,3 +186,32 @@ def callback(self, output, inputs=[], state=[], events=[]):
181186
[self._fix_callback_item(x) for x in state],
182187
[self._fix_callback_item(x) for x in events])
183188

189+
def dispatch(self):
190+
import flask
191+
body = flask.request.get_json()
192+
return self. dispatch_with_args(body, argMap=dict())
193+
194+
def dispatch_with_args(self, body, argMap):
195+
inputs = body.get('inputs', [])
196+
state = body.get('state', [])
197+
output = body['output']
198+
199+
target_id = '{}.{}'.format(output['id'], output['property'])
200+
args = []
201+
for component_registration in self.callback_map[target_id]['inputs']:
202+
args.append([
203+
c.get('value', None) for c in inputs if
204+
c['property'] == component_registration['property'] and
205+
c['id'] == component_registration['id']
206+
][0])
207+
208+
for component_registration in self.callback_map[target_id]['state']:
209+
args.append([
210+
c.get('value', None) for c in state if
211+
c['property'] == component_registration['property'] and
212+
c['id'] == component_registration['id']
213+
][0])
214+
215+
return self.callback_map[target_id]['callback'](*args,**argMap)
216+
217+

django_plotly_dash/views.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from django.shortcuts import render
22

3-
import flask
43
import json
54

65
from .dash_wrapper import get_app_instance_by_id, get_or_form_app
@@ -26,13 +25,22 @@ def layout(request, id, **kwargs):
2625

2726
def update(request, id, **kwargs):
2827
app = get_app_instance_by_id(id)
29-
mFunc = app.locate_endpoint_function('dash-update-component')
30-
# Fudge request object
3128
rb = json.loads(request.body.decode('utf-8'))
32-
with app.test_request_context():
33-
# inputs state and output needed in the json objects
34-
flask.request._cached_json = (rb, flask.request._cached_json[True])
35-
resp = mFunc()
29+
30+
if app.use_dash_dispatch():
31+
# Force call through dash
32+
mFunc = app.locate_endpoint_function('dash-update-component')
33+
34+
import flask
35+
with app.test_request_context():
36+
# Fudge request object
37+
flask.request._cached_json = (rb, flask.request._cached_json[True])
38+
resp = mFunc()
39+
else:
40+
# Use direct dispatch
41+
argMap = {}
42+
resp = app.dispatch_with_args(rb, argMap)
43+
3644
return HttpResponse(resp.data,
3745
content_type=resp.mimetype)
3846

0 commit comments

Comments
 (0)