From 48c699496b67278b67788c0d46ffb2c0a851aa31 Mon Sep 17 00:00:00 2001 From: "Oleg.Rimko" Date: Mon, 16 Dec 2024 02:16:38 +0300 Subject: [PATCH] Support for running= callback argument. https://dash.plotly.com/advanced-callbacks\#updating-component-properties-when-a-callback-is-running --- django_plotly_dash/_callback.py | 10 ++++++++++ django_plotly_dash/dash_wrapper.py | 13 +++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/django_plotly_dash/_callback.py b/django_plotly_dash/_callback.py index 7e8da3d5..25e221aa 100644 --- a/django_plotly_dash/_callback.py +++ b/django_plotly_dash/_callback.py @@ -51,6 +51,15 @@ def register_callback( insert_output = flatten_grouping(output) multi = True + running = _kwargs.get("running") + if running is not None: + if not isinstance(running[0], (list, tuple)): + running = [running] + running = { + "running": {str(r[0]): r[1] for r in running}, + "runningOff": {str(r[0]): r[2] for r in running}, + } + output_indices = make_grouping_by_index(output, list(range(grouping_len(output)))) callback_id = insert_callback( callback_list, @@ -62,6 +71,7 @@ def register_callback( flat_state, inputs_state_indices, prevent_initial_call, + running=running, ) # pylint: disable=too-many-locals diff --git a/django_plotly_dash/dash_wrapper.py b/django_plotly_dash/dash_wrapper.py index e62c11bd..1c664481 100644 --- a/django_plotly_dash/dash_wrapper.py +++ b/django_plotly_dash/dash_wrapper.py @@ -339,6 +339,9 @@ def callback(self, *_args, **_kwargs): 'state': state, 'prevent_initial_call': prevent_initial_call} + if 'running' in _kwargs: + callback_set['running'] = _kwargs['running'] + def wrap_func(func): self._callback_sets.append((callback_set, func)) # add an expanded attribute to the function with the information to use in dispatch_with_args @@ -601,7 +604,7 @@ def _fix_callback_item(self, item): item.component_id = self._fix_id(item.component_id) return item - def callback(self, output, inputs, state, prevent_initial_call): + def callback(self, output, inputs, state, prevent_initial_call, running=None): 'Invoke callback, adjusting variable names as needed' if isinstance(output, (list, tuple)): @@ -609,10 +612,16 @@ def callback(self, output, inputs, state, prevent_initial_call): else: fixed_outputs = self._fix_callback_item(output) + if isinstance(running, list): + fixed_running = [(self._fix_callback_item(out), on, off) for out, on, off in running] + else: + fixed_running = running + return super().callback(fixed_outputs, [self._fix_callback_item(x) for x in inputs], [self._fix_callback_item(x) for x in state], - prevent_initial_call=prevent_initial_call) + prevent_initial_call=prevent_initial_call, + running=fixed_running) def clientside_callback(self, clientside_function, output, inputs, state, prevent_initial_call): # pylint: disable=dangerous-default-value 'Invoke callback, adjusting variable names as needed'