Skip to content

Commit

Permalink
Merge pull request #4300 from holoviz/update_selected_on_cds
Browse files Browse the repository at this point in the history
Ensure selected plot option can be updated
  • Loading branch information
philippjfr committed Mar 18, 2020
2 parents 8494351 + 3041b6b commit c2516ae
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
4 changes: 3 additions & 1 deletion holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,8 @@ def _update_glyph(self, renderer, properties, mapping, glyph, source, data):
if not cds_replace:
if not self.static_source:
self._update_datasource(source, data)
if hasattr(self, 'selected') and self.selected is not None:
self._update_selected(source)
elif self.document:
server = self.renderer.mode == 'server'
with hold_policy(self.document, 'collect', server=server):
Expand Down Expand Up @@ -1224,7 +1226,7 @@ def _update_glyph(self, renderer, properties, mapping, glyph, source, data):
glyph.update(**update)

if data is not None and cds_replace and not self.static_source:
return self._update_datasource(source, data)
self._update_datasource(source, data)


def _postprocess_hover(self, renderer, source):
Expand Down
5 changes: 4 additions & 1 deletion holoviews/plotting/bokeh/tabular.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,21 @@ def update_frame(self, key, ranges=None, plot=None):
to the key.
"""
element = self._get_frame(key)
self.param.set_param(**self.lookup_options(element, 'plot').options)
self._get_title_div(key, '12pt')

# Cache frame object id to skip updating data if unchanged
previous_id = self.handles.get('previous_id', None)
current_id = element._plot_id
source = self.handles['source']
self.handles['previous_id'] = current_id
self.static_source = (self.dynamic and (current_id == previous_id))
if (element is None or (not self.dynamic and self.static) or
(self.streaming and self.streaming[0].data is self.current_frame.data
and not self.streaming[0]._triggering) or self.static_source):
if self.static_source and hasattr(self, 'selected') and self.selected is not None:
self._update_selected(source)
return
source = self.handles['source']
style = self.lookup_options(element, 'style')[self.cyclic_index]
data, _, style = self.get_data(element, ranges, style)
columns = self._get_columns(element, data)
Expand Down
10 changes: 10 additions & 0 deletions holoviews/tests/plotting/bokeh/testpointplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from holoviews.core.options import Cycle
from holoviews.core.util import pd
from holoviews.element import Points
from holoviews.streams import Stream

from .testplot import TestBokehPlot, bokeh_renderer
from ..utils import ParamLogStream
Expand Down Expand Up @@ -328,6 +329,15 @@ def test_points_selected(self):
cds = plot.handles['cds']
self.assertEqual(cds.selected.indices, [0, 2])

def test_points_update_selected(self):
stream = Stream.define('Selected', selected=[])()
points = Points([(0, 0), (1, 1), (2, 2)]).apply.opts(selected=stream.param.selected)
plot = bokeh_renderer.get_plot(points)
cds = plot.handles['cds']
self.assertEqual(cds.selected.indices, [])
stream.event(selected=[0, 2])
self.assertEqual(cds.selected.indices, [0, 2])

###########################
# Styling mapping #
###########################
Expand Down
17 changes: 16 additions & 1 deletion holoviews/tests/plotting/bokeh/testtabular.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from holoviews.core.spaces import DynamicMap
from holoviews.element import Table
from holoviews.element.comparison import ComparisonTestCase
from holoviews.streams import CDSStream
from holoviews.streams import CDSStream, Stream

try:
from bokeh.models.widgets import (
Expand Down Expand Up @@ -73,3 +73,18 @@ def test_table_change_columns(self):
plot.update(('b',))
self.assertEqual(sorted(plot.handles['source'].data.keys()), ['b'])
self.assertEqual(plot.handles['table'].columns[0].title, 'b')

def test_table_selected(self):
table = Table([(0, 0), (1, 1), (2, 2)], ['x', 'y']).opts(selected=[0, 2])
plot = bokeh_renderer.get_plot(table)
cds = plot.handles['cds']
self.assertEqual(cds.selected.indices, [0, 2])

def test_table_update_selected(self):
stream = Stream.define('Selected', selected=[])()
table = Table([(0, 0), (1, 1), (2, 2)], ['x', 'y']).apply.opts(selected=stream.param.selected)
plot = bokeh_renderer.get_plot(table)
cds = plot.handles['cds']
self.assertEqual(cds.selected.indices, [])
stream.event(selected=[0, 2])
self.assertEqual(cds.selected.indices, [0, 2])

0 comments on commit c2516ae

Please sign in to comment.