Black Lives Matter. Please consider donating to Black Girls Code today.

Proper way to save a plot to HTML?

This might be a simple question but I can’t find any documentation on this. I’m trying to run my python script and am saving my plots offline with py.plot(fig,filename='sampleplot.html)

This is all good and the plot saves, but whenever I run the script I get an error on my command line: xdg-open: no method available for opening 'C:/sampleplot.html'

Am I saving it in the wrong format? Don’t get why it’s trying to open the plot, I just want it to be saved. Is there a savefig function similar to matplotlib? Thanks for the help

1 Like

@ronenshohat py.plot(fig, filename=name.html') sends the plot to Plotly cloud, and doesn’t save the file on your system.

If you are working offline, then plotly.offline.plot(fig, filename='name.html') saves the html file on your machine and you can open it in a browser, but usually it opens instantly.

1 Like

yeah I’m using polty.offline.plot. It saves the html file to my machine, but whenever I run the script I think it’s trying to open the file in my command terminal and I get an xdg-open error. Is there any way to stop this from being outputted?

Run your py file from Jupyter notebook or a Python IDE, not from terminal.

But I just want to save the HTML file, not open up the chart once it’s run- does that make sense? Is there no way to do this? I thought there was an entire thing of functionality built into plotly offline

1 Like

use auto_open=False:

plotly.offline.plot(fig, filename = 'filename.html', auto_open=False)
2 Likes

Brilliant! Was wondering why do my exports freeze on the Linux console, that’s why :slight_smile:

That does not work. There is no file created.

As of version 3.8, the best way to save a figure to html is using the plotly.io.write_html function. See the docstring for more info.

-Jon

1 Like

As of version 4.0, the write_html() function is available on go.Figure objects as well, so you can do fig.write_html("path/to/file.html") directly now too.

2 Likes

That was what I needed. Thank you.

Here is the canonical documentation page for this feature: https://plotly.com/python/interactive-html-export/