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 / data-ingestion-and-payloads

Data ingestion and payload construction

XY turns user data into a small chart description plus typed data buffers. It keeps a canonical copy of source values for ranges, interaction, and exact readout. It then derives geometry, visual channels, and level-of-detail data for the browser or a static renderer. This page explains that path from input columns to transport data; see the Python chart API for the wider chart model.

Data path

The data path separates source data from the representation sent to a renderer. Input values first become shared canonical columns. Mark builders add the geometry and visual meaning for each trace. Payload construction then selects the data needed for the current screen and places it in binary buffers.

The main flow is:

flowchart LR
    A[Python array-like] --> B[Canonical column]
    B --> C[Mark geometry]
    C --> D[Channels and styles]
    D --> E[Screen-sized payload]
    E --> F[Binary transport]
    F --> G[Browser or static renderer]

The canonical columns remain the source for later range checks, hover rows, selection, appends, and view updates. Encoded buffers and density grids are derived views. A change to the source data can therefore rebuild the transport form without treating that form as the data owner. This separation also connects to level of detail and streaming, where the screen view can change while the source columns stay available.

Input forms

XY accepts common Python array forms and treats them as column data. A chart mark decides which columns must have matching lengths or shapes. A general data source can therefore hold columns for several marks without forcing every column to use the same shape.

Canonical columns

A canonical column is the shared numeric form used inside one figure. Numeric values become contiguous 64-bit floating-point values. Date and time values become milliseconds from the epoch in the same numeric form. Missing values use NaN, so later stages have one missing-value rule. Boolean, complex, non-numeric, and non-one-dimensional geometry columns are rejected at the data boundary. python/…/columns.py• L560-607

The column store reuses a column when several traces refer to the same array. It also keeps per-column range facts for auto-range and later data selection. This makes repeated use of a source array cheap and gives payload construction a stable place to read source values. python/…/columns.py• L315-404

Arrow arrays

PyArrow arrays and chunked arrays are an optional input path. A null-free primitive Arrow array can become a read-only NumPy view without a data copy. Nulls need a materialized array so they can become NaN, and several chunks need to be combined before the column can be read as one sequence. The ingest copy count records these costs. python/…/columns.py• L513-557

Arrow input does not change the chart contract. After conversion, Arrow values follow the same numeric, time, missing-value, length, and shape rules as NumPy or pandas input. The Reflex data layer also keeps its input contract broad: a data method returns a mapping of named array-like columns, while mark binding checks the relationships between those columns. python/…/data_vars.py• L55-84

Column names and shapes

Named data sources describe available columns without embedding chart behavior in the data source. A typed mapping can expose the column names before the data method runs. The chart plan later binds names such as x, y, or a channel column to real values. python/…/data_vars.py• L13-22

Shape rules belong to the mark that understands them. Paired point marks require equal-length x and y columns. A grid mark expects a two-dimensional value matrix and a declared row and column layout. A bar or rectangle mark may derive several edges from one logical value series. Keeping these rules at the mark boundary lets one data source serve different chart families. python/…/data_vars.py• L55-61, python/…/_figure.py• L576-590

Geometry model

Geometry is the data needed to place a mark in chart space. XY stores simple x/y geometry directly and derives extra columns for marks that need edges, baselines, spans, or cells. The payload names those roles so the renderer does not need to infer them from array shape. These roles support the broader chart families and layout model.

Mark geometry

Point, line, and area marks use x/y columns, with area marks carrying a baseline. Rectangular marks use four edge columns. Segment-like marks use endpoint columns. Flow bands and funnels use several span columns that are labeled by their visual role in the payload. Heatmaps use one value grid with its shape and data ranges instead of one rectangle per cell. python/…/_trace.py• L25-41, python/…/_payload.py• L740-796, python/…/_payload.py• L889-1005

The conversion preserves data-space meaning until the renderer stage. The payload records the axis attached to each geometry role. For nonlinear axes, density binning uses axis-scale coordinates so cells have a uniform screen footprint. The raw data ranges remain available for labels and view state. python/…/_payload.py• L556-571, python/…/_payload.py• L1278-1284

Coordinate annotations

Rules, bands, text, markers, arrows, and callouts are chart annotations rather than data traces. Their positions remain data coordinates in the chart specification. Arrow and callout paths are then formed in screen space so curves, end decorations, and label gaps stay tied to the visible layout during pan and zoom. python/…/_annotations.py• L451-541, python/…/_arrowgeom.py• L50-96

This distinction keeps annotations out of data ranges and trace legends. It also lets the browser and static renderers use the same annotation meaning while applying their own drawing surface. Annotation styling and placement belong with the broader styling, themes, and annotations model.

