Skip to content

Commit a06cf2d

Browse files
gianlucasalvatoGibbsConsulting
authored andcommitted
Add support for dash client-side callbacks (#149) (#165)
This should close issue #149. This is a purely additive change. Adding support for client-side callbacks is just a matter of registering a javascript function name. The client-side callback may be registered by calling the `clientside_callback` function on a `DjangoDash` object in the same exact way as it would have been done with a plain Dash app (see: https://community.plot.ly/t/dash-0-41-0-released/22131).
1 parent 48ce8b5 commit a06cf2d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

django_plotly_dash/dash_wrapper.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def __init__(self, name=None, serve_locally=None,
100100
self._uid = name
101101
self.layout = None
102102
self._callback_sets = []
103+
self._clientside_callback_sets = []
103104

104105
self.css = Holder()
105106
self.scripts = Holder()
@@ -201,6 +202,8 @@ def form_dash_instance(self, replacements=None, ndid=None, base_pathname=None):
201202

202203
for cb, func in self._callback_sets:
203204
rd.callback(**cb)(func)
205+
for cb in self._clientside_callback_sets:
206+
rd.clientside_callback(**cb)
204207
for s in self.css.items:
205208
rd.css.append_css(s)
206209
for s in self.scripts.items:
@@ -229,6 +232,16 @@ def expanded_callback(self, output, inputs=[], state=[], events=[]): # pylint: d
229232
self._expanded_callbacks = True
230233
return self.callback(output, inputs, state, events)
231234

235+
def clientside_callback(self, clientside_function, output, inputs=None, state=None):
236+
'Form a callback function by wrapping, in the same way as the underlying Dash application would'
237+
callback_set = { 'clientside_function': clientside_function,
238+
'output': output,
239+
'inputs': inputs and inputs or dict(),
240+
'state': state and state or dict() }
241+
242+
self._clientside_callback_sets.append(callback_set)
243+
244+
232245
def get_asset_url(self, asset_name):
233246
'''URL of an asset associated with this component
234247
@@ -449,6 +462,21 @@ def callback(self, output, inputs=[], state=[], events=[]): # pylint: disable=da
449462
[self._fix_callback_item(x) for x in inputs],
450463
[self._fix_callback_item(x) for x in state])
451464

465+
def clientside_callback(self, clientside_function, output, inputs=[], state=[]): # pylint: disable=dangerous-default-value
466+
'Invoke callback, adjusting variable names as needed'
467+
468+
if isinstance(output, (list, tuple)):
469+
fixed_outputs = [self._fix_callback_item(x) for x in output]
470+
# Temporary check; can be removed once the library has been extended
471+
raise NotImplementedError("django-plotly-dash cannot handle multiple callback outputs at present")
472+
else:
473+
fixed_outputs = self._fix_callback_item(output)
474+
475+
return super(WrappedDash, self).clientside_callback(clientside_function,
476+
fixed_outputs,
477+
[self._fix_callback_item(x) for x in inputs],
478+
[self._fix_callback_item(x) for x in state])
479+
452480
def dispatch(self):
453481
'Perform dispatch, using request embedded within flask global state'
454482
import flask

0 commit comments

Comments
 (0)