Skip to content

Commit 5f475b8

Browse files
committed
feat(altair): add inline option to SVG renderer for opt-out support
1 parent 824cc0b commit 5f475b8

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

marimo/_output/formatters/altair_formatters.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ def _show_chart(chart: AltairChartType) -> tuple[KnownMimeType, str]:
8080
data_url = io_to_data_url(mime_response, mime_type)
8181
return (mime_type, data_url or "")
8282
if isinstance(mime_response, str):
83-
if mime_type == "image/svg+xml":
83+
if (
84+
mime_type == "image/svg+xml"
85+
and not altair.renderers.options.get("inline")
86+
):
8487
svg_bytes = mime_response.encode()
8588
data_url = io_to_data_url(svg_bytes, mime_type)
8689
return (mime_type, data_url or "")

tests/_output/formatters/test_altair_formatters.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,24 +157,34 @@ def test_altair_formatter_mimebundle():
157157

158158

159159
@pytest.mark.skipif(not HAS_DEPS, reason="altair not installed")
160-
def test_altair_formatter_svg():
160+
@pytest.mark.parametrize(
161+
("inline", "expected"),
162+
[
163+
(True, "<svg></svg>"),
164+
(False, "data:image/svg+xml;base64,PHN2Zz48L3N2Zz4="),
165+
],
166+
)
167+
def test_altair_formatter_svg(inline: bool, expected: str):
161168
AltairFormatter().register()
162169

163170
import altair as alt
164171

165172
# Create a mock chart with a _repr_mimebundle_ method that returns SVG
166173
mock_chart = alt.Chart(get_data()).mark_point()
167-
with patch.object(
168-
alt.Chart,
169-
"_repr_mimebundle_",
170-
return_value={"image/svg+xml": "<svg></svg>"},
174+
with (
175+
patch.dict(alt.renderers.options, {"inline": inline}),
176+
patch.object(
177+
alt.Chart,
178+
"_repr_mimebundle_",
179+
return_value={"image/svg+xml": "<svg></svg>"},
180+
),
171181
):
172182
formatter = get_formatter(mock_chart)
173183
assert formatter is not None
174184
mime, content = formatter(mock_chart)
175185

176186
assert mime == "image/svg+xml"
177-
assert content == "data:image/svg+xml;base64,PHN2Zz48L3N2Zz4="
187+
assert content == expected
178188

179189

180190
@pytest.mark.skipif(not HAS_DEPS, reason="altair not installed")

0 commit comments

Comments
 (0)