Search code examples
javascriptpythonbokehinteractive

How to get mouse position with bokeh server?


I want to get the mouse position in a plot via a callback function using bokeh server. A solution for the latest bokeh version 2.0.2 would be great.

So far I found this old solution which does not work any more due to deprecation of the tool_events attribute in the figure object.

I have found this javascript example which does not work for the boekh server context.

Has someone an idea how to achieve this with bokeh?


Solution

  • If you want to get mouse position after every move, regardless of whether the cursor is over any glyph, you can just listen to the mousemove event:

    from bokeh.events import PointEvent
    from bokeh.io import curdoc
    from bokeh.plotting import figure
    
    p = figure()
    p.circle(0, 0)
    
    
    def on_mouse_move(event: PointEvent):
        print(event.x, event.y, event.sx, event.sy)
    
    
    p.on_event('mousemove', on_mouse_move)
    
    curdoc().add_root(p)
    

    Also mouseenter and mouseleave may be of interest to you.