plotly

Plotly.io.to_html

Hypertext Markup Language or HTML documents are markup documents that allow you to display information on the World Wide Web. When you are viewing a specific website, you are using an HTML document.

It is therefore no surprise that you may need to convert a specific Plotly figure into a HTML string.

In this tutorial, we will learn how to use the Plotly io module to convert a figure into an HTML string representation.

Let’s dive in.

Plotly.io.to_html() function

The to_html() function from Plotly’s io module allows you to pass a specific figure as a parameter and convert it into an HTML string.

The function syntax is expressed as shown below:

plotly.io.to_html(fig, config=None, auto_play=True, include_plotlyjs=True, include_mathjax=False, post_script=None, full_html=True, animation_opts=None, default_width='100%', default_height='100%', validate=True, div_id=None)

Let us explore the function parameters, below:

  1. Fig – refers to the figure object or the dictionary representation of the figure.
  2. Config – specifies the plotly.js configuration options. Pass the configuration options as a dictionary.
  3. Auto_play – determines if the animation sequence should play automatically on the page load. This parameter is useful if you have a figure containing animated frames.
  4. Include_plotlyjs – this parameter specifies how the plotly.js library is included in the HTML. Accepted values in this parameter include:
    • True – includes the plotly.js library as a script tag in the HTML document. This will increase the file size but will allow the file to be used in offline environments.
    • ‘cdn’ – setting the include_plotlyjs parameter to ‘cdn’ will add a script tag pointing to a remote link for the plotly.js library. Use this option when in a networked environment or file size-sensitive environment.
    • ‘directory’ – includes a script tag that points to an external plotly.min.js bundle located in the same directory as the HTML document.
    • ‘require’ – setting the value to require will load the plotly.js library using require.js. Ensure that require.js is installed and available globally on the system.
    • ‘False’ – does not include the plotly.js library. Avoid this value when setting the full_html parameter to True as it will render the entire document, unusable.
  5. include_mathjax – specifies if the file should include the mathjax.js library. Accepted values are similar to include_plotlyjs except the ‘require’ value.
  6. Post_script – allows you to include JavaScript snippets that are executed after the plot creation.
  7. Full_html – if true, the function will return a string containing the entire HTML document, starting with the <html> tags. If false, the function will return an HTML string starting with the <div> element.
  8. Animation_opts – allows you to specify custom animation properties as a dictionary. These values are then passed to the plotly.animate function in plotly.js.
  9. Default_width – specifies the default width of the resulting figure in pixels.
  10. Default_height – specifies the default height of the figure in pixels.
  11. Validate – determines if the figure should be validated before being converted to an HTML string.
  12. Div_id – specifies a value of the id attribute of the div tag where the plot is located.

Then, the function returns a string representation of the figure as a string.

Example Usage

The following code shows how to use the to_html function to convert a figure into an HTML string representation.

import plotly.express as px

df = px.data.stocks(indexed=True)

fig = px.area(df, facet_col='company', facet_col_wrap=2)

fig.show()

In the example above, we start by importing the Plotly express module as px. We then create a data frame using the stock data from Plotly.

Finally, we create a facetted area plot using the px.area function. This should return a figure as shown below:

We can convert the above figure into an HTML string representation as shown in the code below:

import plotly.io as io

io.to_html(fig, full_html=False)

Running the code above should return a massive HTML string as shown in the screenshot below:

Note that the HTML document starts at the <div> element. If you want to include the entire HTML file, set the parameter full_html=True as shown:

import plotly.io as io

io.to_html(fig, full_html=True)

Notice the markup starts at the <html> tag.

To include plotly.js, we can set the parameter as:

import plotly.io as io

io.to_html(fig, include_plotlyjs='cdn', full_html=True)

In the example above, we tell the function to include the plotly.js library via a CDN link.

We can verify this from the output below:

The best way to use the to_html document is by exporting the HTML string into an HTML file.

We can do this by running a simple code as shown below:

import plotly.io as io

with open('facetted.html', 'w') as f:

f.writelines(io.to_html(fig, include_plotlyjs='cnd', full_html=True))

In this example, we use the file module in Python to write the HTML string as aN html file.

We can then open the HTML document in the browser and view the figure as shown:

Conclusion

Congratulations, you have successfully exported your Plotly figure as an HTML string using the plotly.io.to_html function.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list