Axolotl
Axolotl

Reputation: 105

Saving html file with images in bokeh

I'm creating a bokeh plot containing several images. I create and show my file like this:

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

It then shows me the plot and creates a graph.html file in my directory "Graphs". But when I open the html later, the plot doesn't contain the images. How can I save the html file, so that it contains the images as well?

Upvotes: 5

Views: 32161

Answers (2)

Guido
Guido

Reputation: 6772

In case you are running into problems (especially working in an offline environment), you may want to consider adding the mode='inline' parameter:

output_file('plot.html', mode='inline')

This ensures that the required js/css is included in your output html. In this way, you create a standalone html.

Combining with the existing code, this results in:

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)

Check out this answer for further reference.

Upvotes: 13

filaton
filaton

Reputation: 2326

As the documentation mentions, you have two ways to achieve this:

  • using save() instead of 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)
    
  • using the file_html function, which is low-level

    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)
    

Upvotes: 29

Related Questions