A linked streams example demonstrating how to use the BoxDraw stream.
streams, linked, position, interactive
In [ ]:
import holoviews as hv
from holoviews import opts
from holoviews import streams

The BoxEdit 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:

Add box

Hold shift then click and drag anywhere on the plot.

Move box

Click and drag an existing box, the box will be dropped once you let go of the mouse button.

Delete box

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 BoxEdit stream instance. When we now plot the boxes 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 num_objects may be defined, causing the first box to be dropped when the limit is exceeded.

In [ ]:
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)
    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:

In [ ]:

Alternatively we can use the element property to get an Element containing the returned data:

In [ ]:

Download this notebook from GitHub (right-click to download).