Generating Self‑Contained HTML Plots with Plotly Offline Mode
Learn how to export Plotly figures to a single, self‑contained HTML file using offline mode, including code, verification steps, size limits, and common pitfalls.
20 Jan 2026, 06:25 UTC

How to create an offline HTML plot
Plotly’s plotly.offline module can write a figure to a single HTML file that bundles the Plotly.js library as a base64‑encoded string. This lets you share interactive charts without needing a web server or internet connection.
Code example
import plotly.graph_objects as go
fig = go.Figure(data=go.Bar(x=['Product A','Product B'], y=[15, 30]))
fig.write_html('sales_chart.html', include_plotlyjs=True, auto_open=False)
Run the snippet in a Python environment where Plotly ≥ 4.0 is installed and you have write permission to the current directory. The command creates sales_chart.html in the same folder.
Verifying the result
- Open
sales_chart.htmlin a browser; you should see an interactive bar chart. - View the page source (right‑click → View Page Source) and search for
data:application/javascript;base64,. A long base64 block indicates the Plotly.js library is embedded.
Limits and file‑size considerations
The generated file includes the full Plotly.js library (~2 MB when compressed as base64). Consequently, the HTML file is often larger than 1 MB. Adding many data points increases the size further because the data are serialized into the HTML.
Common mistakes
- Leaving
auto_open=True(the default) when generating many charts in a loop opens a browser window for each file, which can be disruptive. - Setting
include_plotlyjs='cdn'while expecting offline use; the resulting HTML relies on an external CDN and fails without internet. - Assuming the HTML will work with any Plotly.js version; if the viewer has a different version embedded elsewhere, mismatches can cause rendering glitches.
- Storing sensitive data directly in the HTML; the data appear in plain text within the file.
Practical way to check for problems
After creating the file, check its size with ls -lh sales_chart.html (Linux/macOS) or the file properties dialog (Windows). If the size is unexpectedly small, verify that include_plotlyjs=True was used. Open the file in a browser without network connectivity (e.g., turn off Wi‑Fi) to confirm true offline operation.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.