Bokeh Plot的补充JavaScript

时间:2015-04-05 20:54:52

标签: python bokeh

假设我有一个非常简单的散景情节:

from bokeh.plotting import figure
p = figure(title="simple line example", x_axis_label='x', y_axis_label='y')
p.line([1, 2, 3], [1, 4, 9])

该图很好地生成HTML

html = p.__repr_html__()

为了将其嵌入网页,我需要的所有其他内容是什么?我更喜欢链接到外部托管的JavaScript而不是内联所有内容。

我的理想答案是表格"只需复制粘贴这三行:..."

1 个答案:

答案 0 :(得分:3)

要在您的网页中嵌入Bokeh图而不必内联JS / CSS,您可以将bokeh.embed.componentsbokeh.resources.CDN一起使用,如下例(http://bokeh.pydata.org/en/latest/docs/user_guide/embedding.html#components)所示:

from bokeh.plotting import figure
from bokeh.resources import CDN
from bokeh.embed import components

plot = figure()
plot.circle([1,2], [3,4])

script, div = components(plot, CDN)

考虑到使用这些组件假定BokehJS已经加载,例如在文档文本中内联,或者从CDN加载。

您必须在html页面中添加的CDN标记才能呈现图表,例如对于散景版0.8.2:

<link href="http://cdn.pydata.org/bokeh/release/bokeh-0.8.2.min.css" rel="stylesheet" type="text/css">
<script src="http://cdn.pydata.org/bokeh/release/bokeh-0.8.2.min.js">

确保这些链接与您实际传递给组件的版本相对应。您可以通过以下方式获取这些链接:

In [1]: from bokeh.resources import CDN

In [2]: CDN.js_files
Out[2]: ['http://cdn.pydata.org/bokeh/release/bokeh-0.8.2.min.js']

In [3]: CDN.css_files
Out[3]: ['http://cdn.pydata.org/bokeh/release/bokeh-0.8.2.min.css']