Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into jonmmease/offline_jup…
Browse files Browse the repository at this point in the history
…yter_chart
  • Loading branch information
jonmmease committed Jan 6, 2024
2 parents 0ae167c + 4698361 commit ac46de3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
4 changes: 3 additions & 1 deletion altair/jupyter/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ export async function render({ model, el }) {
model.save_changes();
return;
}
let embedOptions = structuredClone(model.get("embed_options")) ?? undefined;

let api;
try {
api = await vegaEmbed(el, spec);
api = await vegaEmbed(el, spec, embedOptions);
} catch (error) {
showError(error)
return;
Expand Down Expand Up @@ -139,6 +140,7 @@ export async function render({ model, el }) {
}

model.on('change:spec', reembed);
model.on('change:embed_options', reembed);
model.on('change:debounce_wait', reembed);
model.on('change:max_wait', reembed);
await reembed();
Expand Down
8 changes: 7 additions & 1 deletion altair/jupyter/jupyter_chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import anywidget
import traitlets
import pathlib
from typing import Any, Set
from typing import Any, Set, Optional

import altair as alt
from altair.utils._vegafusion_data import (
Expand Down Expand Up @@ -113,6 +113,7 @@ class JupyterChart(anywidget.AnyWidget):
max_wait = traitlets.Bool(default_value=True).tag(sync=True)
local_tz = traitlets.Unicode(default_value=None, allow_none=True).tag(sync=True)
debug = traitlets.Bool(default_value=False)
embed_options = traitlets.Dict(default_value=None, allow_none=True).tag(sync=True)

# Internal selection traitlets
_selection_types = traitlets.Dict()
Expand Down Expand Up @@ -184,6 +185,7 @@ def __init__(
debounce_wait: int = 10,
max_wait: bool = True,
debug: bool = False,
embed_options: Optional[dict] = None,
**kwargs: Any,
):
"""
Expand All @@ -203,6 +205,9 @@ def __init__(
sent until chart interactions have completed.
debug: bool
If True, debug messages will be printed
embed_options: dict
Options to pass to vega-embed.
See https://github.com/vega/vega-embed?tab=readme-ov-file#options
"""
self.params = Params({})
self.selections = Selections({})
Expand All @@ -211,6 +216,7 @@ def __init__(
debounce_wait=debounce_wait,
max_wait=max_wait,
debug=debug,
embed_options=embed_options,
**kwargs,
)

Expand Down
10 changes: 7 additions & 3 deletions altair/utils/_importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ def import_vegafusion() -> ModuleType:
min_version = "1.5.0"
try:
version = importlib_version("vegafusion")
if Version(version) < Version(min_version):
embed_version = importlib_version("vegafusion-python-embed")
if version != embed_version or Version(version) < Version(min_version):
raise RuntimeError(
f"The vegafusion package must be version {min_version} or greater. "
f"Found version {version}"
"The versions of the vegafusion and vegafusion-python-embed packages must match\n"
f"and must be version {min_version} or greater.\n"
f"Found:\n"
f" - vegafusion=={version}\n"
f" - vegafusion-python-embed=={embed_version}\n"
)
import vegafusion as vf # type: ignore

Expand Down
7 changes: 6 additions & 1 deletion altair/vegalite/v5/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,14 @@ def jupyter_renderer(spec: dict, **metadata):
# mypy doesn't see the enable_offline class method for some reason
JupyterChart.enable_offline(offline=offline) # type: ignore[attr-defined]

# propagate embed options
embed_options = metadata.get("embed_options", None)

# Need to ignore attr-defined mypy rule because mypy doesn't see _repr_mimebundle_
# conditionally defined in AnyWidget
return JupyterChart(chart=Chart.from_dict(spec))._repr_mimebundle_() # type: ignore[attr-defined]
return JupyterChart(
chart=Chart.from_dict(spec), embed_options=embed_options
)._repr_mimebundle_() # type: ignore[attr-defined]


html_renderer = HTMLRenderer(
Expand Down
9 changes: 9 additions & 0 deletions tests/vegalite/v5/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,12 @@ def test_json_renderer_embed_options(chart, renderer="json"):
assert set(bundle.keys()) == {mimetype, "text/plain"}
assert bundle[mimetype] == spec
assert metadata == {mimetype: {"option": "foo"}}


def test_jupyter_renderer_mimetype(chart, renderer="jupyter"):
"""Test that we get the expected widget mimetype when the jupyter renderer is enabled"""
with alt.renderers.enable(renderer):
assert (
"application/vnd.jupyter.widget-view+json"
in chart._repr_mimebundle_(None, None)[0]
)

0 comments on commit ac46de3

Please sign in to comment.