Exploring the Different Ways of Converting Figures

The methods that control the conversion of figures start with either to_ or write_. Let’s explore some of the most interesting ones.

Converting figures into HTML

Plotly figures are actually HTML objects together with the JavaScript that makes them interactive. We can easily capture that HTML file if we want to share it with others via email, for example. We might consider having this as a feature in our dashboards. The users can create the chart or report they want, convert it into HTML, download it, and share it with their colleagues.

All we have to do is provide a file path where we want it to be saved. The method also takes several optional parameters for further customization. Let’s convert our figure into HTML and add a config option to make it download the figure image in SVG format. The effect of this will be reflected in the HTML file when the camera icon is clicked. The code for this is straightforward:

fig.write_html('html_plot.html',
   config={'toImageButtonOptions':
      {'format': 'svg'}})

We can now open the file in a browser as a separate HTML file, filling the whole browser screen as shown in the working example below:

Get hands-on with 1200+ tech skills courses.