- A linked streams example demonstrating how to use the BoxDraw stream.
- streams, linked, position, interactive
import holoviews as hv from holoviews import opts from holoviews import streams hv.extension('bokeh')
stream adds a bokeh tool to the source plot, which allows drawing, dragging and deleting boxes and making the drawn data available to Python. The tool supports the following actions:
Hold shift then click and drag anywhere on the plot.
Click and drag an existing box, the box will be dropped once you let go of the mouse button.
Tap a box to select it then press BACKSPACE key while the mouse is within the plot area.
As a very straightforward example we will create a Polygons element containing multiple boxes, then attach it as a source to a
stream instance. When we now plot the
Polygons instance it will add the tool, letting us draw, drag and delete the box polygons. To limit the number of boxes that can be drawn a fixed number of
may be defined, causing the first box to be dropped when the limit is exceeded.
boxes = hv.Polygons([hv.Box(0, 0, 1), hv.Box(2, 1, 1.5), hv.Box(0.5, 1.5, 1)]) box_stream = streams.BoxEdit(source=boxes, num_objects=2) boxes.opts( opts.Polygons(active_tools=['box_edit'], fill_alpha=0.5, height=400, width=400))
Whenever an action is executed the geometry data will be synced with Python, both in the notebook and when deployed on the bokeh server. We can access the data directly as columns of the corners of each box:
Alternatively we can use the
property to get an Element containing the returned data: