Boxedit

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


Title: BoxEdit Stream

Description: A linked streams example demonstrating how to use the BoxEdit stream.

Dependencies: Bokeh

Backends: Bokeh

In [1]:
import holoviews as hv
from holoviews import opts
from holoviews import streams
hv.extension('bokeh')

The BoxEdit stream adds a bokeh tool to the source plot that allows drawing, dragging, and deleting boxes and making the drawn data available to Python. The tool supports the following actions:

Add box

Double click to start one corner, then move the pointer to the other corner and double click again. (Or 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 or DELETE key while the mouse is within the plot area.

Properties

  • empty_value: Value to add to non-coordinate columns when adding new box
  • num_objects (int): Maximum number of boxes to draw before deleting the oldest object
  • styles (dict): Dictionary of style properties (e.g. line_color, line_width etc.) to apply to each box. If values are lists the values will cycle over the values.

As a very straightforward example we will create a Rectangles element containing multiple boxes, then attach it as a source to a BoxEdit stream instance. When we now plot the Rectangles 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 [2]:
boxes = hv.Rectangles([(0, 0, 1, 1), (2, 1, 3.5, 2.5), (0.5, 1.5, 1.5, 2.5)])
box_stream = streams.BoxEdit(source=boxes, num_objects=3, styles={'fill_color': ['red', 'green', 'blue']})
boxes.opts(
    opts.Rectangles(active_tools=['box_edit'], fill_alpha=0.5, height=400, width=400))
Out[2]:

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 [3]:
box_stream.data

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

In [4]:
box_stream.element
WARNING:param.RectanglesPlot03031: Tool of type 'box_edit' could not be found and could not be activated by default.
Out[4]:

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