Skip to content Skip to sidebar Skip to footer

Openurl In Bokeh - Open In Same Tab

Considering the example below from the Bokeh Docs, is there a way to adjust the TapTool so that when I click on a circle I'm taken to the url on the same tab rather than opening a

Solution 1:

tags are a dusty feature, and they have nothing to do with this. They simply allow you to attach some arbitrary bit of information to a Bokeh Model, which can help if you are querying the object graph later looking for that particular model .


As of Bokeh 0.12.3, the OpenURL does not support this, it simply calls window.open:

https://github.com/bokeh/bokeh/blob/master/bokehjs/src/coffee/models/callbacks/open_url.coffee#L18

Adding a new property for the name parameter of window.open would be only a couple of lines of code. I'd suggest opening a feature request issue on the issue tracker. If you are interested in working up a PR to implement this feature we are always happy to help new contributors get started.


This could also be done with a CustomJS callback. If you just always want to open a fixed URL whenever a circle is clicked, it's something like

callback = CustomJS(args=dict(source=source), code="""          
    window.open("http://foo.com" ,"_self");
    """)    

If there is a column in your data source that has complete URLs and you want to pick one based on the selected index, you need to do something more like what the OpenURL callback does: get the selected indices off the data source, then get a URL out of a column in the data source using the selected indices, then call window.open. Here is a complete example:

from bokeh.plotting import figure, output_file, show from bokeh.models import CustomJS, ColumnDataSource, TapTool

source = ColumnDataSource(data=dict(
    x = [1, 2],
    y = [3, 4],
    url = ["http://google.com", "http://bing.com"],
))

p = figure(tools="tap")
p.circle('x', 'y', size=15, source=source)

code = """
selection = require("core/util/selection")
indices = selection.get_indices(source)
for (i = 0; i < indices.length; i++) {
    ind = indices[i]
    url = source.data['url'][ind]
    window.open(url, "_self")
}
"""

taptool = p.select(type=TapTool)
taptool.callback = CustomJS(args=dict(source=source), code=code)

output_file("/tmp/foo.html")

show(p)

Post a Comment for "Openurl In Bokeh - Open In Same Tab"