Skip to content

Commit

Permalink
wrap callbacks with triggers and filter them out
Browse files Browse the repository at this point in the history
  • Loading branch information
frnhr committed Mar 27, 2024
1 parent 1e70fed commit c57f06e
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions dash_extensions/enrich.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import sys
import threading
import uuid
from itertools import compress

import plotly
import dash

Expand Down Expand Up @@ -952,8 +954,6 @@ def __init__(self, component_id, component_property):

class TriggerTransform(DashTransform):

# NOTE: This transform cannot be implemented for clientside callbacks since the JS can't be modified from here.

def apply_serverside(self, callbacks):
for callback in callbacks:
is_trigger = [isinstance(item, Trigger) for item in callback.inputs]
Expand All @@ -965,6 +965,24 @@ def apply_serverside(self, callbacks):
callback.f = filter_args(is_trigger)(f)
return callbacks

def apply_clientside(self, callbacks):
for callback in callbacks:
is_not_trigger = [not isinstance(item, Trigger) for item in callback.inputs]
# Check if any triggers are there.
if all(is_not_trigger):
continue
# If so, filter the callback args.
args = [f"arg{i}" for i in range(len(callback.inputs))]
filtered_args = compress(args, is_not_trigger)
if isinstance(callback.f, ClientsideFunction):
callback.f = f"window.dash_clientside['{callback.f.namespace}']['{callback.f.function_name}']"
callback.f = f"""
function({", ".join(args)}) {{
const func = {callback.f};
return func({", ".join(filtered_args)});
}}"""
return callbacks


def filter_args(args_filter):
def wrapper(f):
Expand Down

0 comments on commit c57f06e

Please sign in to comment.