Always show vertices polygon draw stream

Hi!

I am quite new to holoviews, so I’m trying to experiment with making a custom callback for the polygon_draw tool. I want to have two scatterplot axes, both with poly_draw tools, and as the user draws a polygon on one axis, the other axis also draws the same polygon. I’m experimenting with this and haven’t written the full implementation of the callback yet, so for now I’m just returning an empty polygon so that the script doesn’t crash (a DynamicMap MUST have something passed to it, and I don’t think there’s any other way to write a callback function for a stream? let me know if I’m mistaken).

I noticed that sometimes, when I reload the page, the show_vertices=True kwarg in the stream initialization is not applying. Do I need to write something specific in my callback function to explicitly show the points?

Here is a video of what I’m talking about. https://youtu.be/9SRSZeULOyk

Here is my code:

import numpy as np
from bokeh.models import Range1d
import holoviews as hv
import panel as pn

hv.extension('bokeh')

def poly_mirror(ax1_vertices, ax2_vertices, target):
    if ax1_vertices == None:
        return hv.Polygons([])
    print(np.array([np.array(ax1_vertices['xs']).flatten(), np.array(ax1_vertices['ys']).flatten()]))
    return hv.Polygons([])
##    if target == 'ax1':
##        return hv.Polygons(ax2_vertices)
##    elif target == 'ax2':
##       return hv.Polygons(ax1_vertices)
##    else:
##        return hv.Polygons([])


x = np.arange(0, 10)
y = np.array([10, 1, 9, 2, 8, 3, 7, 4, 6, 5])
z = np.arange(10, 0, -1)

points_1 = hv.Points((x, y, z), vdims='z').opts(color='z', cmap='rainbow')
points_2 = hv.Points((x, z, y), vdims='z').opts(color='z', cmap='rainbow')


poly_1 = hv.Polygons([]).opts(hv.opts.Polygons(fill_alpha=0.3, fill_color='black'))
selector_1 = hv.streams.PolyDraw(source=poly_1, drag=False, num_objects=1, show_vertices=True, vertex_style={'size': 5, 'fill_color': 'white', 'line_color' : 'black'}).rename(data='ax1_vertices')


poly_2 = hv.Polygons([]).opts(hv.opts.Polygons(fill_alpha=0.3, fill_color='black'))
selector_2 = hv.streams.PolyDraw(source=poly_2, drag=False, num_objects=1, show_vertices=True, vertex_style={'size': 5, 'fill_color': 'white', 'line_color' : 'black'}).rename(data='ax2_vertices')

mirror_to_1 = hv.DynamicMap(lambda ax1_vertices, ax2_vertices:
                            poly_mirror(ax1_vertices, ax2_vertices, 'ax1'), streams=[selector_1, selector_2]).opts(hv.opts.Polygons(fill_alpha=0.3, fill_color='black'))
mirror_to_2 = hv.DynamicMap(lambda ax1_vertices, ax2_vertices:
                            poly_mirror(ax1_vertices, ax2_vertices, 'ax2'), streams=[selector_1, selector_2]).opts(hv.opts.Polygons(fill_alpha=0.3, fill_color='black'))


ax1 = (points_1 * poly_1 * mirror_to_1)
ax2 = (points_2 * poly_2 * mirror_to_2)

pn.Row(ax1, ax2).servable()

After troubleshooting this more, there appears to be a race condition in the HoloViews library, opening an issue…