Hover tool doesn't work for plots added inside callback

I am experiencing strange behaviour, the hover tool doesn’t show tooltips when created inside a callback. Here is the simplest example:

from bokeh.plotting import Figure, curdoc
from bokeh.layouts import column
from bokeh.models.tools import HoverTool
from bokeh.resources import CDN
from bokeh.embed import server_document, server_session
from bokeh.client import push_session
from bokeh.models import Button
doc = curdoc()
button = Button(label='Add Plot', button_type="success", name='my_button'
)
plots_layout = column(button)
def callback():
    hover = HoverTool(tooltips='hi'    )
plot = Figure()
plot.add_tools(hover)
plots_layout.children.append(plot)
plot.line([1, 2], [1, 2
])
button.on_click(callback)
doc.add_root(plots_layout)
session = push_session(curdoc())
session.show()
session.loop_until_closed()

Interesting that it works after refreshing the page.

Adding versions of the software:

  • python 3.5

  • bokeh 0.12.9

  • Latest Chrome and Firefox

There were no errors neither from python side or javascript side.

Will be happy to hear how to fix this. Thank you!

···

On Tuesday, September 26, 2017 at 6:34:10 AM UTC-7, Dmitriy Serdyuk wrote:

I am experiencing strange behaviour, the hover tool doesn’t show tooltips when created inside a callback. Here is the simplest example:

from bokeh.plotting import Figure, curdoc
from bokeh.layouts import column
from bokeh.models.tools import HoverTool
from bokeh.resources import CDN
from bokeh.embed import server_document, server_session
from bokeh.client import push_session
from bokeh.models import Button
doc = curdoc()
button = Button(label='Add Plot', button_type="success", name='my_button'
)
plots_layout = column(button)
def callback():
    hover = HoverTool(tooltips='hi'    )
plot = Figure()
plot.add_tools(hover)
plots_layout.children.append(    plot)
plot.line([1, 2], [1, 2
])
button.on_click(callback)
doc.add_root(plots_layout)
session = push_session(curdoc())
session.show()
session.loop_until_closed()

Interesting that it works after refreshing the page.

Hi Dmitriy,

One way to get the tooltips to appear correctly is to set the renderers property on the hover tool explicitly. This seems to work for me:

def callback():
plot = Figure()
r = plot.line([1, 2], [1, 2])
hover = HoverTool(tooltips=“hi”, renderers=[r])
plot.add_tools(hover)
plots_layout.children.append(plot)

``

Hope this helps,

Claire

···

Claire,

Thanks for your response.

In my project, I’d like to add lines to plot dynamically meaning that I don’t know in advance the renderer. I tried to add the renderer after but it doesn’t seem to work:

def callback():
    hover = HoverTool(tooltips='hi'    )
plot = Figure()
plot.add_tools(hover)
plots_layout.children.append(plot)
# This happens in another part of code:
    line = plot.line([1, 2], [1, 2    ])
hover.renderers.append(line)

Should I refresh/update something?

···

On Wed, Sep 27, 2017 at 7:26 PM, Claire Tang [email protected] wrote:

Hi Dmitriy,

One way to get the tooltips to appear correctly is to set the renderers property on the hover tool explicitly. This seems to work for me:

def callback():
plot = Figure()
r = plot.line([1, 2], [1, 2])
hover = HoverTool(tooltips=“hi”, renderers=[r])
plot.add_tools(hover)
plots_layout.children.append(plot)

``

Hope this helps,

Claire

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

To unsubscribe from this topic, visit https://groups.google.com/a/continuum.io/d/topic/bokeh/EtZYpU-DUOo/unsubscribe.

To unsubscribe from this group and all its topics, 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/e4a72597-dc12-4220-a276-e0852d81f2f7%40continuum.io.

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