嵌入bokeh图表时遇到问题

3 投票
1 回答
2401 浏览
提问于 2025-04-18 11:17

我在做一个flask网站,目的是让用户能够绘制一些数据。我决定用bokeh而不是matplotlib,因为bokeh似乎更适合嵌入使用,并且可以处理动态数据。我在网上找了很多例子,也看了bokeh的文档。在这些例子中,我看到有个命令叫'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

我把这个代码和下面的主要flask代码一起运行:

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"这个命令,而我用的anaconda版本的python(也是bokeh开发团队做的)报了以下错误:

AttributeError: 'Plot' object has no attribute 'create_html_snippet'

看起来bokeh正在快速发展,我在想这个命令是不是已经被淘汰了。有没有人知道现在获取我想要的html代码片段的最佳方法是什么?

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文档,但你可以在这里查看新的文档的markdown文件:

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

这些文档会进一步扩展,但现在已经能提供一个很好的概述。

补充: 也就是说,create_html_snippet 目前应该还是可以用的。如果你想在GitHub上提个问题,我们可以讨论或者进一步调查。

撰写回答