Bokeh:双击圆圈时回调JS

时间:2016-09-10 12:38:23

标签: javascript python callback bokeh

我在单击圈子时在网页上显示一些信息,现在我想在双击时添加一些互动:打开一个新的标签,加载一个网址,希望使用OpenURL

这是我目前的代码:

p = bokeh.plotting.figure(tools=TOOLS)
cr = p.circle(y_true[:, 1], y_pred[:, 1], size=5, source=source)

taptool = p.select(type=bokeh.models.TapTool)
taptool.callback = bokeh.models.CustomJS(args={'circle': cr.data_source},
        code=self.show_data_callback())

show_data_callback只返回一个包含JS代码的字符串,该代码显示有关所点击圈子的信息。

1 个答案:

答案 0 :(得分:5)

按照here中的教程,在散景聊天的帮助下,我为点击和双击动作创建了一个不同操作的扩展程序:

from bokeh.core.properties import Instance
from bokeh.io import output_file, show
from bokeh.models import ColumnDataSource, TapTool, CustomJS, OpenURL
from bokeh.plotting import figure

output_file('tool.html')

JS_CODE = """
p = require "core/properties"
TapTool = require "models/tools/gestures/tap_tool"

class NewTapToolView extends TapTool.View

  _get_canvas_position: (e) ->
    canvas = @plot_view.canvas
    vx = canvas.sx_to_vx(e.bokeh.sx)
    vy = canvas.sy_to_vy(e.bokeh.sy)
    return [vx, vy]

  _tap: (e) ->
    console.log('click')
    [vx, vy] = @_get_canvas_position(e)
    append = e.srcEvent.shiftKey ? false
    @_select(vx, vy, true, append)

  _doubletap: (e) ->
    console.log('double click')
    [vx, vy] = @_get_canvas_position(e)
    append = false
    @_select(vx, vy, true, append)

class NewTapTool extends TapTool.Model
  default_view: NewTapToolView
  type: "NewTapTool"

  tool_name: "New Tap Tool"

  @define { source: [ p.Instance ] }

module.exports =
  Model: NewTapTool
  View: NewTapToolView
"""


class NewTapTool(TapTool):
    __implementation__ = JS_CODE
    source = Instance(ColumnDataSource)


x = y = [i for i in range(10)]
source = ColumnDataSource(data=dict(x=x, y=y))

plot = figure(x_range=(0, 10), y_range=(0, 10), tools=[NewTapTool(source=source)])
plot.title.text = "Double click on a dot to test"
plot.circle('x', 'y', source=source, size=10)

newtaptool = plot.select(type=TapTool)
newtaptool.callback = OpenURL(url="https://en.wikipedia.org/")

show(plot)

该示例只需双击圆圈即可打开Wikipedia。在_tap函数中,可以自定义单击的行为。我希望它有所帮助!