Adding lines dynamically and then edit/remove them again: Jupyter Notebook

Hi there,

Still fairly new to using Bokeh, but I’m trying my best to get a hang of it.

I’m adding lines to a plot on demand, and I want to be able to remove them again. I’m encountering the issue, that if I add lines after the show command and apply them using push_notebook I can’t update them. I don’t know if this is intentional or if I’m doing something wrong.

Example 1: Calling show after everything is added

import bokeh.plotting.figure as bk_figure
import numpy as np

from bokeh.io import push_notebook, show, output_notebook
from bokeh.models import ColumnDataSource, Line
from bokeh.palettes import Category10 as palette

output_notebook()
plot = bk_figure(plot_width=590, plot_height=400, title="Legend Test Plot", x_axis_label="X Value", y_axis_label="Y Value")

x = np.arange(1, 11)
y = x ** 0
source = ColumnDataSource(data={'x':x,'y':y})
glyph = Line(x='x',y='y')
glyph = plot.add_glyph(source,glyph)

x = np.arange(1, 11)
y = x ** 1
color = palette[10][0]
source = ColumnDataSource(data={'x':x,'y':y})
glyph = Line(x='x',y='y',line_color=color)
glyph = plot.add_glyph(source,glyph)

x = np.arange(1, 11)
y = x ** 2
color = palette[10][1]
source = ColumnDataSource(data={'x':x,'y':y})
glyph = Line(x='x',y='y',line_color=color)
glyph = plot.add_glyph(source,glyph)
h = show(plot, notebook_handle=True)

Here everything is fine. I can go and do

plot.renderers[2].visible = False
push_notebook(handle=h)

and it will hide the line just fine. However, as my lines are going to be added dynamically, it will look more like this:

import bokeh.plotting.figure as bk_figure
import numpy as np

from bokeh.io import push_notebook, show, output_notebook
from bokeh.models import ColumnDataSource, Line
from bokeh.palettes import Category10 as palette

output_notebook()
plot = bk_figure(plot_width=590, plot_height=400, title="Legend Test Plot", x_axis_label="X Value", y_axis_label="Y Value")

x = np.arange(1, 11)
y = x ** 0
source = ColumnDataSource(data={'x':x,'y':y})
glyph = Line(x='x',y='y')
glyph = plot.add_glyph(source,glyph)
h = show(plot, notebook_handle=True)

x = np.arange(1, 11)
y = x ** 1
color = palette[10][0]
source = ColumnDataSource(data={'x':x,'y':y})
glyph = Line(x='x',y='y',line_color=color)
glyph = plot.add_glyph(source,glyph)
push_notebook(handle=h)

x = np.arange(1, 11)
y = x ** 2
color = palette[10][1]
source = ColumnDataSource(data={'x':x,'y':y})
glyph = Line(x='x',y='y',line_color=color)
glyph = plot.add_glyph(source,glyph)
push_notebook(handle=h)

and now, I can’t interact with the 2 lines added after the show. This won’t work

plot.renderers[2].visible = False
push_notebook(handle=h)

but this will

plot.renderers[0].visible = False
push_notebook(handle=h)

Why I’m not using the widgets from Bokeh and making an app

This is how the “application” is going to look:


I’m not saying it is pretty, but the key thing is the selection widget used for Files, as there can be 10 000+ files to select from, I need the search bar and requiring the user to keep pressing ctrl is not a nice solution when selecting multiple, so I’m using a combination of widgets from ipywidgets, and then placing the bokeh plot inside a ipywidget grid.

If I could fix this removal of lines issue, then my “application” would be all set and ready to use, so I was hoping someone here could assist me in a solution.

I would like to add that I can “fix” the issue by creating empty glyphs before hand. However, this way I would set a “limit” to the amount of lines I can show. And I would like to avoid this if possible.

Looks like you are running in to #8244, which is still open. I think a “pre-allocated” pool of renderers is going to be your best bet for now.

Thanks for the answer. I’m going to pre-allocate then, seeing the date of the issue I’m assuming there are not many who encounters this issue.

1 Like