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

Make Buffer following behavior toggleable #3823

Merged
merged 2 commits into from
Jul 21, 2019
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
7 changes: 7 additions & 0 deletions examples/user_guide/16-Streaming_Data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@
"dfstream.clear()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that when using the ``Buffer`` stream the view will always follow the current range of the data by default, by setting ``buffer.following=False`` or passing following as an argument to the constructor this behavior may be disabled."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
3 changes: 2 additions & 1 deletion holoviews/plotting/bokeh/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,8 @@ def _update_ranges(self, element, ranges):
if any(isinstance(ax_range, FactorRange) for ax_range in [x_range, y_range]):
xfactors, yfactors = self._get_factors(element, ranges)
framewise = self.framewise
streaming = (self.streaming and any(stream._triggering for stream in self.streaming))
streaming = (self.streaming and any(stream._triggering and stream.following
for stream in self.streaming))
xupdate = ((not (self.model_changed(x_range) or self.model_changed(plot))
and (framewise or streaming))
or xfactors is not None)
Expand Down
8 changes: 7 additions & 1 deletion holoviews/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,14 @@ class Buffer(Pipe):
When streaming a DataFrame will reset the DataFrame index by
default making it available to HoloViews elements as dimensions,
this may be disabled by setting index=False.

The ``following`` argument determines whether any plot which is
subscribed to this stream will update the axis ranges when an
update is pushed. This makes it possible to control whether zooming
is allowed while streaming.
"""

def __init__(self, data, length=1000, index=True, **params):
def __init__(self, data, length=1000, index=True, following=True, **params):
if (util.pd and isinstance(data, util.pd.DataFrame)):
example = data
elif isinstance(data, np.ndarray):
Expand Down Expand Up @@ -512,6 +517,7 @@ def __init__(self, data, length=1000, index=True, **params):
params['data'] = example
super(Buffer, self).__init__(**params)
self.length = length
self.following = following
self._chunk_length = 0
self._count = 0
self._index = index
Expand Down
40 changes: 40 additions & 0 deletions holoviews/tests/plotting/bokeh/teststreaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import numpy as np

from holoviews.core import DynamicMap
from holoviews.element import Curve
from holoviews.streams import Buffer

from .testplot import TestBokehPlot, bokeh_renderer


class TestBufferStreamPlot(TestBokehPlot):

def test_buffer_stream_following(self):
stream = Buffer(data={'x': np.array([1]), 'y': np.array([1])}, following=True)
dmap = DynamicMap(Curve, streams=[stream])

plot = bokeh_renderer.get_plot(dmap)

x_range = plot.handles['x_range']
y_range = plot.handles['y_range']

self.assertEqual(x_range.start, 0)
self.assertEqual(x_range.end, 2)
self.assertEqual(y_range.start, 0)
self.assertEqual(y_range.end, 2)

stream.send({'x': np.array([2]), 'y': np.array([-1])})

self.assertEqual(x_range.start, 1)
self.assertEqual(x_range.end, 2)
self.assertEqual(y_range.start, -1)
self.assertEqual(y_range.end, 1)

stream.following = False

stream.send({'x': np.array([3]), 'y': np.array([3])})

self.assertEqual(x_range.start, 1)
self.assertEqual(x_range.end, 2)
self.assertEqual(y_range.start, -1)
self.assertEqual(y_range.end, 1)