5

I have a Bokeh graph with some points in it and I'm trying to catch a variable belonging to any point on MouseClick. Code examples are below this text.

Using the OpenURL method everything works as expected. The value of variable @ref is put into the URL and the URL opens in a new window.

Using CustomJS however this doesn't work. The javascript in the taptool callback works (a link with the URL is added to the current page), but now variable @ref is passed as a string; its value is not put into the URL.

Does anyone know how to make the CustomJS version pass the @ref variable to the url correctly?

Code examples:

Using 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)

-> URL looks like: "http://domain.com/dosomething?reference=79"

Using 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>";
        """)

-> URL looks like: "http://domain.com/dosomething?reference=@ref"

Patrick
  • 51
  • 2

0 Answers0