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

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.

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))

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:

box_stream.data

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

box_stream.element
WARNING:param.RectanglesPlot00338: Tool of type 'box_edit' could not be found and could not be activated by default.
This web page was generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

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