Channel model

Channels carry values that vary across marks but are not part of the main geometry. XY resolves a user value into a declared channel mode. The payload then carries a compact per-item value when the renderer needs one, or keeps a constant value in metadata when it does not.

Color channels

Color input can be a constant paint, numeric values, category labels, or explicit RGB/RGBA values. Numeric values use a domain and a colormap. Categories become stable labels and integer codes that refer to a palette. Explicit RGB/RGBA input stays direct paint instead of becoming categories. python/…/channels.py• L148-210, python/…/channels.py• L540-701

The channel keeps the user-facing domain, labels, categories, and palette in the chart description. The per-item data uses a scalar or a code where possible. A large categorical set can exceed the client palette space, so the resolved channel records the palette behavior and warns rather than hiding a color collision. python/…/channels.py• L103-126, python/…/channels.py• L613-693

Size and style channels

Size input is either one constant pixel value or numeric values mapped to a pixel range. Other per-mark properties, such as opacity, stroke width, symbol, or radius, use the same scalar-or-channel idea. Constants stay in the trace style. Varying values travel in a direct channel with a component count and data type. python/…/channels.py• L213-227, python/…/channels.py• L230-255, python/…/channels.py• L1086-1116

Geometry filtering and channel filtering use the same row selection. If a mark drops rows with non-finite or invalid log-scale values, its color, size, stroke, and style channels drop the matching rows. This keeps every per-item value aligned with the geometry that consumes it. python/…/_payload.py• L494-554, python/…/channels.py• L1119-1129

Density channels

Large scatter data can use a density representation rather than one point per source row. The count grid carries occupancy. A channel-bearing trace can add a per-cell mean color plane, while a constant-color trace can tint the count grid without a separate color plane. The payload records which channels were aggregated or dropped. python/…/_payload.py• L1260-1277, python/…/_payload.py• L1361-1439

The same color rule applies to later density views. A standalone chart can re-bin its retained sample in a worker. A live chart can request a new grid from Python. Both paths aim to keep density color consistent with the points that the surface represents. js/…/46_worker.ts• L1-21, js/…/45_lod.ts• L59-80

Payload model

A payload has two linked parts: JSON-like metadata that describes the chart and a binary area that carries numeric columns. Trace entries point to their geometry and channels through the column table. This keeps large numeric arrays out of JSON and lets the client upload typed data without parsing numbers one by one.

Payload metadata

The payload specification carries chart dimensions, axes, view ranges, trace roles, channel modes, and the column table. Each trace emitter converts one logical trace into that common shape. Direct traces carry their visible rows. Long line and area traces carry a screen-sized reduction. Dense scatter traces carry a grid and its recorded view facts. python/…/_payload.py• L257-287, python/…/_payload.py• L312-352

Geometry columns use relative 32-bit floating-point values with an offset and scale in metadata. The client restores data-space values from those fields. Linear data can keep a stable offset across appends so the old bytes remain a prefix. Log-family axes use a zero origin and apply the axis transform after decoding. python/…/_payload.py• L114-137, python/…/columns.py• L240-262

Binary columns

The payload writer provides a small set of transport forms:

Data roleTransport formPurpose
GeometryOffset-encoded f32Preserve useful position precision with small buffers
Scalar channel or gridf32Carry normalized or final-unit values
Categorical code or density countu8 when the value space allowsReduce data size for lookup and texture inputs
Direct RGBAPacked u8Carry four color components per mark
Selection or transition identityu32Address rows or stable mark identities

The build path keeps continuous channel values as f32 because a standalone client can retain them for approximate readout. Live interaction updates may use u8 for values that are only used as lookup or ramp coordinates because exact hover and pick rows still come from the canonical source. python/…/_payload.py• L72-80, python/…/channels.py• L954-995, python/…/channels.py• L1032-1083

Packed and split buffers

Packed payloads place all columns in one blob and address them by byte offset. Split payloads place each column in its own buffer and address it by buffer index. Both layouts describe the same columns and data types. Split layout avoids joining buffers on live paths, while packed layout is useful for static files and standalone export. python/…/_payload.py• L82-112, spec/…/wire-protocol.md• L336-383

The browser selects the layout from the specification, not from the shape of the received value. It creates typed views over the supplied bytes and checks that every column fits. A mismatch is a transport error rather than a reason to guess, because guessing can pair a valid column with the wrong data. js/…/00_header.ts• L133-171, js/…/60_entries.ts• L18-55

Transport forms

The same chart payload can serve a notebook widget, a live application, or a static page. The transport changes how metadata and buffers move. It does not change the trace or channel meaning.

