Plotly renderer limits for headless container deployment
0 reputation · 03 Nov 2022, 14:58 UTC
0 reputation · 03 Nov 2022, 14:58 UTC
Goal: Configure Plotly so that figures render or export correctly in headless production containers without requiring a graphical display or additional browser dependencies.
Constraints: The solution must avoid installing heavy GUI libraries, remain compatible with Plotly versions 4.x‑5.x, and work both for standalone scripts and Dash callbacks that return images or HTML. Uncertainty remains about which renderer provides the most reliable SVG output globally and whether setting a default renderer affects online plotting mode or offline file generation.
What renderer setting yields consistent SVG output without needing Kaleido or Orca? Is it safe to apply plotly.io.renderers.default = 'svg' globally across development and production? How does this setting interact with plotly.offline.plot and plotly.plot (online) modes?
32775 reputation · 03 Nov 2022, 22:12 UTC
No renderer setting gives you static SVG files without an export engine. plotly.io.renderers.default = 'svg' only changes how figures are displayed interactively (for example, what fig.show() emits in a notebook). It does not power fig.write_image() or fig.to_image(). Plotly's Python library has no pure-Python SVG exporter, so headless file export requires Kaleido (the current, lightweight option) or the deprecated Orca. The good news: Kaleido needs no display server or GUI libraries, so it fits your container constraints.
ValueError about no supported image export engine.Install Kaleido in the container image — it ships as a self-contained binary wheel, no X11, no browser packages:
pip install "plotly>=5" kaleidoThen export explicitly where you need files:
import plotly.graph_objects as go
fig = go.Figure(go.Scatter(y=[1, 2, 3]))
fig.write_image("/tmp/chart.svg") # or fig.to_image(format="svg") for bytesOn Plotly 5.x, Kaleido is picked up automatically once installed. On 4.x you may need engine="kaleido" explicitly in write_image — verify against your pinned version. Make sure the Kaleido wheel matches your container's OS/architecture (linux x86_64 vs arm64); a mismatched wheel is a common silent failure in multi-arch builds.
Dash's interactive figures are rendered client-side in the user's browser, so headless export doesn't apply to normal dcc.Graph output. If a callback must return an image or file, call fig.to_image(format="svg") (or "png") server-side with Kaleido installed and return the bytes via dcc.send_bytes or a download component. If a callback returns HTML, use fig.to_html() — pure Python, no export engine needed.
python -c "import kaleido; print(kaleido.__version__)"
python -c "import plotly.graph_objects as go; go.Figure(go.Scatter(y=[1,2])).write_image('/tmp/t.svg')"
head -c 100 /tmp/t.svg # should start with <?xml or <svgSetting pio.renderers.default = 'svg' globally is harmless but solves a different problem. For consistent headless SVG output across scripts and Dash, install Kaleido and use write_image/to_image; use offline.plot/to_html when HTML output is acceptable and you want zero extra dependencies. Version-specific behavior (4.x engine selection, Kaleido's own version compatibility) should be confirmed against your pinned releases before rollout.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.