简体   繁体   English

Python Bokeh将情节的一部分链接

[英]Python Bokeh make part of the plot a link

I have a figure with multiple plots on it. 我有一个上面有多个图的图。 I would like to make the rectangles on it links. 我想在其链接上制作矩形。 The docs of something similar makes all the plots links, however i would just like the rectangles to be links. 类似的文档使所有图都链接,但是我只想将矩形作为链接。

I tried a lot of thinks with js_on_event , costum_js_on_tap etc... 我用js_on_eventcostum_js_on_tap等尝试了很多想法...

Example try: 示例尝试:

...
plot.line([1, seq_len], [-0.2, -0.2], color="black")
callback = OpenURL(url="google.com")
a = plot.rect(1, 1, 100, 1)
a.js_on_event('tap', callback)
...  

there is probably an easier method but this works for me: 可能有一个更简单的方法,但这对我有用:

from bokeh.models import ColumnDataSource, TapTool,CustomJS
from bokeh.plotting import figure
from bokeh.embed import file_html
from bokeh.resources import INLINE

p = figure(plot_width=400, plot_height=400,
           tools="tap", title="The rectangles are links")

source1 = ColumnDataSource(data=dict(x1=[1],x2=[2],y1=[2],y2=[1],color=["navy"],url=["http://www.google.com"]))
source2 = ColumnDataSource(data=dict(x1=[2],x2=[3],y1=[5],y2=[4],color=["orange"],url=["https://stackoverflow.com"]))
source3 = ColumnDataSource(data=dict(x1=[3],x2=[4],y1=[8],y2=[7],color=["olive"],url=["https://docs.bokeh.org"]))

#glyphs with links
p.quad('x1','x2','y1','y2',color='color',source=source1)
p.quad('x1','x2','y1','y2',color='color',source=source2)
p.quad('x1','x2','y1','y2',color='color',source=source3)

#glyph without a link
p.circle(5, 5, size=20)

jscode="""
s=cb_data.source;
atr=s.attributes;
var d=atr.data;//d is the data dictionary
url=d.url;
url1=url[0]
if (url1.substring(0, 4) == 'http') {
    window.open(url);
} 
"""

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

#save file as .html
with open("bokehRectangleLink.html","w") as f:
    f.write(file_html(p, INLINE, "bokehRectangleLink"))

#open the .html in the browser
import webbrowser
webbrowser.open("bokehRectangleLink.html")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM