Introducing a new way to review your PRs.

Read the announcement

reflex-dev / xy

Ultra-fast and customizable Python charts

View on GitHub

20 documents · Updated Nov 18, 2026

docs / export-and-static-rendering

Export and static rendering

Export turns a composed chart into a file or byte string for reports, web pages, print, and offline use. The system supports PNG, JPEG, WebP, PDF, SVG, standalone HTML, and mixed batches while keeping the chart data and layout model shared with the live browser view.

Export model

Static output starts from the chart's render payload rather than from a separate chart definition. This keeps axes, marks, annotations, color scales, and level-of-detail choices aligned with the live chart; see Data ingestion and payloads, Chart families and layout, and Python chart API.

Export entry points

The chart API offers byte-returning methods and file-writing methods. to_image() selects an image or document format, while write_image() selects the format from the file extension unless the caller supplies one. to_html() is the direct path for an interactive standalone document. Composed charts and facet grids expose the same broad export surface through their chart objects.

NeedMain entry pointResult
Get encoded output in memorychart.to_image(format)PNG, JPEG, WebP, SVG, or PDF bytes
Save one outputchart.write_image(path)Atomic file write with extension-based format selection
Keep an interactive chartchart.to_html()Self-contained HTML document
Export many chartsxy.write_images(figures, files)Ordered byte results and one file per input

Explicit arguments take priority over declarative export defaults. A chart can also describe its preferred file formats, name, size, scale, background, and quality for Python export and for the live download menu. PDF and HTML remain Python-side formats in that menu because the browser client offers PNG, JPEG, WebP, SVG, and CSV downloads.

References: python/…/export.py• L1126-1251 python/…/_figure.py• L2251-2394 python/…/components.py• L4251-4429 js/…/53_interaction.ts• L1325-1339

Render paths

The system has two rendering paths. The native path is browser-free and is the default for normal static output. The Chromium path opens the same standalone chart document in a headless Chrome-family browser when browser CSS, browser font behavior, WebGL behavior, or supplied author CSS matters.

The path choice is deterministic. Engine.auto uses native rendering unless custom_css is present; Engine.chromium requests browser rendering. SVG always uses the native vector path because a browser screenshot cannot produce vector SVG.

flowchart LR
    A[Chart] --> B[Render payload]
    B --> C{Output request}
    C -->|PNG JPEG WebP PDF SVG| D{Engine policy}
    D -->|Native| E[Browser-free renderer]
    D -->|Chromium| F[Standalone HTML]
    F --> G[Headless Chromium]
    C -->|HTML| F
    E --> H[Bytes or file]
    G --> H

References: python/…/export.py• L30-45 python/…/export.py• L902-921 python/…/export.py• L1054-1123 python/…/_chromium.py• L157-167

Output formats

Each format serves a different downstream use. Raster formats target pixels, SVG targets scalable vector graphics, PDF targets print and document exchange, and HTML keeps a chart live without a Python process.

Raster formats

PNG, JPEG, and WebP produce pixel images. PNG and native WebP preserve transparency; JPEG has no alpha channel and therefore needs an opaque background. The scale value raises raster pixel density without changing the chart's CSS size. quality applies to JPEG and browser-produced lossy WebP, while native WebP stays lossless.

background="auto" keeps the renderer's normal backdrop. An explicit CSS color replaces the full painted chart backdrop, including figure and plot theme paints. background="transparent" is supported where the format has alpha and is rejected for JPEG instead of being silently flattened.

References: python/…/export.py• L924-978 python/…/export.py• L1054-1097 python/…/_raster.py• L3638-3677 tests/test_image_export.py• L70-175

SVG output

SVG is a resolution-independent document generated from the same screen-bounded payload used by the other static paths. Lines and other vector marks remain scalable. Density and heatmap representations use compact embedded raster layers when a grid is the right visual form; chart text, axes, legends, and contour geometry remain SVG content.

The native SVG path mirrors the browser's scales, tick labels, color maps, layout, and mark styles. It can differ where a browser-only CSS expression has no static value. It also has no stylesheet to resolve class names, so CSS class-based styling does not carry into the file.

References: python/…/_svg.py• L1-16 python/…/_svg.py• L7233-7273 tests/test_svg_export.py• L69-100 tests/test_svg_export.py• L301-342

PDF output

