JS callback to show/hide the legend

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=;

//leg.visible=false;

cb_obj.button_type = “success”;

cb_obj.label = “Show legend”;

} else {

//leg.visible=true;

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

Hi,

···

On Sat, Jul 1, 2017 at 12:31 AM, [email protected] wrote:

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=;

//leg.visible=false;

cb_obj.button_type = “success”;

cb_obj.label = “Show legend”;

} else {

//leg.visible=true;

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?

This used to be a bug. This was fixed in 0.12.5 (if I recall correctly).

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)

It sometimes happens that particular properties aren’t listened to (more bugs), so you need to perform an action, like panning a plot, to trigger a repaint.

Mateusz

this is with bokeh 0.12.4

Thank you,

Seb

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/bfe3a602-431c-4190-a4d1-cee5e669eb43%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Figured the update problem, just had to add fig=fig in the callback arguments and fig.trigger(“change”) at the end of the callback code to have the update instantly on click.
However the “leg.visible” part still doesn’t work.

Hi, thank you I just tested with 0.12.6 and indeed it works with just setting the ‘visible’ property to true/false

···

Le vendredi 30 juin 2017 18:43:58 UTC-4, mateusz.paprocki a écrit :

Hi,

On Sat, Jul 1, 2017 at 12:31 AM, [email protected] wrote:

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=;

//leg.visible=false;

cb_obj.button_type = “success”;

cb_obj.label = “Show legend”;

} else {

//leg.visible=true;

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?

This used to be a bug. This was fixed in 0.12.5 (if I recall correctly).

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)

It sometimes happens that particular properties aren’t listened to (more bugs), so you need to perform an action, like panning a plot, to trigger a repaint.

Mateusz

this is with bokeh 0.12.4

Thank you,

Seb

You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/bfe3a602-431c-4190-a4d1-cee5e669eb43%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.