Skip to content

Commit

Permalink
Remove vega v5 wrappers (#2829)
Browse files Browse the repository at this point in the history
* restructure, put template code together

* remove vega5 schema generation wrapper code

* remove vega folder from altair

* remove vega v5 magic functions

* remove vega as option for mimebundle

* remove vega as option for save.py

* remove vega test from test_mimebundle

* remove vega tests folder

* update NOTES_FOR_MAINTAINERS

* update pyproject and setup files

* remove vega tests from test_magics

* remove vega docs from importing.rst

* remove `test_vegalite_to_vega_mimebundle` in test_mimebundle

* black

* allow export format as vega in mimebundle.py

* Revert "remove vega test from test_mimebundle"

This reverts commit 7b06730.

* Revert "remove `test_vegalite_to_vega_mimebundle` in test_mimebundle"

This reverts commit 6b82076.

* Revert "remove vega test from test_mimebundle"

This reverts commit 7b06730.

* use not equal to over is not

* allow vega as format but not as mode in save.py

* change `test_spec_to_vega_mimebundle`
to test for deprecation warning and
to test for a valueerror

* mention mode 'vega-lite' as preferred in warning

* refactor save function

* add a line in release note

* Apply suggestions from code review

First round of improving the requested changes.

Co-authored-by: Stefan Binder <[email protected]>

* remove warnings

* remove warnings

* remove warning from test

* solve conflicts to rebase

* remove unused warnings package from import

Co-authored-by: Stefan Binder <[email protected]>
  • Loading branch information
mattijn and binste authored Jan 25, 2023
1 parent e43d292 commit 1f6d1c9
Show file tree
Hide file tree
Showing 23 changed files with 136 additions and 20,314 deletions.
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 @@ -12,7 +12,6 @@
from toolz import curried

from altair.vegalite import v5 as vegalite_v5
from altair.vega import v5 as vega_v5

try:
import yaml
Expand All @@ -23,18 +22,13 @@


RENDERERS = {
"vega": {"5": vega_v5.Vega},
"vega-lite": {
"5": vegalite_v5.VegaLite,
},
}


TRANSFORMERS = {
"vega": {
# Vega doesn't yet have specific data transformers; use vegalite
"5": vegalite_v5.data_transformers,
},
"vega-lite": {
"5": vegalite_v5.data_transformers,
},
Expand Down Expand Up @@ -70,70 +64,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
18 changes: 5 additions & 13 deletions altair/utils/mimebundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ 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']
Expand All @@ -23,7 +23,7 @@ def spec_to_mimebundle(
a dictionary representing a vega-lite plot spec
format : string {'html', 'json', 'png', 'svg', 'pdf', 'vega', '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 @@ -44,15 +44,10 @@ def spec_to_mimebundle(
Note
----
The png, svg, pdf, and vega outputs require the 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-lite":
raise ValueError("mode must be '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"]:
return _spec_to_mimebundle_with_engine(
spec, format, mode, engine=engine, **kwargs
Expand All @@ -68,9 +63,6 @@ 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}
Expand All @@ -91,7 +83,7 @@ def _spec_to_mimebundle_with_engine(spec, format, mode, **kwargs):
a dictionary representing a vega-lite plot spec
format : string {'png', 'svg', 'pdf', 'vega'}
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
71 changes: 43 additions & 28 deletions altair/utils/save.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,44 @@ def write_file_or_filename(fp, content, mode="w"):
fp.write(content)


def set_inspect_format_argument(format, fp, inline):
"""Inspect the format argument in the save function"""
if format is None:
if isinstance(fp, str):
format = fp.split(".")[-1]
elif isinstance(fp, pathlib.PurePath):
format = fp.suffix.lstrip(".")
else:
raise ValueError(
"must specify file format: "
"['png', 'svg', 'pdf', 'html', 'json', 'vega']"
)

if format != "html" and inline:
warnings.warn("inline argument ignored for non HTML formats.")

return format


def set_inspect_mode_argument(mode, embed_options, spec, vegalite_version):
"""Inspect the mode argument in the save function"""
if mode is None:
if "mode" in embed_options:
mode = embed_options["mode"]
elif "$schema" in spec:
mode = spec["$schema"].split("/")[-2]
else:
mode = "vega-lite"

if mode != "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")

return mode


def save(
chart,
fp,
Expand Down Expand Up @@ -45,9 +83,9 @@ def save(
the format to write: one of ['json', 'html', 'png', 'svg', 'pdf'].
If not specified, the format will be determined from the filename.
mode : string (optional)
Either 'vega' or 'vegalite'. If not specified, then infer the mode from
Must be 'vega-lite'. If not specified, then infer the mode from
the '$schema' property of the spec, or the ``opt`` dictionary.
If it's not specified in either of those places, then use 'vegalite'.
If it's not specified in either of those places, then use 'vega-lite'.
vega_version : string (optional)
For html output, the version of vega.js to use
vegalite_version : string (optional)
Expand Down Expand Up @@ -81,34 +119,11 @@ def save(
if embed_options is None:
embed_options = {}

if format is None:
if isinstance(fp, str):
format = fp.split(".")[-1]
elif isinstance(fp, pathlib.PurePath):
format = fp.suffix.lstrip(".")
else:
raise ValueError(
"must specify file format: " "['png', 'svg', 'pdf', 'html', 'json']"
)
format = set_inspect_format_argument(format, fp, inline)

spec = chart.to_dict()

if mode is None:
if "mode" in embed_options:
mode = embed_options["mode"]
elif "$schema" in spec:
mode = spec["$schema"].split("/")[-2]
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 == "vega-lite" and vegalite_version is None:
raise ValueError("must specify vega-lite version")

if format != "html" and inline:
warnings.warn("inline argument ignored for non HTML formats.")
mode = set_inspect_mode_argument(mode, embed_options, spec, vegalite_version)

if format == "json":
json_spec = json.dumps(spec, **json_kwds)
Expand All @@ -128,7 +143,7 @@ def save(
**kwargs,
)
write_file_or_filename(fp, mimebundle["text/html"], mode="w")
elif format in ["png", "svg", "pdf"]:
elif format in ["png", "svg", "pdf", "vega"]:
mimebundle = spec_to_mimebundle(
spec=spec,
format=format,
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.

Loading

0 comments on commit 1f6d1c9

Please sign in to comment.