Bokeh server gets mouse position

I am developing an interactive bokeh application (0.12.2) that updates graphics based on specific interactions.

I am currently using sliders to change the position of the glyph in the plot, but I really want to access the position of my mouse in a specific plot.

A data set is a multidimensional matrix (tensor), dense data, and each graph displays one dimension in a specific place. If I changed the position of the marker glyph on one chart, the other charts need to be updated, which means that I have to cut my data set in accordance with the updated position.

Here is a simple example: I tried to get mouse data in my bokeh server update function using the freeze tool:

from bokeh.plotting import figure, ColumnDataSource
from bokeh.models import CustomJS, HoverTool
from bokeh.io import curdoc

s = ColumnDataSource(data=dict(x=[0, 1], y=[0, 1]))
callback = CustomJS(args=dict(s=s), code="""
        var geometry = cb_data['geometry'];
        var mouse_x = geometry.x;
        var mouse_y = geometry.y;
        var x = s.get('data')['x'];
        var y = s.get('data')['y'];
        x[0] = mouse_x;
        y[0] = mouse_y;
        s.trigger('change');
    """)
hover_tool = HoverTool(callback=callback)
p = figure(x_range=(0, 1), y_range=(0, 1), tools=[hover_tool])
p.circle(x='x', y='y', source=s)


def update():
    print s.data

curdoc().add_root(p)
curdoc().add_periodic_callback(update, 1000)

Unfortunately, the server only displays:

{'y': [0, 1], 'x': [0, 1]}

{'y': [0, 1], 'x': [0, 1]}

{'y': [0, 1], 'x': [0, 1]}

{'y': [0, 1], 'x': [0, 1]}

( python)? ( javascript).


EDIT. , tool_events.on_change(), . , TapTool, LassoSelectTool BoxSelectTool, HoverTool:

from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.models.tools import BoxSelectTool, TapTool, HoverTool, LassoSelectTool
from bokeh.models.ranges import Range1d

TOOLS = [TapTool(), LassoSelectTool(), BoxSelectTool(), HoverTool()]
p = figure(tools=TOOLS,
           x_range=Range1d(start=0.0, end=10.0),
           y_range=Range1d(start=0.0, end=10.0))

def tool_events_callback(attr, old, new):
    print attr, 'callback', new

p.tool_events.on_change('geometries', tool_events_callback)
curdoc().add_root(p)

, : ColumnDataSource, Bokeh CustomJS?. , pan tool_events. (TapTool) (Lasso/BoxSelectTool). .

+4
1

, , . , , , GestureTool, / . bokeh ().

$ bokeh serve dir_with_mainfile/

: MouseMoveTool.py:

from bokeh.models import Tool
class MouseMoveTool(Tool):
    # assuming your models are saved in subdirectory models/
    with open('models/MouseMoveTool.coffee', 'r') as f:
        controls = f.read()
    __implementation__ = controls

MouseMoveTool.coffee:

p = require "core/properties"
GestureTool = require "models/tools/gestures/gesture_tool"

class MouseMoveToolView extends GestureTool.View
     ### Override the _pan function ###
     _pan: (e) ->
        frame = @plot_model.frame
        canvas = @plot_view.canvas

        vx = canvas.sx_to_vx(e.bokeh.sx)
        vy = canvas.sy_to_vy(e.bokeh.sy)
        if not frame.contains(vx, vy)
            return null

        # x and y are your mouse coordinates relative to the axes values
        x = frame.x_mappers.default.map_from_target(vx)
        y = frame.y_mappers.default.map_from_target(vy)

        # update the model geometry attribute. this will trigger
        # the tool_events.on_change('geometries', ..) callback
        # in your python code.
        @plot_model.plot.tool_events.geometries = [{x:x, y:y}]

class MouseMoveTool extends GestureTool.Model
    default_view: MouseMoveToolView
    type: "MouseMoveTool"

    tool_name: "Mouse Move Tool"
    icon: "bk-tool-icon-pan"
    event_type: "pan"
    default_order: 13

module.exports =
    Model: MouseMoveTool
    View: MouseMoveToolView

main.py:

from models.MouseMoveTool import MouseMoveTool
p = figure(plot_width=300, plot_height=300, tools=[MouseMoveTool()])
p.tool_events.on_change('geometries', on_mouse_move)

def on_mouse_move(attr, old, new):
    print new[0] # will print {x:.., y:..} coordinates
+4

Source: https://habr.com/ru/post/1656069/


All Articles