Search code examples
pythonhyperlinkbokeh

How can I open an absolute url using `OpenURL` in bokeh?


I have the following code which plots a point at (1.0, 1.0). I want to be able to click this point to open the browser to the page designated by the url attribute specified:

from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.models import ColumnDataSource, TapTool, OpenURL

import pandas as pd

df = pd.DataFrame({"x": [1.0], "y": [1.0], "url": ["https://www.google.com"]})

datasource = ColumnDataSource(df)

fig = figure(
    title='test',
    plot_width=600,
    plot_height=600,
    tools=('pan, wheel_zoom, reset, tap')
)

p = fig.select(type=TapTool)
p.callback = OpenURL(url="@url")

fig.circle(
    'x',
    'y',
    source=datasource,
    line_alpha=0.6,
    fill_alpha=0.6,
    size=10
)

curdoc().add_root(fig)

However, when I run the app and click the point, a new tab opens to http://localhost:5006/https%3A%2F%2Fwww.google.com which seems to suggest bokeh wants to open the URL relative to the tornado server. How can I simply open the absolute URL instead of a relative URL?


Solution

  • This is a bug in the current version:

    https://github.com/bokeh/bokeh/issues/11182

    For now you will have to downgrade, or wait for the next 2.3.2 point release.

    (Or you could also use a plain CustomJS and construct your own call to window.open)