bokeh is a fantastic framework and adding/removing glyphrenderers dynamically **almost **works.
Things get a bit messed up when we repeatedly add/remove a line (axis labels disappear after a few quick clicks, looking like a problem of sync).
(when placing legend outside the plot, the figure drifts slowly, noticeable after 20-30 clicks)
Example code below.
I understand that (from April 2016 discussions) it was recommended to declare them (together with their associated cds) instead at start and to play with the columndatasource (set to ) or with alpha or visible attributes, but things have greatly evolved since.
The fact that it “almost” works well makes me hope that a fully functional dynamic behavior (at least for glyphrenderers) is intended in the future. It would be so great !
It looks like it is not very far.
Is that correct ?
Is there a way to make this code reliably work ?
Thx
from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource,Legend
from bokeh.models.widgets import Toggle
b = Toggle(label=‘add/remove line’)
x=y=list(range(30)); source = ColumnDataSource(dict(index=x,x=x,dn=y))
p = figure(plot_width=800, plot_height=250)
leg = Legend(name=‘leg’,location = ‘top_left’,click_policy=“hide”); p.add_layout(leg)
def b_cb(x):
if x:
r_dn = p.line(x=‘x’,y=‘dn’,name=‘gr_dn’,source=source,legend=‘down’)
else:
for r in p.renderers:
if r.name==‘gr_dn’:
p.renderers.remove(r); p.legend[0].items.pop()
b.on_click(lambda x: b_cb(x))
curdoc().add_root(column(b,p))