Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

altair: set background to transparent for vegafusion if no background is set (#6614)

Shahmir Varqha6c58c1aecb82
diff --git a/.gitignore b/.gitignore
index ca87ece16e..815343939e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -96,6 +96,7 @@ uv.lock
# WIP files
.wip/
+wip/
# editors
*.swp
diff --git a/marimo/_output/formatters/altair_formatters.py b/marimo/_output/formatters/altair_formatters.py
index 13c0f3c003..c79e641c58 100644
--- a/marimo/_output/formatters/altair_formatters.py
+++ b/marimo/_output/formatters/altair_formatters.py
@@ -82,6 +82,14 @@ class AltairFormatter(FormatterFactory):
# If vegafusion is enabled, just wrap in altair_chart
if alt.data_transformers.active.startswith("vegafusion"):
+ # Bug https://github.com/marimo-team/marimo/issues/6601. Vegafusion defaults to white background
+ # So, we set the background to transparent
+ if (
+ chart._get("background") is alt.Undefined # type: ignore
+ ):
+ LOGGER.debug("setting background to transparent")
+ chart = chart.properties(background="transparent")
+
return (
"application/vnd.vega.v5+json",
chart_to_json(chart=chart, spec_format="vega"),
diff --git a/tests/_output/formatters/test_altair_formatters.py b/tests/_output/formatters/test_altair_formatters.py
index 7a03bb2332..0aeb10ff81 100644
--- a/tests/_output/formatters/test_altair_formatters.py
+++ b/tests/_output/formatters/test_altair_formatters.py
@@ -100,6 +100,27 @@ def test_altair_formatter_full_width(mock_make_full_width: MagicMock):
mock_make_full_width.assert_called_once()
[email protected](not HAS_DEPS, reason="altair not installed")
+def test_altair_formatter_vegafusion_dark_mode():
+ AltairFormatter().register()
+
+ import altair as alt
+
+ with alt.data_transformers.enable("vegafusion"):
+ chart = alt.Chart(get_data()).mark_point()
+ formatter = get_formatter(chart)
+
+ assert formatter is not None
+ res = formatter(chart)
+ assert res is not None
+ mime, content = res
+ assert mime == "application/vnd.vega.v5+json"
+ assert isinstance(content, str)
+ json_data = json.loads(content)
+ assert "background" in json_data
+ assert json_data["background"] == "transparent"
+
+
@pytest.mark.skipif(not HAS_DEPS, reason="altair not installed")
def test_altair_formatter_mimebundle():
AltairFormatter().register()