Export an interactive report
Sharing a chart as a browser-openable artifact
Plotly · Module
Writes a Plotly figure as an interactive HTML file that can be opened in a browser. The default output is self-contained; export options control dependency inclusion and document structure.
fig.write_html serializes a Plotly figure as an HTML representation and writes it to the specified file. By default, the output is self-contained and interactive; options control browser opening, Plotly.js inclusion, and whether to emit a complete document or embeddable figure content.
fig.write_html(file, config=None, auto_play=True, include_plotlyjs=True, full_html=True, default_width="100%", default_height="100%", validate=True, **kwargs)| Name | Type | Description |
|---|---|---|
fig | plotly.graph_objects.Figure | Figure to serialize and export. |
file | str or pathlib.Path or file-like object | Destination path or file-like object for the HTML output. |
... | keyword arguments | Configuration and HTML-export options, including config, auto_play, include_plotlyjs, and full_html. |
Writes the figure's HTML representation to the requested destination.
write_html on any Plotly figure to export it as an interactive HTML file.include_plotlyjs to control how the Plotly.js dependency is supplied.to_html instead when the HTML needs to remain in a Python string rather than being written to disk.include_plotlyjs setting when file size matters.full_html=False when the result will be inserted into an existing HTML template instead of used as a complete document.import plotly.express as px
fig = px.scatter(x=range(10), y=range(10))
fig.write_html("scatter.html", auto_open=False)Creates a scatter figure and writes an interactive HTML file without opening a browser.
Sharing a chart as a browser-openable artifact
Adding a figure to an existing HTML page
Keeping generated markup in memory
fig.write_html do?It writes a Plotly figure to an HTML file, preserving interactivity for viewing in a browser. The method is available directly on a figure, such as fig.write_html("chart.html").
Follow-up: How would you avoid generating a complete document? Set full_html=False so the output contains only the figure code needed by a surrounding template.
The default output is self-contained because it includes the Plotly.js dependency in the generated HTML. That makes the file usable without fetching the library separately, but increases its size.
Follow-up: What trade-off does include_plotlyjs="cdn" make? It reduces the file by loading Plotly.js from a web CDN, so offline portability is lost.
write_html differ from to_html?Use fig.write_html when writing directly to disk, and use to_html when the HTML should be returned as a Python string. The documentation distinguishes these two output destinations.
Follow-up: When embedding a chart into an existing page, which option is useful? Use full_html=False to omit the surrounding complete HTML document.
plotly.io.to_htmlplotly.io.write_htmlinclude_plotlyjsfull_html