保存 html 个包含散景图像的文件

Saving html file with images in bokeh

我正在创建一个包含多张图像的散景图。 我这样创建并显示我的文件:

output_file(my_dir + "Graphs\graph")
show(bar)

然后它向我展示了情节并在我的目录 "Graphs" 中创建了一个 graph.html 文件。但是当我稍后打开 html 时,绘图不包含图像。 如何保存 html 文件,使其也包含图像?

如文档所述,您有两种方法可以实现此目的:

  • 使用 save() 而不是 show()

    from bokeh.plotting import figure, output_file, save
    p = figure(title="Basic Title", plot_width=300, plot_height=300)
    p.circle([1, 2], [3, 4])
    output_file("test.html")
    save(p)
    
  • 使用file_html函数,它是低级的

    from bokeh.plotting import figure
    from bokeh.resources import CDN
    from bokeh.embed import file_html
    
    plot = figure()
    plot.circle([1,2], [3,4])
    
    html = file_html(plot, CDN, "my plot")
    
    with open("/myPath.html") as f:
        f.write(html)
    

如果您 运行 遇到问题(尤其是在离线环境中工作),您可能需要考虑添加模式='inline' 参数:

output_file('plot.html', mode='inline')

这可确保所需的 js/css 包含在您的输出 html 中。通过这种方式,您创建了一个独立的 html.

结合现有代码,结果为:

from bokeh.plotting import figure, output_file, save
p = figure(title="Basic Title", plot_width=300, plot_height=300)
p.circle([1, 2], [3, 4])
output_file('plot.html', mode='inline')
save(p)

查看 进一步参考。