散景:在MouseClick上将变量传递给URL - OpenURL与CustomJS

Pat*_*ick 5 variables graph mouseclick-event bokeh

我有一个带有一些点的散景图,我试图捕捉属于MouseClick上任何一点的变量.代码示例在本文下面.

使用OpenURL方法一切都按预期工作.变量@ref的值将放入URL中,URL将在新窗口中打开.

但是,使用CustomJS不起作用.taptool回调中的javascript工作(带有URL的链接被添加到当前页面),但现在变量@ref作为字符串传递; 它的值不会放入URL中.

有谁知道如何让CustomJS版本正确地将@ref变量传递给url?

代码示例:

使用OpenURL:

from bokeh.plotting import figure, ColumnDataSource
from bokeh.models import HoverTool, TapTool, OpenURL


        source = ColumnDataSource(
            data=dict(
                x=[x for x in range(0, 200)],
                y=[y for y in range(0, 200)],
                ref=[x + y for x in range(0, 200) for y in range(0, 200)]
            )
        )

        hover = HoverTool(
            tooltips=[
                ("ref", "@ref"),
                ("(x,y)", "($x, $y)"),
            ]
        )

        tools = [hover, 'box_zoom,box_select,crosshair,resize,save,reset, tap']

        url = "http://domain.com/dosomething?reference=@ref"

        fig = figure(x_range=(0, 200), y_range=(0, 200), tools=tools)
        fig.circle('x', 'y', source=source)

        taptool = fig.select(type=TapTool)
        taptool.callback = OpenURL(url=url)
Run Code Online (Sandbox Code Playgroud)

- > URL看起来像:" http://domain.com/dosomething?reference=79 "

使用CustomJS:

from bokeh.plotting import figure, ColumnDataSource
from bokeh.models import HoverTool, TapTool, CustomJS


        source = ColumnDataSource(
            data=dict(
                x=[x for x in range(0, 200)],
                y=[y for y in range(0, 200)],
                ref=[x + y for x in range(0, 200) for y in range(0, 200)]
            )
        )

        hover = HoverTool(
            tooltips=[
                ("ref", "@ref"),
                ("(x,y)", "($x, $y)"),
            ]
        )

        tools = [hover, 'box_zoom,box_select,crosshair,resize,save,reset, tap']

        url = "http://domain.com/dosomething?reference=@ref"

        fig = figure(x_range=(0, 200), y_range=(0, 200), tools=tools)
        fig.circle('x', 'y', source=source)

        taptool = fig.select(type=TapTool)
        taptool.callback = CustomJS(args=dict(source=source), code="""
            var mydiv = document.getElementById("link");
            mydiv.innerHTML = "<a href='""" + url + """'>link</a>";
        """)
Run Code Online (Sandbox Code Playgroud)

- > URL看起来像:" http://domain.com/dosomething?reference=@ref "