feat: vgplot Python API#1007
Open
karthik-anand wants to merge 220 commits into
Open
Conversation
adding recursive_dict_update and get_field_datum_value_defs
Creating generate_mosaic_schema_wrapper.py
handling anyOf and ref instances in schema
* fix: init changes to account for uv * fix: test issue * fix: changes to file structure * chore: Delete generated class file * chore: remove file
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 full Python API for
vgplot, allowing users to build Mosaic visualizations programmatically in Python rather than writing raw JSON/YAML specs by hand. It adds a newpackages/vgplot-pythonpackage and integrates Python spec output across the docs and examples.Key additions
vgplotPython package, a fluent builder API mirroring the vgplot JavaScript API, with Python-idiomatic snake_case naming (e.g.line_y,x_domaininstead oflineY,xDomain)spec_classesmodule, which provides auto-generated Python classes derived from the Mosaic JSON schema, including typed wrappers for all marks, interactors, layout helpers, and plot optionsapi/module, offering higher-level helpers for constructing plots (plot.py), managing data sources (data.py), and working with params/selections (params.py)ast-to-python.js), a JavaScript-side utility that translates parsed Mosaic spec ASTs into equivalent Python code, used to regenerate thespecs/python/examplesdocs/public/specs/python/andspecs/python/has been regenerated using the updated codegentest_full_round_trip.py), which validate that Python-built specs serialize to the same JSON as their source counterpartsHow It's Implemented
The package is structured around a two-layer design:
Schema-derived spec classes are generated at build time from the Mosaic JSON schema via
generate_spec_classes.py. Each class inherits fromSchemaBaseand implementsto_dict()for JSON serialization.High-level API helpers in the
api/module provide more convenient interfaces:plot()composes marks and directives into a plot dict,ParamandSelectionclasses handle reactive state, anddata()helpers reference named datasets. Everything is assembled into aSpecviavg.spec().Naming follows a consistent convention: camelCase option names from the JS API become snake_case in Python. The codegen layer handles this mapping automatically when regenerating examples.
How to Use
Install the package:
Build a basic scatter plot and display it in Jupyter:
Add an interactive selection:
Option names match the vgplot API reference, but in snake_case. For example,
xDomain→x_domain,colorScheme→color_scheme.