我正在学习Bokeh,我想让示例'stock'脚本https://demo.bokehplots.com/apps/stocks生成一个独立的html文件...我添加了以下代码:
from bokeh.embed import components
plots = [corr, ts1, ts2]
script, div = components(plots)
print(script)
print(div)
html = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bokeh Scatter Plots</title>
<link rel="stylesheet" href="http://cdn.pydata.org/bokeh/release/bokeh-0.12.6.min.css" type="text/css" />
<script type="text/javascript" src="http://cdn.pydata.org/bokeh/release/bokeh-0.12.6.min.js"></script>
<!-- COPY/PASTE SCRIPT HERE -->
{}
</head>
<body>
<!-- INSERT DIVS HERE -->
{}
</body>
</html>""".format(script, div)
with open("plotstock.html", "w") as f:
f.write(html)
位于脚本的底部,这适用于在http://bokeh.pydata.org/en/latest/docs/user_guide/embed.html找到的更简单的示例(没有小部件导入)但是对于库存脚本我得到错误:
散景错误
模型`PreText'不存在。这可能是由于在首次使用之前未注册小部件或自定义模型。**
如果删除对此窗口小部件的引用,我会在下一个窗口小部件中获得相同的错误,即选择窗口小部件...
我可以在散景服务器中运行此脚本,并将数据本地化,但嵌入是问题......
生成的html似乎完整,包含数据和组件部分:
<head>
<meta charset="utf-8">
<title>Bokeh Scatter Plots</title>
<link rel="stylesheet" href="http://cdn.pydata.org/bokeh/release/bokeh-0.12.6.min.css" type="text/css" />
<script type="text/javascript" src="http://cdn.pydata.org/bokeh/release/bokeh-0.12.6.min.js"></script>
<!-- COPY/PASTE SCRIPT HERE -->
<script type="text/javascript">
(function() {
var fn = function() {
Bokeh.safely(function() {
var docs_json = {"15b39cbd-8b49-4761-9152-f666c6fb1d9a":{"roots": {"references":[{"attributes":{"data_source":{"id":"f6f697cf-6d00-4218-94c1- 18c7e97d7e16","type":"ColumnDataSource"},"glyph":{"id":"3764347f-7617-42a8-a1d4- 0a6b255fc47d","type":"Line"},"hover_glyph":null,"muted_glyph":null,"nonselection_ glyph":{"id":"461527a6-e0fe-4159-89a8- c748b3f16335","type":"Line"},"selection_glyph":null},"id":"a6991b17-5e11-4864- 92ed-85b29f7fe737","type":"GlyphRenderer"},{"attributes":{"months": [0,4,8]},"id":"a3ac5382-f490-408f-b608-f3cdca26772b","type":"MonthsTicker"}, {"attr .... ["87c565c2-e8d1-41ea-958d-ea09daa88b3a"]},"title":"Stocks","version":"0.12.6"}};
var render_items = [{"docid":"15b39cbd-8b49-4761-9152-f666c6fb1d9a","elementid":"ac68790a-4571-49cb-8d1c-08c6f3a9d711","modelid":"a5ab1659-6d31-4869-8d5d-184d27fd5732"}, {"docid":"15b39cbd-8b49-4761-9152-f666c6fb1d9a","elementid":"202aa28c-793d-46df-99ed-30cc4d62b827","modelid":"c22f84ba-56d9-49ab-9d81-314546cfec5c"}, {"docid":"15b39cbd-8b49-4761-9152-f666c6fb1d9a","elementid":"725a58d5-6c34-4f56-8d82-64ebef5c4e46","modelid":"e1fc4190-8882-4d48-be12-b46f9817d525"}];
Bokeh.embed.embed_items(docs_json, render_items);
});
};
if (document.readyState != "loading") fn();
else document.addEventListener("DOMContentLoaded", fn);
})();
</script>
</head>
<body>
<!-- INSERT DIVS HERE -->
('\n<div class="bk-root">\n <div class="bk-plotdiv" id="ac68790a-4571-49cb-8d1c-08c6f3a9d711"></div>\n</div>', '\n<div class="bk-root">\n <div class="bk-plotdiv" id="202aa28c-793d-46df-99ed-30cc4d62b827"></div>\n</div>', '\n<div class="bk-root">\n <div class="bk-plotdiv" id="725a58d5-6c34-4f56-8d82-64ebef5c4e46"></div>\n</div>')
</body>
</html>
有人能告诉我发生了什么或有任何想法吗?
感谢。
答案 0 :(得分:0)
Bokeh小部件的JS文件是单独分发的,因此页面只会在实际使用它们时产生加载它们的成本。您需要按照in the documentation所述的CDN添加bokeh-widgets
:
<link
href="http://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.6.min.css"
rel="stylesheet" type="text/css">
<script src="http://cdn.pydata.org/bokeh/release/bokeh-widgets-0.12.6.min.js"></script>