Skip to content

[wip] previous state support #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import dash_renderer

from .dependencies import Event, Input, Output, State
from .dependencies import Event, Input, Output, State, PrevInput, PrevState
from .resources import Scripts, Css
from .development.base_component import Component
from . import exceptions
Expand Down Expand Up @@ -481,7 +481,14 @@ def _validate_callback(self, output, inputs, state, events):
# the dropdown.
# TODO - Check this map for recursive or other ill-defined non-tree
# relationships
def callback(self, output, inputs=[], state=[], events=[]):
def callback(
self,
output,
inputs=[],
state=[],
events=[],
prev_state=[],
prev_inputs=[]):
self._validate_callback(output, inputs, state, events)

callback_id = '{}.{}'.format(
Expand All @@ -499,6 +506,14 @@ def callback(self, output, inputs=[], state=[], events=[]):
'events': [
{'id': c.component_id, 'event': c.component_event}
for c in events
],
'prev_inputs': [
{'id': c.component_id, 'property': c.component_property}
for c in prev_inputs
],
'prev_state': [
{'id': c.component_id, 'property': c.component_property}
for c in prev_state
]
}

Expand Down Expand Up @@ -530,23 +545,28 @@ def dispatch(self):
body = flask.request.get_json()
inputs = body.get('inputs', [])
state = body.get('state', [])
prev_inputs = body.get('prevInputs', [])
prev_state = body.get('prevState', [])
output = body['output']

target_id = '{}.{}'.format(output['id'], output['property'])
args = []
for component_registration in self.callback_map[target_id]['inputs']:
args.append([
c.get('value', None) for c in inputs if
c['property'] == component_registration['property'] and
c['id'] == component_registration['id']
][0])

for component_registration in self.callback_map[target_id]['state']:
args.append([
c.get('value', None) for c in state if
c['property'] == component_registration['property'] and
c['id'] == component_registration['id']
][0])
for types in [
['inputs', inputs],
['state', state],
['prev_inputs', prev_inputs],
['prev_state', prev_state]]:
for component_registration in self.callback_map[target_id][types[0]]:
try:
args.append([
c.get('value', None) for c in types[1] if
c['property'] == component_registration['property'] and
c['id'] == component_registration['id']
][0])
except Exception as e:
print(types)
print(component_registration)
print(e)

return self.callback_map[target_id]['callback'](*args)

Expand Down
12 changes: 12 additions & 0 deletions dash/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,15 @@ class Event:
def __init__(self, component_id, component_event):
self.component_id = component_id
self.component_event = component_event


class PrevInput:
def __init__(self, component_id, component_property):
self.component_id = component_id
self.component_property = component_property


class PrevState:
def __init__(self, component_id, component_property):
self.component_id = component_id
self.component_property = component_property