Skip to content

Commit

Permalink
Made bokeh callbacks more general
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Mar 20, 2017
1 parent 7a93447 commit e97ec94
Showing 1 changed file with 40 additions and 15 deletions.
55 changes: 40 additions & 15 deletions holoviews/plotting/bokeh/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,17 @@ class Callback(object):
# The plotting handle(s) to attach the JS callback on
handles = []

# Additional handles available to the callback
extra_handles = []

# Conditions when callback should be skipped
skip_conditions = []

# Callback will listen to events of the supplied type on the handles
event = None
events = []

# Callback will listen to changes to this attribute on the handles
change = None
# List of attributes on the handles to listen to
change = []

_comm_type = JupyterCommJS

Expand All @@ -193,6 +199,7 @@ def __init__(self, plot, streams, source, **params):
self.comm = self._comm_type(plot, on_msg=self.on_msg)
self.source = source
self.handle_ids = defaultdict(list)
self.callbacks = []


def initialize(self):
Expand All @@ -212,7 +219,14 @@ def initialize(self):
'attach %s callback' % warn_args)
continue
handle = handles[handle_name]
self.set_customjs(handle, handles)
requested = {}
for h in self.handles+self.extra_handles:
if h in handles:
requested[h] = handles[h]
else:
print("Warning %s could not find the %s model. "
"The corresponding stream may not work.")
self.callbacks.append(self.set_customjs(handle, requested))


def _filter_msg(self, msg, ids):
Expand Down Expand Up @@ -257,8 +271,7 @@ def _get_plot_handles(self, plots):
handles = {}
for plot in plots:
for k, v in plot.handles.items():
if k not in handles and k in self.handles:
handles[k] = v
handles[k] = v
return handles


Expand Down Expand Up @@ -290,13 +303,20 @@ def set_customjs(self, handle, references):
debounce=self.debounce)

attributes = attributes_js(self.attributes, references)
code = 'var data = {};\n' + attributes + self.code + self_callback
conditions = ["%s" % cond for cond in self.skip_conditions]
conditional = ''
if conditions:
conditional = 'if (%s) { return };\n' % (' || '.join(conditions))
code = conditional + 'var data = {};\n' + attributes + self.code + self_callback

print references
js_callback = CustomJS(args=references, code=code)
if self.event:
handle.js_on_event(self.event, js_callback)
if self.events:
for event in self.events:
handle.js_on_event(event, js_callback)
elif self.change:
handle.js_on_change(self.change, js_callback)
for change in self.change:
handle.js_on_change(change, js_callback)
elif id(handle.callback) in self._callbacks:
# Merge callbacks if another callback has already been attached
cb = self._callbacks[id(handle.callback)]
Expand All @@ -309,28 +329,32 @@ def set_customjs(self, handle, references):
else:
self._callbacks[id(js_callback)] = self
handle.callback = js_callback
return js_callback



class PositionXYCallback(Callback):

attributes = {'x': 'cb_data.geometry.x', 'y': 'cb_data.geometry.y'}
handles = ['plot']
events = ['mousemove']

tool_filter = {'pan': PanTool, 'box_zoom': BoxZoomTool}

handles = ['hover']


class PositionXCallback(Callback):

attributes = {'x': 'cb_data.geometry.x'}

handles = ['hover']
handles = ['plot']
events = ['mousemove']


class PositionYCallback(Callback):

attributes = {'y': 'cb_data.geometry.y'}

handles = ['hover']
handles = ['plot']
events = ['mousemove']


class RangeXYCallback(Callback):
Expand All @@ -341,6 +365,7 @@ class RangeXYCallback(Callback):
'y1': 'y_range.attributes.end'}

handles = ['x_range', 'y_range']
change = ['start', 'end']

def _process_msg(self, msg):
data = {}
Expand Down

0 comments on commit e97ec94

Please sign in to comment.