我正在創建包含多個圖像的散景圖。 我創建並顯示我的文件是這樣的:在景中保存帶有圖像的html文件
output_file(my_dir + "Graphs\\graph")
show(bar)
然後,它顯示了我的情節,並創建我的目錄「圖形」一個graph.html文件。但是當我稍後打開html時,該圖不包含圖像。 如何保存html文件,以便它包含圖像?
我正在創建包含多個圖像的散景圖。 我創建並顯示我的文件是這樣的:在景中保存帶有圖像的html文件
output_file(my_dir + "Graphs\\graph")
show(bar)
然後,它顯示了我的情節,並創建我的目錄「圖形」一個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)
在你碰到的問題(在離線環境特別工作)的情況下,你可能需要考慮增加模式=「內聯」參數:
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以備將來參考。
我嘗試了這兩個選項,我仍然遇到圖像沒有顯示在保存的html文件中的問題。 – Lotte
在這種情況下,這可能是CSS/JS資源的問題。您可以在打開HTML文件時共享Chrome控制檯的屏幕截圖嗎? – filaton
它說: [散景] ImageURL 0次重試後無法加載[mypath]圖像 但是圖像有路徑[mypath]。 – Lotte