Skip to content
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

Add ability to define selected glyphs on bokeh plots #4281

Merged
merged 3 commits into from
Mar 11, 2020
Merged
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
2 changes: 1 addition & 1 deletion holoviews/plotting/bokeh/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ def _process_msg(self, msg):
if isinstance(el, Table):
# Ensure that explicitly applied selection does not
# trigger new events
sel = el.opts.get('style').kwargs.get('selection')
sel = el.opts.get('plot').kwargs.get('selected')
if sel is not None and list(sel) == msg['index']:
return {}
return self._transform(msg)
Expand Down
12 changes: 12 additions & 0 deletions holoviews/plotting/bokeh/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class PointPlot(LegendPlot, ColorbarPlot):
jitter = param.Number(default=None, bounds=(0, None), doc="""
The amount of jitter to apply to offset the points along the x-axis.""")

selected = param.List(default=None, doc="""
The current selection as a list of integers corresponding
to the selected items.""")

# Deprecated parameters

color_index = param.ClassSelector(default=None, class_=(basestring, int),
Expand Down Expand Up @@ -509,6 +513,10 @@ def _init_glyph(self, plot, mapping, properties):

class ErrorPlot(ColorbarPlot):

selected = param.List(default=None, doc="""
The current selection as a list of integers corresponding
to the selected items.""")

selection_display = BokehOverlaySelectionDisplay()

style_opts = ([
Expand Down Expand Up @@ -715,6 +723,10 @@ class SideSpikesPlot(SpikesPlot):
SpikesPlot with useful defaults for plotting adjoined rug plot.
"""

selected = param.List(default=None, doc="""
The current selection as a list of integers corresponding
to the selected items.""")

xaxis = param.ObjectSelector(default='top-bare',
objects=['top', 'bottom', 'bare', 'top-bare',
'bottom-bare', None], doc="""
Expand Down
9 changes: 9 additions & 0 deletions holoviews/plotting/bokeh/geometry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import absolute_import, division, unicode_literals

import numpy as np
import param

from ..mixins import GeomMixin
from .element import ColorbarPlot, LegendPlot
Expand All @@ -14,6 +15,10 @@ class SegmentPlot(GeomMixin, ColorbarPlot):
(x, y) node of the line.
"""

selected = param.List(default=None, doc="""
The current selection as a list of integers corresponding
to the selected items.""")

selection_display = BokehOverlaySelectionDisplay()

style_opts = base_properties + line_properties + ['cmap']
Expand All @@ -34,6 +39,10 @@ def _get_factors(self, element, ranges):

class RectanglesPlot(GeomMixin, LegendPlot, ColorbarPlot):

selected = param.List(default=None, doc="""
The current selection as a list of integers corresponding
to the selected items.""")

selection_display = BokehOverlaySelectionDisplay()

style_opts = (base_properties + line_properties + fill_properties +
Expand Down
4 changes: 4 additions & 0 deletions holoviews/plotting/bokeh/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ def get_batched_data(self, element, ranges=None):

class ContourPlot(PathPlot):

selected = param.List(default=None, doc="""
The current selection as a list of integers corresponding
to the selected items.""")

show_legend = param.Boolean(default=False, doc="""
Whether to show legend for the plot.""")

Expand Down
17 changes: 16 additions & 1 deletion holoviews/plotting/bokeh/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,23 @@ def get_data(self, element, ranges, style):
raise NotImplementedError


def _update_selected(self, cds):
from .callbacks import Selection1DCallback
cds.selected.indices = self.selected
for cb in self.callbacks:
if isinstance(cb, Selection1DCallback):
for s in cb.streams:
s.update(index=self.selected)

def _init_datasource(self, data):
"""
Initializes a data source to be passed into the bokeh glyph.
"""
data = self._postprocess_data(data)
return ColumnDataSource(data=data)
cds = ColumnDataSource(data=data)
if hasattr(self, 'selected') and self.selected is not None:
self._update_selected(cds)
return cds


def _postprocess_data(self, data):
Expand Down Expand Up @@ -153,6 +164,10 @@ def _update_datasource(self, source, data):
else:
source.data.update(data)

if hasattr(self, 'selected') and self.selected is not None:
self._update_selected(source)


def _update_callbacks(self, plot):
"""
Iterates over all subplots and updates existing CustomJS
Expand Down
12 changes: 7 additions & 5 deletions holoviews/plotting/bokeh/tabular.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@ class TablePlot(BokehPlot, GenericElementPlot):

height = param.Number(default=300)

selected = param.List(default=None, doc="""
The current selection as a list of integers corresponding
to the selected items.""")

width = param.Number(default=400)

selection_display = TabularSelectionDisplay()

style_opts = ['row_headers', 'selectable', 'selected', 'editable',
style_opts = ['row_headers', 'selectable', 'editable',
'sortable', 'fit_columns', 'scroll_to_selection',
'index_position', 'visible']

Expand Down Expand Up @@ -69,8 +73,8 @@ def initialize_plot(self, ranges=None, plot=None, plots=None, source=None):
source = self._init_datasource(data)
self.handles['source'] = self.handles['cds'] = source
self.handles['selected'] = source.selected
if 'selected' in style:
source.selected.indices = list(style['selected'])
if self.selected is not None:
source.selected.indices = self.selected

columns = self._get_columns(element, data)
style['reorderable'] = False
Expand Down Expand Up @@ -140,6 +144,4 @@ def update_frame(self, key, ranges=None, plot=None):
data, _, style = self.get_data(element, ranges, style)
columns = self._get_columns(element, data)
self.handles['table'].columns = columns
if 'selected' in style:
source.selected.indices = list(style['selected'])
self._update_datasource(source, data)
6 changes: 6 additions & 0 deletions holoviews/tests/plotting/bokeh/testcallbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ def test_callback_cleanup(self):
self.assertFalse(bool(stream._subscribers))
self.assertFalse(bool(Callback._callbacks))

def test_selection1d_syncs_to_selected(self):
points = Points([(0, 0), (1, 1), (2, 2)]).opts(selected=[0, 2])
stream = Selection1D(source=points)
bokeh_renderer.get_plot(points)
self.assertEqual(stream.index, [0, 2])


class TestResetCallback(CallbackTestCase):

Expand Down
6 changes: 6 additions & 0 deletions holoviews/tests/plotting/bokeh/testpointplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ def test_points_datetime_hover(self):
hover = plot.handles['hover']
self.assertEqual(hover.tooltips, [('x', '@{x}'), ('y', '@{y}'), ('date', '@{date_dt_strings}')])

def test_points_selected(self):
points = Points([(0, 0), (1, 1), (2, 2)]).opts(selected=[0, 2])
plot = bokeh_renderer.get_plot(points)
cds = plot.handles['cds']
self.assertEqual(cds.selected.indices, [0, 2])

###########################
# Styling mapping #
###########################
Expand Down