2

I have a Bokeh server code that looks like this:

listOfSubLayouts = main_layout.children
plotToRemove = curdoc().get_model_by_name(self.plotid_long)
logging.warning(plotToRemove.name)
listOfSubLayouts.remove(plotToRemove)

The remove line gives the following error:

error handling message Message 'PATCH-DOC' (revision 1): ValueError('list.remove(x): x not in list',)

Could someone please shed some light on what might be going wrong here? Is this solution which I found here:

Dynamically add/remove plot using 'bokeh serve' (bokeh 0.12.0)

not working in a Bokeh server environment maybe?

JPA
  • 49
  • 1
  • 5
  • Hi maybe those posts could help https://stackoverflow.com/questions/46697867/replacing-figure-and-table-in-layout-when-using-global-columndatasource/46713283#46713283 https://stackoverflow.com/questions/46494811/how-to-replace-curdoc/46498077#46498077 – Seb Nov 08 '17 at 14:15

1 Answers1

2

Check if this example helps you, you can treat the children as a list. Check this other question where I explain and ask for possible alternatives.

from bokeh.models import Button, ColumnDataSource
from bokeh.layouts import column, layout
from bokeh.plotting import curdoc, figure

fig = figure(
    title='Third figure',
    width=400,
    height=400,
    x_range=[-5, 10], 
    y_range=(0, 5),
    tools='pan, wheel_zoom, zoom_in, zoom_out, box_select, lasso_select, tap, reset',
    x_axis_label='x axis',
    y_axis_label='y axis',
)

x = [1, 2, 3, 4]
y = [4, 3, 2, 1]

source = ColumnDataSource(data=dict(x=x, y=y))

fig.circle(
    x='x',
    y='y',
    source=source,
    radius=0.5,              
    fill_alpha=0.6,          
    fill_color='green',
    line_color='black',
)

def add_button():
    print("adding figure")
    layout.children.append(fig)

def remove_button():
    print("removing figure")
    layout.children.pop()


button = Button(label="Click to add the figure", button_type="success")
button.on_click(add_button)

button_rmv = Button(label="Click to remove the figure", button_type="success")
button_rmv.on_click(remove_button)

layout = layout([[button, button_rmv]])
curdoc().add_root(layout)
ChesuCR
  • 9,352
  • 5
  • 51
  • 114