feat: vgplot Python API#1007
Closed
karthik-anand wants to merge 310 commits into
Closed
Conversation
domoritz
requested changes
Jun 25, 2026
domoritz
left a comment
Member
There was a problem hiding this comment.
Noticed that we still use some string references in the specs and that the widget needs to be updated. Hopefully those are small changes.
Co-authored-by: Dominik Moritz <589034+domoritz@users.noreply.github.com>
… case insensitive for tab labels
Member
domoritz
reviewed
Jul 6, 2026
domoritz
left a comment
Member
There was a problem hiding this comment.
Thank you @karthik-anand @SayeVikram @sittingthyme @Hamzu24 @Xinyue-Yang for all these changes!
Member
|
@karthik-anand @SayeVikram, why did we remove the API generation from the schema in cmudig#24? I'm worried about maintainability of the vgplot python api if we don't automatically update when the js api changes. |
Member
|
Closed in favor of #1067 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

This PR introduces a Python API for
vgplot, letting users build Mosaic visualizations programmatically in Python instead of writing raw JSON/YAML specs by hand. It adds a newpackages/vgplot/vgplot-pythonpackage (published to PyPI asvgplot) and wires Python spec output into the docs and examples.Key additions
vgplotPython package (packages/vgplot/vgplot-python) — a fluent builder API mirroring the vgplot JavaScript API, using Python-idiomatic snake_case naming (e.g.line_y,x_domaininstead oflineY,xDomain). Python keywords are escaped with a trailing underscore (from_,for_).Viewobjects —vg.plot(),vg.vconcat(), andvg.hconcat()returnViewobjects that render directly in Jupyter/marimo and export to JSON.vg.parquet(),vg.csv(),vg.table(),vg.spatial()) and params/selections declared as local variables are discovered from the caller's frame at render/export time; no explicit registration or passing required.packages/vgplot/spec/src/ast-to-python.js) — a JavaScript-side utility that translates parsed Mosaic spec ASTs into equivalent Python, used to regenerate thespecs/python/anddocs/public/specs/python/examples. Regenerate withnpm run docs:examples.test_full_round_trip.py) — verify that running each generated Python spec produces JSON identical to itsspecs/json/reference fixture, and that the Python/JSON/ESM example sets stay in sync.How it works
The package is hand-written fluent helpers over Mosaic's spec format (not codegen from a schema):
Builder functions (
plot.py,data.py,params.py,encodings.py) construct marks, directives, data sources, params/selections, and input widgets. Common marks and attributes have explicit helpers; anything else is reachable by its snake_case name via a dynamic__getattr__fallback (e.g.vg.regression_y(...),vg.x_tick_rotate(45)) that maps snake_case → camelCase.Viewserialization (spec.py) — aViewholds the composed layout, and atto_dict()/to_json()/ display time it inspects the caller's local variables to collect theDataDefand param objects it references, assembling a complete Mosaic spec. The camelCase ↔ snake_case mapping is applied during serialization.Option names match the vgplot API reference but in snake_case (
xDomain→x_domain,colorScheme→color_scheme,filterBy→filter_by).How to use
Install the package (add
mosaic-widgetto render in notebooks):Build a scatter plot and render it in Jupyter or marimo:
Add an interactive crossfilter selection:
Bind a scalar param to an input widget:
Data sources and params are plain local variables —
viewdiscovers them automatically by name at render time, so novg.data()wrapper or explicit passing is required. Export withview.to_dict()(a plain dict) orview.to_json(indent=2)(a JSON string).