PDF is a single-page document sized from the requested chart dimensions. Native PDF keeps text and ordinary chart geometry as vectors. Dense visual layers such as heatmaps and density grids become bounded embedded rasters, which gives the document a hybrid vector-raster form rather than turning the whole chart into one image.

Chromium PDF uses the browser's print surface instead. Native PDF accepts the SVG form produced by the chart renderer, so unsupported SVG features fail clearly instead of being guessed at. This keeps PDF output tied to the chart's supported visual model.

References: python/…/export.py• L1083-1087 python/…/_chromium.py• L496-547 python/…/_pdf.py• L1-37 tests/test_pdf_export.py• L75-167

Standalone HTML

Standalone HTML is an interactive, self-contained chart document. It embeds the chart specification, binary data, and render client so it can open without a kernel or network connection. It keeps the interactions and chart controls supported by the chart, including hover, zoom, pan, and selection where the chart's interaction model allows them; see Interaction and view state.

The document uses an isolated frame when shown in a notebook. Optional custom_css is included in the document so author classes and CSS tokens can work in the standalone page. HTML export does not need Chromium at creation time, but a browser is needed later to display it.

References: python/…/export.py• L285-376 python/…/export.py• L393-414 python/…/_figure.py• L2251-2285 js/…/60_entries.ts• L65-89

Chromium rendering

Chromium rendering is a fidelity path for output that must follow browser CSS and browser drawing behavior. It captures the standalone document after the chart has loaded and painted, so the result reflects the same document structure used by standalone HTML rather than a second chart implementation.

Browser selection

The renderer finds an installed Chrome-family executable through the configured browser setting, the process path, and common installation locations. A missing browser is an explicit error for a browser request; the system does not silently switch to native output because that would change the requested fidelity level.

The browser launch uses a sandbox by default. Some CI and container environments cannot start the sandbox, so the current export path retries without it before reporting failure. Treat that fallback as a deployment concern and use the non-sandbox mode only with trusted input.

References: python/…/export.py• L58-84 python/…/export.py• L473-519 python/…/export.py• L1034-1052 tests/test_static_client_security.py• L1-120

Browser capture

For raster capture, Chromium sets the requested chart size and device-pixel ratio, waits for page load, waits for painted frames, and captures PNG, JPEG, or WebP. PDF capture uses the same settled page and prints one chart-sized page. Software WebGL is the default for repeatable pixels; hardware WebGL can favor speed over cross-machine sameness.

References: python/…/_chromium.py• L408-490 python/…/_chromium.py• L496-547 python/…/export.py• L548-632

Fidelity boundaries

Static renderers share the chart's data, scales, and layout, but they do not share every styling mechanism. Browser output has a CSS cascade. Native writers read a defined set of chart tokens and static slot styles. This boundary is important when a chart must look the same in a report and in an application; see Styling, themes, and annotations.

Style coverage

The main mark and axis style contracts survive across browser, native raster, and native vector output. Chart-level theme tokens also reach all three paths. Per-slot styles reach native files when the slot describes visible static chrome such as titles, axis labels, tick labels, legends, or colorbars.

Styling sourceBrowser and ChromiumNative rasterNative vector
Mark or axis styleSupportedSupportedSupported
Chart-level style tokensSupportedSupportedSupported
Static text-slot stylesSupportedSupported subsetSupported subset
class_namesSupported with matching CSSNot appliedNot applied
custom_cssSupportedRequires ChromiumNot supported
Live-only chrome stylesSupported when visibleNo output to styleNo output to style

Native raster text has a smaller font property range than vector output. Font family, letter spacing, and some opacity behavior can remain browser or vector concerns. More specific chart or component styling wins over a wider chart-level slot style when both target the same visible item.

References: python/…/_svg.py• L1331-1377 tests/test_export_style_survival.py• L45-63 tests/test_export_style_survival.py• L149-219 spec/…/export.md• L241-300

Theme behavior

Native export uses the theme declared in the Python chart specification. A live browser can also derive colors from host-page CSS, dark-mode classes, or a system color-scheme change. That host-page state is local to the browser and is not sent back through the chart update channel, so Python export can differ from what a user sees in an embedded page.

An explicit export background avoids this particular mismatch by replacing the full backdrop in every supported renderer. It does not make host-page CSS or browser-only style rules available to native output.

References: python/…/export.py• L1011-1031 python/…/_svg.py• L1399-1428 spec/…/export.md• L149-188

Batch export

Batch export produces many files through one planned operation. It accepts figures, composed charts, and mixed formats. The path for each item comes from its extension unless formats= supplies one format for all items or one format per item.

