I’m using a panel widget to toggle between drawing points and polygons with the PointDraw and PolyDraw streams in Holoviews. When “Points” is selected, only the Points layer is visible and the PointDraw tool is active, and when “Polygons” is selected the Polygons layer is visible and PolyDraw is active.
My problem: when switching from “Polygons” back to “Points,” the polygon vertices from the PolyDraw tool remain visible and don’t disappear as expected. A similar problem happens when activating the PolyEdit tool instead of PolyDraw: if the polygon is selected (vertices are visible) and I switch back to “Points”, the vertices remain visible alongside the Points.
Here is a minimal example:
import holoviews as hv
import panel as pn
from holoviews import streams
import param
pn.extension()
select_button = pn.widgets.RadioButtonGroup(options=['Points', 'Polygons'], value='Points')
active_tool_stream = streams.Stream.define('ActiveToolStream', t=streams.param.String(default=''))()
@param.depends(select_button.param.value)
def points_layer(value):
points = hv.Points([(0, 0), (0.25, 0.25)], kdims=['Longitude', 'Latitude']).opts(size=8, color='red')
if value == "Points":
points = points.opts(visible=True)
active_tool_stream.event(t='point_draw')
else:
points = points.opts(visible=False)
active_tool_stream.event(t='poly_draw') # comment this line and uncomment below to test poly_edit
# active_tool_stream.event(t='poly_edit')
return points
@param.depends(select_button.param.value)
def polygons_layer(value):
polys = hv.Polygons([[(1, 0), (1, 1), (0, 1)]], kdims=['Longitude', 'Latitude'])
if value == "Points":
polys = polys.opts(visible=False)
else:
polys = polys.opts(visible=True)
return polys
dm_points = hv.DynamicMap(points_layer)
point_draw_stream = hv.streams.PointDraw(source=dm_points, num_objects=2, drag=True)
dm_polys = hv.DynamicMap(polygons_layer)
poly_draw_stream = hv.streams.PolyDraw(source=dm_polys, num_objects=1, drag=True, show_vertices=True)
poly_edit_stream = hv.streams.PolyEdit(source=dm_polys)
overlay = (dm_points * dm_polys).apply.opts(
active_tools=[active_tool_stream.param.t],
height=400, width=400
)
layout = pn.Column(select_button, overlay)
layout
This is how it looks after switching from “Polygons” to “Points” (only red points should be visible):

Is this a bug? In any case, any ideas on how to solve this will be appreciated!
Thank you!