Share a Plotly chart as a link
Plotly's whole value proposition is that the chart is interactive: hover for the value, drag to zoom, click the legend to isolate a series. Then you share it, and the interaction is the first thing to die. It becomes a PNG in Slack, where nobody can check the number under the peak.
The self-contained HTML file that write_html produces already solves the
technical half. What it does not solve is that the file lives on your disk.
Publish it
import plotly.express as px, requests, os
fig = px.line(df, x="day", y="signups", color="channel")
fig.write_html(
"chart.html",
include_plotlyjs="https://cdn.jsdelivr.net/npm/plotly.js-dist-min@2/plotly.min.js",
)
requests.post(
"https://commareports.com/api/v1/reports",
headers={"Authorization": f"Bearer {os.environ['COMMA_API_TOKEN']}"},
json={"title": "Signups by channel", "html": open("chart.html").read()},
).raise_for_status()
Two ways to handle the library, both fine:
- Inline (
include_plotlyjs=True) — one truly self-contained file, about 3.5 MB of plotly.js, still under the 5 MB HTML cap on its own but tight if you have a lot of data points. - CDN — use jsdelivr, unpkg, cdnjs or esm.sh as above. Plotly's default
"cdn"value points at an origin Comma's CSP does not allow, so pass the full jsdelivr URL instead and the chart loads in a few KB of HTML.