Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Plotly · Module

fig.write_html

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.

How it works

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.

Syntax

fig.write_html(file, config=None, auto_play=True, include_plotlyjs=True, full_html=True, default_width="100%", default_height="100%", validate=True, **kwargs)

Parameters

NameTypeDescription
figplotly.graph_objects.FigureFigure to serialize and export.
filestr or pathlib.Path or file-like objectDestination path or file-like object for the HTML output.
...keyword argumentsConfiguration and HTML-export options, including config, auto_play, include_plotlyjs, and full_html.

Returns

Writes the figure's HTML representation to the requested destination.

Key points

  • Call write_html on any Plotly figure to export it as an interactive HTML file.
  • The exported document keeps the figure interactive when opened in a browser.
  • The default output is a complete standalone HTML document.
  • Use include_plotlyjs to control how the Plotly.js dependency is supplied.
  • Choose to_html instead when the HTML needs to remain in a Python string rather than being written to disk.

Common mistakes

  • The default self-contained output can produce a large file because it embeds the Plotly.js library; choose an alternative include_plotlyjs setting when file size matters.
  • Set full_html=False when the result will be inserted into an existing HTML template instead of used as a complete document.

Example: Export an interactive figure

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.

When to use it

Export an interactive report

Sharing a chart as a browser-openable artifact

Embed in a template

Adding a figure to an existing HTML page

Build HTML dynamically

Keeping generated markup in memory

Interview questions

QWhat does 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.

QWhy can the generated HTML file be large?

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.

QHow does 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_html
  • plotly.io.write_html
  • include_plotlyjs
  • full_html

Source: Interactive HTML Export in Python - Plotly. Checked 2026-10-15.