Can one switch between output_notebook and output_

2020-08-17 08:03发布

问题:

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()



标签: python bokeh