Simplified bokeh Callback initialization
@@ -92,15 +92,31 @@ class MessageCallback(object):
stream._metadata = {}
- def _get_plot_handles(self, plots):
+ def _init_plot_handles(self):
"""
- Iterate over plots and find all unique plotting handles.
+ Find all requested plotting handles and cache them along
+ with the IDs of the models callbacks will be attached to.
"""
+ plots = [self.plot]
+ if self.plot.subplots:
+ plots += list(self.plot.subplots.values())
+
handles = {}
for plot in plots:
for k, v in plot.handles.items():
handles[k] = v
- return handles
+ self.plot_handles = handles
+
+ requested = {}
+ for h in self.models+self.extra_models:
+ if h in self.plot_handles:
+ requested[h] = handles[h]
+ elif h in self.extra_models:
+ print("Warning %s could not find the %s model. "
+ "The corresponding stream may not work.")
+ self.handle_ids.update(self._get_stream_handle_ids(requested))
+
+ return requested
def _get_stream_handle_ids(self, handles):
@@ -468,30 +484,15 @@ class Callback(CustomJSCallback, ServerCallback):
"""
def initialize(self):
- plots = [self.plot]
- if self.plot.subplots:
- plots += list(self.plot.subplots.values())
-
- self.plot_handles = self._get_plot_handles(plots)
- requested = {}
- for h in self.models+self.extra_models:
- if h in self.plot_handles:
- requested[h] = self.plot_handles[h]
- elif h in self.extra_models:
- print("Warning %s could not find the %s model. "
- "The corresponding stream may not work.")
- self.handle_ids.update(self._get_stream_handle_ids(requested))
-
- found = []
- for plot in plots:
+ handles = self._init_plot_handles()
for handle_name in self.models:
- if handle_name not in self.plot_handles:
+ if handle_name not in handles:
warn_args = (handle_name, type(self.plot).__name__,
type(self).__name__)
print('%s handle not found on %s, cannot '
'attach %s callback' % warn_args)
continue
- handle = self.plot_handles[handle_name]
+ handle = handles[handle_name]
# Hash the plot handle with Callback type allowing multiple
# callbacks on one handle to be merged
