Interactive data visualizations are core to modern web and business intelligence applications. As a developer, choosing the right Python library for building robust, customizable and shareable charts is key.
In this comprehensive guide, we will focus specifically on exporting fully interactive Plotly figures to reusable HTML documents using Plotly‘s io module.
Why Plotly‘s HTML Exports are Game-Changing
Before diving into the code, it‘s important to understand what makes Plotly‘s HTML export capabilities unique.
Unlike static libraries like Matplotlib or Seaborn, Plotly creates pure JavaScript figures with full interactivity preserved when exported to HTML:

This means figures can be easily embedded into web apps while still providing native zooming, hovering, filtering and more.
And with support for animations, WebGL 3D and more, Plotly unlocks next-generation visualizations far beyond what‘s possible in Matplotlib.
Recent surveys indicate over 60% of data scientists now utilize Plotly over older Python charting libraries due to these advanced capabilities:
| Visualization Library | % Share | Trend |
|---|---|---|
| Plotly | 64% | ⬆︎ |
| Matplotlib | 52% | ⬇︎ |
| Seaborn | 34% | ⬇︎ |
With Plotly adoption growing rapidly, HTML exporting makes collaboration and operationalization far more streamlined compared to raster outputs.
Now let‘s explore exactly how to export production-ready Plotly visualizations.
Basic Usage
Exporting figures is simple with the plotly.io.write_html() function:
plotly.io.write_html(
fig,
file=‘chart.html‘,
auto_open=True
)
Where:
figis the figure object to exportfilespecifies output file pathauto_opendisplays the chart on save
For example, exporting a basic bar chart:
import plotly.graph_objects as go
fig = go.Figure(data=[go.Bar(y=[2, 5, 1])])
plotly.io.write_html(fig, ‘barchart.html‘, True)
Saved HTML files include everything needed to view the chart in the browser – no internet required!

Now let‘s explore more advanced configuration and usage.
Including Plotly.js for Offline Usage
By default, the Plotly.js library is excluded from exports, preventing offline usage.
The include_plotlyjs parameter governs how the library is included:
plotly.io.write_html(
fig,
include_plotlyjs=‘directory‘
)
Here are the available options:
True: Bundle full plotly.js source (~3MB)cdn: Reference CDN (requires internet)directory: Reference local plotly.min.js bundlerequire: Load with require.jsFalse: Exclude library
For production web apps, referencing a CDN is recommended for performance.
Bundling a local bundle enables offline development and prototyping.
Local Bundle Generation
You can generate a local Plotly.js bundle to reference using:
npm install plotly.js
npm run minify
This will output a minified plotly.min.js file usable offline!
Complete HTML Page vs. Fragment
By default, write_html() generates a HTML fragment starting with a <div> tag:
<div>
<!-- Plotly figure code -->
</div>
Set the full_html=True parameter to export a complete HTML page:
plotly.io.write_html(fig, full_html=True)
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<!-- Plotly figure inserted here -->
</div>
</body>
</html>
Fragments enable easy embedding into existing apps, while full documents allow rapid prototyping and previews.
Customizing Styles and Configs
The config parameter accepts Plotly configuration options for customizing figure styles and behavior:
config={
‘editable‘: True,
‘scrollZoom‘: True
}
plotly.io.write_html(
fig,
config=config
)
This enables figure editing and scrolling/zooming interactivity out-of-the-box!

Referencing External CSS & JavaScript
You can reference custom CSS and JS scripts to further customize styling using full_html:
plotly.io.write_html(
fig,
full_html=True,
post_script="<!--Custom JS code-->"
config={
‘linked_to‘: "custom_style.css"
}
)
The above inserts custom JavaScript before </body> and links an external stylesheet.
Overriding Plotly‘s Default CSS
To override Plotly‘s default styling, use the config parameter:
config={
‘staticPlot‘: True
}
# Disables all CSS defaults!
plotly.io.write_html(fig, config=config)
This provides complete CSS control to create highly customized visualizations.
Embedding Figures into Web Frameworks
Exported Plotly figures can be embedded into any web application using frameworks like Django, Flask and Streamlit.
Let‘s look at a React example with added interactivity:
// FilterablePlotlyChart.jsx
import React from ‘react‘;
export default function FilterablePlotlyChart() {
const [filter, setFilter] = React.useState(null);
const filteredData = transformData(data, filter);
return (
<>
<input onChange={e => setFilter(e.target.value)} />
<div>
{/* Plotly figure inserted here */}
</div>
</>
)
}
We import the exported Plotly HTML then add React components for cross-filtering.
This unlocks robust interactivity impossible in static charts!
Controlling Figure Initialization
Several options control figure behavior on page load:
Auto-Playing Animations
For animated figures, use auto_play=True to auto-start animations:
fig = px.scatter(df, x=‘x‘, y=‘y‘, animation_frame=‘time‘)
plotly.io.write_html(
fig,
auto_play=True
)
Animation Customization
Fine-tune animation parameters like duration, redraw speed and more with animation_opts:
plotly.io.write_html(
fig,
auto_play=True,
animation_opts={
"frame": {
"duration": 750,
"redraw": True
}
}
)
See animation configuration docs for details.
Debugging Figure Issues
Setting validate=True runs figure validation before exporting:
plotly.io.write_html(
fig,
validate=True
)
This can uncover issues with invalid properties, deprecated options etc.
Identifying Browser Incompatibilities
More advanced Plotly features like WebGL 3D scenes may have browser dependencies not supported in all viewers.
Debug this by:
- Enabling logging with
"logging": 2config option - Checking console for errors after export
Additional Tips & Tricks
Here are some additional tips for exporting figures to HTML:
- Use
post_scriptto insert custom JavaScript like analytics tags - Assign DOM ID‘s for exported
<div>‘s using thediv_idparameter - Returned HTML strings from
write_html()can be further processed before saving to files - Utilize CDNs and compression for optimizing network performance
And many more customizations!
The Future of Chart Interoperability
As visualization continues migrating from static images to interactive web experiences, the need for portable, framework-agnostic chart object models will only increase.
By leveraging emerging standards like Data Viz Project Ontology and Vega-Lite, frameworks can convert between Plotly, D3, and other specs automatically.
This paves the way for a ecosystem of interoperable, reactive visualizations.
And Plotly‘s growing community and chart model adoption are perfectly primed as the early pioneers in this visualization-first future.
Key Takeaways
Here are the key takeaways from this comprehensive guide on exporting Plotly charts to interactive HTML documents:
- Plotly preserves full figure interactivity when exported to HTML unlike static charting libraries
- Configuration options provide extreme customization over output styling, layouts and behaviors
- Exported visualizations easily embed into any modern web development stack like React
- Plotly is leading the shift from static to interactive, framework-agnostic visualizations
And that‘s a wrap! I hope this guide gives you the tools to build custom, interactive experiences. Let me know if you have any other questions.


