散景中的OpenURL - 在相同的选项卡中打开 [英] OpenURL in bokeh - open in same tab

查看:137
本文介绍了散景中的OpenURL - 在相同的选项卡中打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下来自 Bokeh Docs ,是否有一种方法来调整TapTool,以便当我点击一个圆圈时,我被带到了同一个选项卡上的网址,而不是打开一个新的选项卡?文档字符串表明唯一的行为是打开一个新的选项卡,但也许有一个CustomJS解决方法或其他黑客来解决这个问题?

  from bokeh.models从bokeh.plotting导入ColumnDataSource,OpenURL,TapTool 
导入图,output_file,show

output_file(openurl.html)

p = figure(plot_width = 400,plot_height = 400,
tools =tap,title =点击点)

source = ColumnDataSource(data = dict(
x = [1,2,3,4,5],
y = [2,5,8,2,7],
颜色= [藏青色,橙色,橄榄色, firebrick,gold]
))

p.circle('x','y',color ='color',size = 20,source = source)

url =http://www.colors.commutercreative.com/@color/
taptool = p.select(type = TapTool)
taptool.callback = OpenURL(url = url)

show(p)

借用这个问题,但对于如何确切执行它)。这导致没有链接打开:

  callback = CustomJS(args = dict(source = source),code =
url = data ['url']
window.open(url,_ self);


taptool = p.select(type = TapTool)
taptool.callback = callback

我也尝试将链接视为<$使用OpenURL的标记关键字来创建c $ c>< a> 标记。这是一个盲目的尝试,因为我找不到任何关于如何正确使用标记的术语。这里没有运气。

  url =http://www.colors.commutercreative.com/@color/
taptool = p .select(type = TapTool)
taptool.callback = OpenURL(url = url,tags = [_ self])

我了解Bokeh仍然非常新,所以可能此功能尚未提供。我仍然认为如果你知道足够的JavaScript(我显然没有),那么就有了一个解决方法。 是一种满是灰尘的功能,它们没有任何关系有了这个。它们只是允许你在Bokeh Model 中附加一些任意信息,这可以帮助你在查找特定模型后查询对象图。






自Bokeh 0.12.3 时, OpenURL 不支持这个,它只是调用 window.open



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

为<$ c $添加一个新属性c> name window.open 的参数只有几行代码。我建议在问题跟踪器上打开功能请求问题。如果您有兴趣成立公关部门来实施此功能,我们将乐意帮助新贡献者开始工作。




这也可以通过 CustomJS 回调来完成。如果你只是总想在点击一个圈子时打开一个固定的URL,那就像

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

更类似于 OpenURL 回调函数的作用:从数据源中获取选定的索引,然后使用所选索引从数据源中的列中获取URL 然后调用 window.open 。这里是一个完整的例子:从bokeh.plotting导入figure,output_file,显示
from bokeh.models导入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 /选择)
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)


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 new tab? The docstring suggests that the only behavior is to open a new tab, but perhaps there's a CustomJS workaround or some other hack to get around this?

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

output_file("openurl.html")

p = figure(plot_width=400, plot_height=400,
       tools="tap", title="Click the Dots")

source = ColumnDataSource(data=dict(
    x=[1, 2, 3, 4, 5],
    y=[2, 5, 8, 2, 7],
    color=["navy", "orange", "olive", "firebrick", "gold"]
    ))

p.circle('x', 'y', color='color', size=20, source=source)

url = "http://www.colors.commutercreative.com/@color/"
taptool = p.select(type=TapTool)
taptool.callback = OpenURL(url=url)

show(p)

I wrapping some javascript without success (borrowing from this question but kind of clueless on how exactly to implement it). This results in no link opening:

callback = CustomJS(args=dict(source=source), code="""          
        url = data['url']
        window.open(url,"_self");
    """)    

taptool = p.select(type=TapTool)
taptool.callback = callback

I also tried treating the link like an <a> tag using the tag keyword for OpenURL. This is a blind attempt since I could not find anything on how to use this tag term properly. No luck here.

url = "http://www.colors.commutercreative.com/@color/"
taptool = p.select(type=TapTool)
taptool.callback = OpenURL(url=url, tags=["_self"])

I understand Bokeh is still pretty new so perhaps this functionality isn't available yet. I still think there's got be a workaround if you know enough javascript (which I apparently don't).

解决方案

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)

这篇关于散景中的OpenURL - 在相同的选项卡中打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