Last modified: Dec 31, 2024 By Alexander Williams
Master Plotly's fig.write_html() for HTML Exports
The fig.write_html()
method in Plotly is a powerful tool for saving your interactive visualizations as standalone HTML files. Let's explore how to use this functionality effectively.
Basic Usage of fig.write_html()
Let's start with a simple example of creating and saving a Plotly figure as an HTML file:
import plotly.graph_objects as go
import numpy as np
# Create sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Create figure
fig = go.Figure()
fig.add_trace(go.Scatter(x=x, y=y, name='Sine Wave'))
# Save as HTML file
fig.write_html('sine_wave.html')
When you open 'sine_wave.html' in a web browser, you'll see a fully interactive Plotly figure that can be zoomed, panned, and explored just like in Jupyter notebooks.
Customizing HTML Output
The write_html()
method offers several customization options. Here's how to use some of the most important parameters:
fig.write_html(
'custom_figure.html',
full_html=True, # Generate complete HTML file
include_plotlyjs=True, # Include Plotly.js library
auto_open=True # Automatically open in browser
)
Including Multiple Figures
You can create a single HTML file containing multiple figures. This is particularly useful for creating dashboards or reports. For more layout customization, check out Plotly Update Layout.
# Create multiple figures
fig1 = go.Figure(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]))
fig2 = go.Figure(go.Bar(x=[1, 2, 3], y=[7, 8, 9]))
# Save both figures in separate div elements
with open('multiple_figures.html', 'w') as f:
f.write('')
f.write(fig1.to_html(full_html=False, include_plotlyjs='cdn'))
f.write(fig2.to_html(full_html=False, include_plotlyjs=False))
f.write('')
Optimizing File Size
To reduce the HTML file size, you can use CDN for Plotly.js and configure what components to include:
fig.write_html(
'optimized_figure.html',
include_plotlyjs='cdn', # Use CDN
include_mathjax=False, # Exclude MathJax
config={'displayModeBar': False} # Hide mode bar
)
Advanced Configuration
For more complex visualizations, you might want to customize the interaction modes. This example shows how to combine it with Mastering Plotly Scatter Plots.
config = {
'scrollZoom': True,
'displaylogo': False,
'modeBarButtonsToRemove': ['zoom2d', 'pan2d']
}
fig.write_html(
'advanced_figure.html',
config=config,
auto_play=False,
include_plotlyjs='cdn'
)
Embedding in Existing HTML
You can also generate HTML code to embed in existing web pages. This is useful when integrating with web applications:
# Generate div element only
html_string = fig.to_html(
full_html=False,
include_plotlyjs='cdn',
div_id='my_plot'
)
# Use in template
template = f"""
My Plot
My Interactive Plot
{html_string}
"""
with open('embedded_plot.html', 'w') as f:
f.write(template)
Best Practices
File Size Management: Use CDN for Plotly.js when internet connection is available to reduce file size.
Performance: Consider using include_mathjax=False
if you don't need mathematical expressions.
Compatibility: Test your HTML files across different browsers to ensure consistent behavior.
Conclusion
The fig.write_html()
method is a versatile tool for saving and sharing interactive Plotly visualizations. By understanding its parameters and best practices, you can create efficient and effective HTML exports.