3

在一个允许用户绘制某些数据的烧瓶网站上工作时,我决定使用 bokeh 而不是 matplotlib,因为它似乎是为嵌入而构建的,并且能够使用动态数据。我搜索了在线示例和散景文档。在示例中,我看到命令“create_html_snippet”,它应该返回可以插入到模板中的 html 片段:

from bokeh.plotting import *

import numpy as np


# Define a function that will return an HTML snippet.

def build_plot():

# Set the output for our plot.

output_file('plot.html', title='Plot')

# Create some data for our plot.

x_data = np.arange(1, 101)
y_data = np.random.randint(0, 101, 100)

# Create a line plot from our data.

line(x_data, y_data)

# Create an HTML snippet of our plot.

snippet = curplot().create_html_snippet(embed_base_url='../static/js/',
                                        embed_save_loc='./static/js')

# Return the snippet we want to place in our page.

return snippet

我将此代码与下面的主要烧瓶代码一起运行:

from flask import Flask, render_template
from plots import build_plot

app = Flask(__name__)

@app.route('/') # The base URL for the home page.
def render_plot():
plot_snippet = build_plot()

return plot_snippet

if __name__ == "__main__":
app.run(debug=True)

在文档中找不到“create_html_snippet”命令,我的蟒蛇版本的 python(由开发散景的同一个人创建)给出以下错误:

AttributeError:“绘图”对象没有属性“create_html_snippet”

散景似乎正在快速发展,我想知道它是否已被弃用。有谁知道当前获取我正在寻找的 html 片段的最佳方法?

4

1 回答 1

7

create_html_snippet确实已弃用。我们将在 7 月 7 日发布 Bokeh 0.5,现在有一个大大改进、简化和记录的bokeh.embed模块取代了该功能。如果您想尽快尝试,现在有可用的开发版本,说明在邮件列表中:

https://groups.google.com/a/continuum.io/forum/#!topic/bokeh/NVxeqdYy2eQ

您可以在此处查看新的嵌入模块(带有完整的文档字符串):

https://github.com/ContinuumIO/bokeh/blob/master/bokeh/embed.py

以及一个非常好的 Flask 嵌入示例:

https://github.com/ContinuumIO/bokeh/tree/master/examples/embed

我们还没有能力为开发版本发布 Sphinx 文档,但您可以在此处查看新文档的降价文件:

https://github.com/ContinuumIO/bokeh/blob/master/sphinx/source/docs/user_guide.rst#embedding

这些也将得到更多扩展,但它们现在提供了一个很好的概述。

编辑:也就是说,create_html_snippet暂时应该仍然存在并且可以使用。如果您想在 GH 上提出问题,我们可以讨论它或进行更多调查。

于 2014-06-27T04:55:41.343 回答