Skip to content

How to export an interactive chart to a standalone HTML file in Plotly (with Code)

The Short Answer (TL;DR)

Use fig.write_html with include_plotlyjs=True and full_html=True to create a complete Plotly HTML document with its chart library included. Set auto_open=False when an automated script should save the file without launching a browser.

import plotly.express as px
fig = px.line(x=[0, 1, 2, 3], y=[3, 4, 4, 6], markers=True, title="Synthetic measurements")
fig.write_html("standalone-chart.html", include_plotlyjs=True, full_html=True, auto_open=False)

Detailed Implementation

This example uses synthetic data and does not need a network connection. Install the recorded package versions in a separate Python environment before running it.

python -m pip install pandas==3.0.6 plotly==7.1.0
from pathlib import Path
import pandas as pd
import plotly.express as px

data = pd.DataFrame({
    "date": pd.date_range("2026-01-01", periods=5, freq="D"),
    "measurement": [3.0, 4.0, 4.0, 6.0, 5.0],
})
data = data.sort_values("date")
fig = px.line(data, x="date", y="measurement", markers=True,
              labels={"date": "Observation date", "measurement": "Synthetic value"},
              title="Five daily measurements")
fig.update_traces(line_shape="linear")
fig.update_layout(template="plotly_white", height=420)
output = Path("standalone-chart.html")
fig.write_html(output, include_plotlyjs=True, full_html=True, auto_open=False,
               div_id="daily-measurements", config={"responsive": True})
print(f"Saved {output.resolve()} ({output.stat().st_size:,} bytes)")

The saved HTML file contains one interactive line chart with five dated markers and an inline JavaScript library. Hover and zoom remain available when the complete file is opened in a browser.

What makes this file standalone?

The document includes its page markup, figure data and Plotly JavaScript. For this basic line chart, no external script file is referenced. The bundled library makes the file substantially larger than an export that loads JavaScript from a CDN.

How should the result be checked?

Open standalone-chart.html in a browser, inspect all five markers and try hovering or zooming. Keep the complete file when copying it elsewhere. The example writes a file without opening a window, which also suits scheduled data-report generation.

When should full_html=False be used?

Use a fragment when inserting a chart into an HTML page that already supplies its own document structure. A complete standalone document needs full_html=True. Files with several charts can share one library copy through a deliberate page template; changing export options does not change the underlying measurements.

Mathematical Context

The line connects the dated observations in increasing time order. Between adjacent points, the chosen linear shape draws a straight segment: halfway between the first two dates, its displayed height is (3+4)/2 = 3.5. That value is a graphical interpolation, not an additional observation or a forecast. Exporting the figure serializes its coordinates and settings; it does not fit a statistical model or attach uncertainty to those segments.

\[ y(t)=y_i+\frac{t-t_i}{t_{i+1}-t_i}(y_{i+1}-y_i) \]

Reproducibility

Executed with Python 3.12.14; pandas 3.0.6, plotly 7.1.0. Automated checks confirm the stated output properties.

Primary documentation

Related guides