Live binary transports

The anywidget path and the Reflex /_xy socket path send binary attachments beside small metadata. The browser receives raw buffers and uploads them to its render client. Streaming append uses the same spec-and-buffer contract, so a host can apply a complete refresh while updating only affected traces. spec/…/wire-protocol.md• L241-300, python/…/namespace.py• L11-25

The transport dispatcher handles interaction messages separately from data buffers. It returns replies with optional binary attachments, while client messages today contain metadata only. Invalid client data is dropped instead of being allowed to crash the chart process. This boundary is shared by transports so hover, view, selection, and density requests keep the same meaning. python/…/channel.py• L1-29, python/…/channel.py• L192-239

Static binary transports

Static HTML uses the packed payload and embeds its bytes as chunked base64 because a page has no live binary attachment channel. Reflex static assets use the versioned XYBF frame, which stores strict JSON metadata and aligned raw buffers. The frame decoder returns views into the received body and enforces size limits before a renderer reads the data. spec/…/wire-protocol.md• L439-461, python/…/_framing.py• L1-29, python/…/_framing.py• L222-289

Static output keeps the screen-bounded payload rather than the full source data. It can still support browser-local hover and sample-based density re-bin where the payload retained the needed values. Server-backed drilldown, exact server picks, and streaming require a live data path. These limits are part of the wider export and static rendering behavior. python/…/payload_asset.py• L1-12, js/…/60_entries.ts• L65-93

Transport ownership

Python owns canonical values, validation, range facts, and payload decisions. The browser owns typed buffer views, GPU uploads, screen layout, and local view changes. A static renderer consumes the same payload contract without taking ownership of the source columns. This ownership split explains why interaction and view state can ask Python for exact rows while the browser can pan and zoom without rewriting every source value.

Change checklist

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

  • Keep the canonical missing-value rules aligned with vertex filtering and channel row selection. A new rejection rule must remove the matching channel and source-row mapping.
  • Keep geometry role names aligned across trace construction, Python emitters, and the browser's mark readers. Role swaps can produce a valid-looking but wrong shape.
  • Keep each emitted dtype, offset, scale, component count, and buffer reference aligned with the client buffer-view rules. A packed/split mismatch must fail rather than fall back.
  • Keep build payload precision separate from live-update quantization. Standalone readout needs f32 channel values, while live ramp coordinates may use u8.
  • Keep density metadata honest when a channel is aggregated, omitted, or limited by row identity width. The client uses these facts to choose the right visual treatment.
  • When the frame layout changes, update both the Python XYBF framing contract and the browser decoder. The renderer protocol and transport-frame version are separate contracts.

Important failure modes

TriggerConsequenceGuard
x and y lengths do not matchA trace cannot map one position to one rowMark-level length validation rejects the trace before payload construction
Null or non-finite geometry reaches a vertex bufferGPU primitives can become corrupt or driver-dependentCanonical null counts and payload row filtering remove unsafe rows
A continuous or categorical channel has the wrong row countVisual values attach to the wrong marksChannel resolution checks the expected shape and payload uses the geometry selection
A client receives split metadata with one blob, or packed metadata with a listColumn references point into the wrong address spaceLayout coherence checks reject the pair
A frame exceeds its metadata, buffer, or total-size limitA transport can consume unbounded memoryPython and JavaScript frame encoders and decoders enforce limits
A density trace exceeds the row-id range for point samplingThe density grid can remain exact but point overlay or drill mapping is unavailableThe payload records overlay omission instead of pretending point identities exist

Key files

FileWhy to read it
python/…/columns.pyDefines the canonical column contract, Arrow conversion, time conversion, sharing, range facts, and append behavior.
python/…/marks.pyShows how user inputs become logical mark geometry and resolved channels across chart families.
python/…/_trace.pyDescribes the shared trace data model and the geometry, channel, density, and drill concepts used by emitters.
python/…/channels.pyDefines color, size, direct style, category, colormap, and compact channel representations.
python/…/_payload.pyCompiles traces into metadata and binary columns for direct, reduced, grid, and density forms.
python/…/_framing.pyDefines the versioned static and HTTP binary envelope for metadata plus raw buffers.
python/…/channel.pyDefines the transport-neutral request and reply boundary for interaction messages.
js/…/00_header.tsDefines browser-side buffer views, layout checks, frame decoding, and renderer protocol constants.
js/…/60_entries.tsConnects incoming payloads to the live and standalone browser render paths.
python/…/data_vars.pyDefines named column data sources for data-bound Reflex charts.
python/…/namespace.pyShows how live application messages and binary payloads use the shared socket connection.