Batch lifecycle

The batch flow resolves the complete work list before it writes a file. This catches length, format, engine, quality, and background errors before output begins. Native items run without a browser. Browser items share one persistent Chromium session, which avoids paying browser startup cost for every chart.

sequenceDiagram
    participant Caller
    participant Batch as Batch exporter
    participant Native as Native renderer
    participant Browser as Chromium session
    participant Disk
    Caller->>Batch: Figures and paths
    Batch->>Batch: Resolve formats and options
    Batch->>Native: Render native items
    Batch->>Browser: Open once for browser items
    Batch->>Browser: Render each browser item
    Batch->>Disk: Atomically write each result
    Batch-->>Caller: Bytes in input order

Each file is written through a temporary file and replacement, so readers do not see a partial file. The operation is not all-or-nothing: if a later item fails, earlier files remain on disk.

References: python/…/export.py• L635-780 python/…/_chromium.py• L157-224 tests/test_batch_export.py• L23-109

Large-data output

Static renderers use level-of-detail data sized for the export view instead of copying every source point into the output. Long lines become screen-sized summaries, and dense marks become density grids. This keeps SVG and raster work tied to output resolution rather than source row count; see Level of detail and streaming.

Standalone HTML embeds its binary payload in the document. Large embedded payloads add base64 size overhead, so HTML is best treated as a portable interactive artifact rather than the smallest archive format.

References: python/…/_payload.py• L257-310 python/…/_svg.py• L1-8 python/…/export.py• L46-56 tests/test_svg_export.py• L301-312

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the format list and extension rules aligned across to_image(), write_image(), batch export, chart export defaults, and the browser download menu.
  • Keep native and browser background handling aligned so an explicit export background replaces both figure and plot theme paints.
  • Keep SVG and PDF support aligned: PDF consumes the SVG writer's supported visual subset and must fail clearly when that subset changes.
  • Recheck the native/browser style boundary when adding a chart style slot or a CSS property. A new live slot may have no static pixels to style.
  • Recheck the payload's screen-size reduction when changing static rendering. Output size and visual detail must remain tied to export dimensions rather than source point count.
  • Keep batch engine planning separate from file writes so invalid mixed-format requests fail before partial output.

Important failure modes

TriggerConsequenceGuard
Chromium is requested but no supported browser is foundBrowser-fidelity export fails instead of silently changing appearanceInstall Chrome, Chromium, Edge, or headless shell, or set the browser executable explicitly
custom_css is passed to native outputThe native renderer cannot apply the author stylesheetUse Chromium; SVG cannot use this option at all
Transparent background is requested for JPEGJPEG cannot store alphaUse an opaque background or choose PNG/WebP/SVG/PDF
quality is passed to native WebPThe request conflicts with the lossless native WebP contractDrop quality or request Chromium WebP
A PDF input contains SVG features outside the chart writer's supported subsetPDF conversion stops rather than producing a wrong documentUse chart-generated SVG or update the SVG/PDF contract together
A batch item fails after earlier items finishEarlier files remain while later files are missingTreat batch output as per-file atomic, not transactional
A host page supplies the visible theme only through CSSPython-side export can differ from the live chartDeclare export-relevant theme tokens in the chart or use browser capture

Key files

FileWhy to read it
python/…/export.pyCentral format routing, engine policy, backgrounds, HTML assembly, and batch behavior
python/…/_chromium.pyChromium session lifecycle and settled-page capture for raster and PDF output
python/…/_svg.pyNative vector rendering, screen-bounded output, static layout, and SVG style coverage
python/…/_pdf.pyNative single-page PDF conversion and the supported SVG-to-PDF visual boundary
python/…/_raster.pyNative raster composition and the shared pixel source for PNG, JPEG, and WebP
python/…/_payload.pyShared payload construction and output-size level-of-detail decisions
python/…/_figure.pyFigure-facing HTML, SVG, PNG, unified image, and file-writing methods
python/…/components.pyComposed-chart export defaults and chart-facing export methods
js/…/60_entries.tsStandalone HTML hydration and the kernel-free render entry point
tests/test_image_export.pyFormat matrix, dimensions, backgrounds, quality, engine routing, and write behavior
tests/test_export_style_survival.pyThe enforced boundary for styling across browser, native raster, and native vector output
tests/test_batch_export.pyBatch input contracts and the browser-resolved batch path