Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove vega-v5 wrappers #2822

Closed
wants to merge 14 commits into from
15 changes: 6 additions & 9 deletions NOTES_FOR_MAINTAINERS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

The core Python API for Altair can be found in the following locations:

- ``altair/vegalite/v2/schema/``
- ``altair/vegalite/v1/schema/``
- ``altair/vega/v3/schema/``
- ``altair/vega/v2/schema/``
- ``altair/vegalite/v5/schema/``

All the files within these directories are created automatically by running
the following script from the root of the repository:
Expand All @@ -19,19 +16,19 @@ $ python tools/generate_schema_wrapper.py

This script does a couple things:

- downloads the appropriate schema files from the specified vega and vega-lite
- downloads the appropriate schema files from the specified vega-lite
release versions & copies the JSON file to the appropriate ``schema``
directory
- generates basic low-level schemapi wrappers from the definitions within
the schema: this is put in the ``schema/core.py`` file
- generates a second layer of higher level wrappers for some vega-lite
functionality; this is put in ``schema/channels.py`` and ``schema/mixins.py``

The script output is designed to be deterministic; if vega/vega-lite versions
are not changed, then running the script should overwrite the schema wrappers
The script output is designed to be deterministic; if the vega-lite version
is not changed, then running the script should overwrite the schema wrappers
with identical copies.

## Updating the Vega & Vega-Lite versions
## Updating the Vega-Lite version

The vega & vega-lite versions for the Python code can be updated by manually
changing the ``SCHEMA_VERSION`` definition within
Expand All @@ -40,7 +37,7 @@ changing the ``SCHEMA_VERSION`` definition within
This will update all of the automatically-generated files in the ``schema``
directory for each version, but please note that it will *not* update other
pieces (for example, the core of the Altair API, including methods and
doc strings within ``altair/vegalite/v2/api.py``.
doc strings within ``altair/vegalite/v5/api.py``.
These additional methods have fairly good test coverage, so running the test
suite should identify any inconsistencies:
```
Expand Down
3 changes: 1 addition & 2 deletions altair/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


def load_ipython_extension(ipython):
from ._magics import vega, vegalite
from ._magics import vegalite

ipython.register_magic_function(vega, "cell")
ipython.register_magic_function(vegalite, "cell")
74 changes: 2 additions & 72 deletions altair/_magics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
Magic functions for rendering vega/vega-lite specifications
Magic functions for rendering vega-lite specifications
"""
__all__ = ["vega", "vegalite"]
__all__ = ["vegalite"]

import json
import warnings
Expand All @@ -14,7 +14,6 @@
from altair.vegalite import v3 as vegalite_v3
from altair.vegalite import v4 as vegalite_v4
from altair.vegalite import v5 as vegalite_v5
from altair.vega import v5 as vega_v5

try:
import yaml
Expand All @@ -25,7 +24,6 @@


RENDERERS = {
"vega": {"5": vega_v5.Vega},
"vega-lite": {
"3": vegalite_v3.VegaLite,
"4": vegalite_v4.VegaLite,
Expand All @@ -35,10 +33,6 @@


TRANSFORMERS = {
"vega": {
# Vega doesn't yet have specific data transformers; use vegalite
"5": vegalite_v5.data_transformers,
},
"vega-lite": {
"3": vegalite_v3.data_transformers,
"4": vegalite_v4.data_transformers,
Expand Down Expand Up @@ -76,70 +70,6 @@ def _get_variable(name):
return ip.user_ns[name]


@magic_arguments.magic_arguments()
@magic_arguments.argument(
"data",
nargs="*",
help="local variable name of a pandas DataFrame to be used as the dataset",
)
@magic_arguments.argument("-v", "--version", dest="version", default="v5")
@magic_arguments.argument("-j", "--json", dest="json", action="store_true")
def vega(line, cell):
"""Cell magic for displaying Vega visualizations in CoLab.

%%vega [name1:variable1 name2:variable2 ...] [--json] [--version='v5']

Visualize the contents of the cell using Vega, optionally specifying
one or more pandas DataFrame objects to be used as the datasets.

If --json is passed, then input is parsed as json rather than yaml.
"""
args = magic_arguments.parse_argstring(vega, line)

existing_versions = {"v5": "5"}
version = existing_versions[args.version]
assert version in RENDERERS["vega"]
Vega = RENDERERS["vega"][version]
data_transformers = TRANSFORMERS["vega"][version]

def namevar(s):
s = s.split(":")
if len(s) == 1:
return s[0], s[0]
elif len(s) == 2:
return s[0], s[1]
else:
raise ValueError("invalid identifier: '{}'".format(s))

try:
data = list(map(namevar, args.data))
except ValueError:
raise ValueError("Could not parse arguments: '{}'".format(line))

if args.json:
spec = json.loads(cell)
elif not YAML_AVAILABLE:
try:
spec = json.loads(cell)
except json.JSONDecodeError:
raise ValueError(
"%%vega: spec is not valid JSON. "
"Install pyyaml to parse spec as yaml"
)
else:
spec = yaml.load(cell, Loader=yaml.SafeLoader)

if data:
spec["data"] = []
for name, val in data:
val = _get_variable(val)
prepped = _prepare_data(val, data_transformers)
prepped["name"] = name
spec["data"].append(prepped)

return Vega(spec)


@magic_arguments.magic_arguments()
@magic_arguments.argument(
"data",
Expand Down
33 changes: 13 additions & 20 deletions altair/utils/mimebundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ def spec_to_mimebundle(
engine=None,
**kwargs,
):
"""Convert a vega/vega-lite specification to a mimebundle
"""Convert a vega-lite specification to a mimebundle

The mimebundle type is controlled by the ``format`` argument, which can be
one of the following ['html', 'json', 'png', 'svg', 'pdf', 'vega', 'vega-lite']
one of the following ['html', 'json', 'png', 'svg', 'pdf', 'vega-lite']

Parameters
----------
spec : dict
a dictionary representing a vega-lite plot spec
format : string {'html', 'json', 'png', 'svg', 'pdf', 'vega', 'vega-lite'}
format : string {'html', 'json', 'png', 'svg', 'pdf', 'vega-lite'}
the file format to be saved.
mode : string {'vega', 'vega-lite'}
mode : string {'vega-lite'}
The rendering mode.
vega_version : string
The version of vega.js to use
Expand All @@ -43,17 +43,13 @@ def spec_to_mimebundle(

Note
----
The png, svg, pdf, and vega outputs require the altair_saver package
The png, svg, and pdf outputs require the vl-convert or altair_saver package
to be installed.
"""
if mode not in ["vega", "vega-lite"]:
raise ValueError("mode must be either 'vega' or 'vega-lite'")

if mode == "vega" and format == "vega":
if vega_version is None:
raise ValueError("Must specify vega_version")
return {"application/vnd.vega.v{}+json".format(vega_version[0]): spec}
if format in ["png", "svg", "pdf", "vega"]:
if mode not in ["vega-lite"]:
raise ValueError("mode must be 'vega-lite'")

if format in ["png", "svg", "pdf"]:
return _spec_to_mimebundle_with_engine(
spec, format, mode, engine=engine, **kwargs
)
Expand All @@ -69,16 +65,13 @@ def spec_to_mimebundle(
return {"text/html": html}
if format == "vega-lite":
assert mode == "vega-lite" # sanity check: should never be False
if mode == "vega":
raise ValueError("Cannot convert a vega spec to vegalite")
if vegalite_version is None:
raise ValueError("Must specify vegalite_version")
return {"application/vnd.vegalite.v{}+json".format(vegalite_version[0]): spec}
if format == "json":
return {"application/json": spec}
raise ValueError(
"format must be one of "
"['html', 'json', 'png', 'svg', 'pdf', 'vega', 'vega-lite']"
"format must be one of ['html', 'json', 'png', 'svg', 'pdf', 'vega-lite']"
)


Expand All @@ -89,9 +82,9 @@ def _spec_to_mimebundle_with_engine(spec, format, mode, **kwargs):
----------
spec : dict
a dictionary representing a vega-lite plot spec
format : string {'png', 'svg', 'pdf', 'vega'}
format : string {'png', 'svg', 'pdf'}
the format of the mimebundle to be returned
mode : string {'vega', 'vega-lite'}
mode : string {'vega-lite'}
The rendering mode.
engine: string {'vl-convert', 'altair_saver'}
the conversion engine to use
Expand Down Expand Up @@ -147,7 +140,7 @@ def _validate_normalize_engine(engine, format):

engine : {None, 'vl-convert', 'altair_saver'}
the user-provided engine string
format : string {'png', 'svg', 'pdf', 'vega'}
format : string {'png', 'svg', 'pdf'}
the format of the mimebundle to be returned
"""
# Try to import vl_convert
Expand Down
4 changes: 2 additions & 2 deletions altair/utils/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def save(
else:
mode = "vega-lite"

if mode not in ["vega", "vega-lite"]:
raise ValueError("mode must be 'vega' or 'vega-lite', " "not '{}'".format(mode))
if mode not in ["vega-lite"]:
raise ValueError("mode must be 'vega-lite', " "not '{}'".format(mode))

if mode == "vega-lite" and vegalite_version is None:
raise ValueError("must specify vega-lite version")
Expand Down
2 changes: 0 additions & 2 deletions altair/vega/__init__.py

This file was deleted.

47 changes: 0 additions & 47 deletions altair/vega/data.py

This file was deleted.

12 changes: 0 additions & 12 deletions altair/vega/display.py

This file was deleted.

14 changes: 0 additions & 14 deletions altair/vega/v5/__init__.py

This file was deleted.

29 changes: 0 additions & 29 deletions altair/vega/v5/data.py

This file was deleted.

Loading