Skip to content

Instantly share code, notes, and snippets.

@schaunwheeler
Last active May 8, 2018 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schaunwheeler/bdacec7a86fc0332a463b44c9effe5a6 to your computer and use it in GitHub Desktop.
Save schaunwheeler/bdacec7a86fc0332a463b44c9effe5a6 to your computer and use it in GitHub Desktop.
"""
Code described in Medium post https://medium.com/@schaun.wheeler/codify-your-workflow-377f5f8bf4c3
Copyright (c) 2018 Valassis Digital under the terms of the BSD 3-Clause license.
"""
def add_layer(self, source_label, bokeh_model, tooltips=None, **kwargs):
if self.plot is None:
raise AssertionError('self.plot is null; call `self.prepare_plot`.')
if 'color' in kwargs.keys():
color = kwargs.pop('color')
kwargs['fill_color'] = color
kwargs['line_color'] = color
if 'alpha' in kwargs.keys():
alpha = kwargs.pop('alpha')
kwargs['fill_alpha'] = alpha
kwargs['line_alpha'] = alpha
try:
model_object = bokeh_model(xs='x_coords', ys='y_coords', name=source_label, **kwargs)
except AttributeError:
model_object = bokeh_model(x='x_coords_point', y='y_coords_point', name=source_label, **kwargs)
if not all(c in self.sources[source_label].column_names for c in ['x_coord_point', 'y_coord_point']):
self.sources[source_label].data['x_coords_point'] = [mean(x) for x in self.sources[source_label].data['x_coords']]
self.sources[source_label].data['y_coords_point'] = [mean(x) for x in self.sources[source_label].data['y_coords']]
self.sources[source_label].column_names.extend(['x_coords_point', 'y_coords_point'])
rend = self.plot.add_glyph(self.sources[source_label], model_object)
if tooltips is not None:
self.plot.add_tools(HoverTool(tooltips=tooltips, renderers=[rend]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment