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

Add embed_options to JupyterChart and pass them through in "jupyter" renderer #3304

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion altair/jupyter/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,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 @@ -134,6 +135,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 @@ -109,6 +109,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 All @@ -129,6 +130,7 @@ def __init__(
debounce_wait: int = 10,
max_wait: bool = True,
debug: bool = False,
embed_options: Optional[dict] = None,
**kwargs: Any,
):
"""
Expand All @@ -148,6 +150,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 @@ -156,6 +161,7 @@ def __init__(
debounce_wait=debounce_wait,
max_wait=max_wait,
debug=debug,
embed_options=embed_options,
**kwargs,
)

Expand Down
8 changes: 6 additions & 2 deletions altair/vegalite/v5/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,17 @@ def svg_renderer(spec: dict, **metadata) -> Dict[str, str]:
)


def jupyter_renderer(spec: dict):
def jupyter_renderer(spec: dict, **metadata):
"""Render chart using the JupyterChart Jupyter Widget"""
from altair import Chart, JupyterChart

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]
)