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

Improve support for Vega and VegaLite as format in save and mimetype #2852

Open
4 tasks
mattijn opened this issue Jan 24, 2023 · 3 comments
Open
4 tasks

Improve support for Vega and VegaLite as format in save and mimetype #2852

mattijn opened this issue Jan 24, 2023 · 3 comments
Labels
breaking Requires a **MAJOR** version bump enhancement

Comments

@mattijn
Copy link
Contributor

mattijn commented Jan 24, 2023

Potential feature requests.

We could associate the file endings .vg and .vg.json with vega and .vl, .vl.json with vega-lite so the files can be opened in JupyterLab https://jupyterlab.readthedocs.io/en/2.2.x/user/file_formats.html#vega-vega-lite

Hm, I just noticed the following variations in the code:
https://github.com/altair-viz/altair/blob/1ed3f6f2fd3647bcf1f2e5e81dd328a0f9cf623d/altair/utils/mimebundle.py#L72-L77

It seems there is different type of json, namely, application/json and application/vnd.vegalite.v5+json (for altair 5). So I'll include vega-lite also as format, it probably would be better if it was renamed to something as vl-json.

Does it make sense to factor out this list into a variable and use it in the strings everywhere? Feels like this is repeated quite often. (red. FORMATS = ["html", "json", "png", "svg", "pdf", "vega", "vega-lite"])

This list should also be updated in the docstring of v5/api.py:TopLevelMixin.save

I can't comment on the correct line but vega does not work here. It will try to access the mimebundle with key image/svg+xml on line 169.

Currently there is no support for vega or vega-lite as output format. This could be supported, both options would be json, but vega would be a compiled version of the Vega-Lite specification with application/vnd.vega.v5+json mime type where the vega-lite would be application/vnd.vegalite.v5+json. This is linked to bullet number 1.

@binste
Copy link
Contributor

binste commented Jan 25, 2023

For the save method, there are no mimebundles to be saved or returned so I think format='vega-lite' will be equivalent to format='json' except that we could extend the part where format is determined based on the file extension https://github.com/altair-viz/altair/blob/master/altair/utils/save.py#L84 so that .vl and .vl.json are linked to vega-lite, rest same as json.

@mattijn mattijn changed the title associate file endings with vega or vegalite to support direct rendering in jupyter Improve support for Vega and VegaLite as format in save and mimetype Jan 25, 2023
@dangotbanned
Copy link
Member

Adding a link to the stable docs: https://jupyterlab.readthedocs.io/en/stable/user/file_formats.html#vega-vega-lite

Docs

Vega/Vega-Lite

JupyterLab Vega-lite rendering (YouTube)
Vega:

  • File extensions: .vg, .vg.json
  • MIME type: application/vnd.vega.v5+json

Vega-Lite:

  • File extensions: .vl, .vl.json
  • MIME type: application/vnd.vegalite.v3+json

Vega and Vega-Lite are declarative visualization grammars that enable visualizations to be encoded as JSON data. For more information, see the documentation of Vega or Vega-Lite. JupyterLab supports rendering Vega 5.x and Vega-Lite 3.x data in files and cell output

@jonmmease would vl_convert.vegalite_to_vega be helpful here?

I just tried this out on https://jupyterlite.github.io/demo/lab/index.html for Vega-Lite:

import altair as alt
from vega_datasets import data

cars = data.cars()

alt.Chart(cars).mark_point().encode(
    x="Horsepower",
    y="Miles_per_Gallon",
    color="Origin",
).save("example1.vl.json", format="json")

With .vl.json suffix

Image

With regular .json suffix

Click to open

Image

Open with

Image

@dangotbanned dangotbanned added the breaking Requires a **MAJOR** version bump label Jan 3, 2025
@dangotbanned
Copy link
Member

Marking this as breaking, since existing code that relies on the .json being added would have a different suffix if this was implemented

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Requires a **MAJOR** version bump enhancement
Projects
None yet
Development

No branches or pull requests

3 participants