Hi, I have tried this to show/hide the legend with a button, here is an example
import bokeh
from bokeh.plotting import figure
from bokeh.models import CustomJS, Button
from bokeh.layouts import gridplot
from bokeh.resources import CDN
from bokeh.embed import file_html
fig = figure()
fig.scatter(range(10),range(10),legend=‘test’)
legend_button = Button(label=‘Hide legend’,width=100,type=‘danger’)
legend_button_code = “”"
if (cb_obj.button_type.includes(“danger”)){
leg.background_fill_alpha=0;
leg.border_line_alpha=0;
leg.items=;
cb_obj.button_type = “success”;
cb_obj.label = “Show legend”;
} else {
leg.items=[it];
leg.background_fill_alpha=1;
leg.border_line_alpha=1;
cb_obj.button_type = “danger”;
cb_obj.label = “Hide legend”;
}
“”"
legend_rend = [rend for rend in fig.renderers if type(rend)==bokeh.models.annotations.Legend][0]
legend_item = legend_rend.items[0]
legend_button.callback = CustomJS(args=dict(leg=legend_rend,it=legend_item),code=legend_button_code)
grid = gridplot([[fig,legend_button]])
outfile=open(‘hide_legend.html’,‘w’)
outfile.write(file_html(grid,CDN,‘test’))
outfile.close()
``
This works, but I would like to be able to do this just by using the legend visible attribute instead of having to change alphas and Legend.items indiviually. Why this code doesn’t work if i just use the “leg.visible=” lines in the callback?
Also what should i do so that the update takes place exactly when i click the button? It only updates on change after I use one of the tools (pan/zoom)
this is with bokeh 0.12.4
Thank you,
Seb