Editing and adding lines using the poly tools

The following is intended to draw two lines on a plot, enable editing and adding additional lines. I want the new lines to be added to the same render ‘lines’.

SOLUTION: The secret is to set a default in the PolyDrawTool using default_overrides = {‘colors’: ‘orange’})

from bokeh.io import curdoc
from bokeh.models import ColumnDataSource, Div, PolyEditTool, PolyDrawTool, Circle
from bokeh.plotting import figure
from bokeh.layouts import column

# Initial data for two lines
line_data = {
    'xs': [[10, 20], [6, 15]],
    'ys': [[1, 4], [6, 9]],
    'colors': ['blue', 'green']
}

# Create ColumnDataSource
source = ColumnDataSource(data=line_data)

# Create a plot
plot = figure(title="Editable Lines", x_axis_label='X', y_axis_label='Y', width=600, height=500)

# Add the line to the plot using MultiLine
lines = plot.multi_line(xs='xs', ys='ys', source=source, line_width=2, line_color='colors')

# Create a vertex renderer for snapping
vr = plot.scatter([], [], size=10, marker='circle_dot', line_color='navy', fill_color='orange', alpha=0.5)

# Add PolyEditTool for editing lines with snapping to vertices
poly_edit_tool = PolyEditTool(renderers=[lines], vertex_renderer=vr)
plot.add_tools(poly_edit_tool)

# Add PolyDrawTool for adding new lines
poly_draw_tool = PolyDrawTool(renderers=[lines], default_overrides = {'colors': 'orange'}))
plot.add_tools(poly_draw_tool)

# Layout of the application
layout = column(plot)

# Add the layout to the current document
curdoc().add_root(layout)

I’m not really very familiar with the edit tools these days. Maybe @mateusz or @Philipp_Rudiger or @James_A_Bednar1 can comment more since I know Holoviews uses them in various ways.

Thank you Bryan. I have worked it out through persistence. the answer was to set default_overrides = {‘colors’: ‘orange’} in the PolyDrawTool. I will update the code

1 Like