Can one switch between output_notebook and output_file within an IPython notebook session with Bokeh?

久未见 提交于 2020-01-23 04:48:55

问题


I'm wondering if it is possible to generate static HTML output and inline plots using Bokeh in the same IPython notebook. What I am currently seeing is that once I either call output_notebook() or output_file("myfile.html") I am stuck using that output modality. For example, if I initially use output_notebook, subsequently calling output_file does not create an output file.


回答1:


reset_output() before the next output_notebook or output_file call works at least in version 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)

First and third show in notebook, second shows in browser.




回答2:


You can create a static HTML using the following code (adapted from example here):

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)

This works no trouble in conjunction with output_notebook()



来源:https://stackoverflow.com/questions/25936005/can-one-switch-between-output-notebook-and-output-file-within-an-ipython-noteboo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!