Skip to content

Commit

Permalink
support pattern matching (#307)
Browse files Browse the repository at this point in the history
* support for component with pattern as ids

* add a NotImplementedError for the pattern match case when updating current state (todo: implement it)

Co-authored-by: GFJ138 <sebastien.dementen@engie.com>
  • Loading branch information
sdementen and sebastiendementen committed Jan 24, 2021
1 parent b2d160f commit 221c29c
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions django_plotly_dash/dash_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,15 @@ def run(self, *args, **kwargs):
def register_blueprint(self, *args, **kwargs):
pass


def compare(id_python, id_dash):
"""Compare an id of a dash component as a python object with an id of a component
in dash syntax. It handles both id as str or as dict (pattern-matching)"""
if isinstance(id_python, dict):
return "{" in id_dash and id_python == json.loads(id_dash)
return id_python == id_dash


class WrappedDash(Dash):
'Wrapper around the Plotly Dash application instance'
# pylint: disable=too-many-arguments, too-many-instance-attributes
Expand Down Expand Up @@ -436,10 +445,16 @@ def augment_initial_layout(self, base_response, initial_arguments=None):
if initial_arguments:
if isinstance(initial_arguments, str):
initial_arguments = json.loads(initial_arguments)
else:
initial_arguments = {}

# Define overrides as self._replacements updated with initial_arguments
overrides = dict(self._replacements)
overrides.update(initial_arguments)

# Walk tree. If at any point we have an element whose id
# matches, then replace any named values at this level
reworked_data = self.walk_tree_and_replace(baseData, initial_arguments)
reworked_data = self.walk_tree_and_replace(baseData, overrides)

response_data = json.dumps(reworked_data,
cls=PlotlyJSONEncoder)
Expand Down Expand Up @@ -473,10 +488,15 @@ def walk_tree_and_replace(self, data, overrides):
replacements = {}
# look for id entry
thisID = data.get('id', None)
if thisID is not None:
replacements = overrides.get(thisID, None) if overrides else None
if not replacements:
replacements = self._replacements.get(thisID, {})
if isinstance(thisID, dict):
# handle case of thisID being a dict (pattern) => linear search in overrides dict
for k, v in overrides.items():
if compare(id_python=thisID, id_dash=k):
replacements = v
break
elif thisID is not None:
# handle standard case of string thisID => key lookup
replacements = overrides.get(thisID, {})
# walk all keys and replace if needed
for k, v in data.items():
r = replacements.get(k, None)
Expand Down Expand Up @@ -628,15 +648,15 @@ def dispatch_with_args(self, body, argMap):

for component_registration in callback_info['inputs']:
for c in inputs:
if c['property'] == component_registration['property'] and c['id'] == component_registration['id']:
if c['property'] == component_registration['property'] and compare(id_python=c['id'],id_dash=component_registration['id']):
v = c.get('value', None)
args.append(v)
if da:
da.update_current_state(c['id'], c['property'], v)

for component_registration in callback_info['state']:
for c in states:
if c['property'] == component_registration['property'] and c['id'] == component_registration['id']:
if c['property'] == component_registration['property'] and compare(id_python=c['id'],id_dash=component_registration['id']):
v = c.get('value', None)
args.append(v)
if da:
Expand Down Expand Up @@ -670,6 +690,9 @@ def dispatch_with_args(self, body, argMap):
if da.have_current_state_entry(output_id, output_property):
value = root_value.get(output_id,{}).get(output_property, None)
da.update_current_state(output_id, output_property, value)
else:
# todo: implement saving of state for pattern matching ouputs
raise NotImplementedError("Updating state for dict keys (pattern matching) is not yet implemented")

return res

Expand Down

0 comments on commit 221c29c

Please sign in to comment.