在使用Bokeh的IPython笔记本会话中,是否可以在output_notebook和output_file之间切换?

13
我想知道是否有可能在同一个IPython notebook中使用Bokeh生成静态HTML输出和内联图表。目前我看到的情况是,一旦我调用了output_notebook()output_file("myfile.html"),我就必须使用该输出模式。例如,如果我最初使用output_notebook,后来调用output_file不会创建输出文件。

一个更正:看起来我最初可以使用 output_file 然后是 output_notebook,但反过来不行。 - Chris Fonnesbeck
2个回答

13

reset_output()在调用下一个output_notebookoutput_file之前至少在0.10.0版本中起作用。

# cell 1
from bokeh.plotting import figure, show, output_notebook, output_file, reset_output
p = figure(width=300, height=300)
p.line(range(5), range(5))
output_notebook()
show(p)

# cell 2
reset_output()
output_file('foo.html')
show(p)

# cell 3
reset_output()
output_notebook()
show(p)

在笔记本中显示第一和第三项,在浏览器中显示第二项。


0
您可以使用以下代码(改编自此处示例)创建静态 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('test.html', 'w') as f:
    f.write(html)

这个与output_notebook()一起使用没有任何问题


我有一个类似的情况,但我的图表是在循环中创建的。是否可以将每个图表附加到一起,创建一个包含所有图表的单个html文件? - mad5245
对于我的疏忽表示抱歉。我对那个问题想得太多了。作为参考,你只需要像处理其他文件一样,将“w”改为“a”。 - mad5245

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接