2017-05-31 35 views
2

我正在创建包含多个图像的散景图。 我创建并显示我的文件是这样的:在景中保存带有图像的html文件

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

然后,它显示了我的情节,并创建我的目录“图形”一个graph.html文件。但是当我稍后打开html时,该图不包含图像。 如何保存html文件,以便它包含图像?

回答

3

由于文档中提到,您有两种方法来实现:使用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) 
    
+0

我尝试了这两个选项,我仍然遇到图像没有显示在保存的html文件中的问题。 – Lotte

+0

在这种情况下,这可能是CSS/JS资源的问题。您可以在打开HTML文件时共享Chrome控制台的屏幕截图吗? – filaton

+0

它说: [散景] ImageURL 0次重试后无法加载[mypath]图像 但是图像有路径[mypath]。 – Lotte

0

在你碰到的问题(在离线环境特别工作)的情况下,你可能需要考虑增加模式=“内联”参数:

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) 

退房this answer以备将来参考。