From f2e2d0061215586da2821ac477f3bd0c258a8661 Mon Sep 17 00:00:00 2001 From: Shahmir Varqha Date: Wed, 1 Oct 2025 01:47:39 +0800 Subject: [PATCH 1/4] set background to black for vegafusion if no bg is set for dark mode --- .gitignore | 1 + .../_output/formatters/altair_formatters.py | 11 ++++++++ .../formatters/test_altair_formatters.py | 25 +++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/.gitignore b/.gitignore index ca87ece16e8..815343939e5 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 13c0f3c0034..5c18256ab84 100644 --- a/marimo/_output/formatters/altair_formatters.py +++ b/marimo/_output/formatters/altair_formatters.py @@ -18,6 +18,7 @@ from marimo._plugins.ui._impl.charts.altair_transformer import ( sanitize_nan_infs, ) +from marimo._utils.theme import get_current_theme LOGGER = marimo_logger() @@ -82,6 +83,16 @@ def _show_chart(chart: AltairChartType) -> tuple[KnownMimeType, str]: # If vegafusion is enabled, just wrap in altair_chart if alt.data_transformers.active.startswith("vegafusion"): + current_theme = get_current_theme() + # Bug https://github.com/marimo-team/marimo/issues/6601. Vegafusion defaults to white background + # So, we set the background to black for dark mode + if ( + current_theme == "dark" + and chart._get("background") is alt.Undefined + ): + LOGGER.debug("setting background to black") + chart = chart.properties(background="black") + 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 7a03bb23323..f03168aea3f 100644 --- a/tests/_output/formatters/test_altair_formatters.py +++ b/tests/_output/formatters/test_altair_formatters.py @@ -100,6 +100,31 @@ def test_altair_formatter_full_width(mock_make_full_width: MagicMock): mock_make_full_width.assert_called_once() +@pytest.mark.skipif(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) + + with patch( + "marimo._output.formatters.altair_formatters.get_current_theme", + return_value="dark", + ): + 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"] == "black" + + @pytest.mark.skipif(not HAS_DEPS, reason="altair not installed") def test_altair_formatter_mimebundle(): AltairFormatter().register() From 98403f241ba88c1b04669e351240b04afa6e9258 Mon Sep 17 00:00:00 2001 From: Shahmir Varqha Date: Wed, 1 Oct 2025 01:48:53 +0800 Subject: [PATCH 2/4] optimize --- marimo/_output/formatters/altair_formatters.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/marimo/_output/formatters/altair_formatters.py b/marimo/_output/formatters/altair_formatters.py index 5c18256ab84..7124ec29900 100644 --- a/marimo/_output/formatters/altair_formatters.py +++ b/marimo/_output/formatters/altair_formatters.py @@ -83,12 +83,11 @@ def _show_chart(chart: AltairChartType) -> tuple[KnownMimeType, str]: # If vegafusion is enabled, just wrap in altair_chart if alt.data_transformers.active.startswith("vegafusion"): - current_theme = get_current_theme() # Bug https://github.com/marimo-team/marimo/issues/6601. Vegafusion defaults to white background # So, we set the background to black for dark mode if ( - current_theme == "dark" - and chart._get("background") is alt.Undefined + chart._get("background") is alt.Undefined + and get_current_theme() == "dark" ): LOGGER.debug("setting background to black") chart = chart.properties(background="black") From 39e74c8159d6517c10de24f7235ff67cf5a07fc2 Mon Sep 17 00:00:00 2001 From: Shahmir Varqha Date: Wed, 1 Oct 2025 01:53:24 +0800 Subject: [PATCH 3/4] type ignore --- marimo/_output/formatters/altair_formatters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/marimo/_output/formatters/altair_formatters.py b/marimo/_output/formatters/altair_formatters.py index 7124ec29900..86ea809fcd5 100644 --- a/marimo/_output/formatters/altair_formatters.py +++ b/marimo/_output/formatters/altair_formatters.py @@ -86,7 +86,7 @@ def _show_chart(chart: AltairChartType) -> tuple[KnownMimeType, str]: # Bug https://github.com/marimo-team/marimo/issues/6601. Vegafusion defaults to white background # So, we set the background to black for dark mode if ( - chart._get("background") is alt.Undefined + chart._get("background") is alt.Undefined # type: ignore and get_current_theme() == "dark" ): LOGGER.debug("setting background to black") From eeb0465aaca127af65daf9038c4f3ece39f77a7b Mon Sep 17 00:00:00 2001 From: Shahmir Varqha Date: Wed, 1 Oct 2025 02:16:49 +0800 Subject: [PATCH 4/4] set background to transparent --- .../_output/formatters/altair_formatters.py | 8 +++---- .../formatters/test_altair_formatters.py | 22 ++++++++----------- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/marimo/_output/formatters/altair_formatters.py b/marimo/_output/formatters/altair_formatters.py index 86ea809fcd5..c79e641c584 100644 --- a/marimo/_output/formatters/altair_formatters.py +++ b/marimo/_output/formatters/altair_formatters.py @@ -18,7 +18,6 @@ from marimo._plugins.ui._impl.charts.altair_transformer import ( sanitize_nan_infs, ) -from marimo._utils.theme import get_current_theme LOGGER = marimo_logger() @@ -84,13 +83,12 @@ def _show_chart(chart: AltairChartType) -> tuple[KnownMimeType, str]: # 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 black for dark mode + # So, we set the background to transparent if ( chart._get("background") is alt.Undefined # type: ignore - and get_current_theme() == "dark" ): - LOGGER.debug("setting background to black") - chart = chart.properties(background="black") + LOGGER.debug("setting background to transparent") + chart = chart.properties(background="transparent") return ( "application/vnd.vega.v5+json", diff --git a/tests/_output/formatters/test_altair_formatters.py b/tests/_output/formatters/test_altair_formatters.py index f03168aea3f..0aeb10ff813 100644 --- a/tests/_output/formatters/test_altair_formatters.py +++ b/tests/_output/formatters/test_altair_formatters.py @@ -110,19 +110,15 @@ def test_altair_formatter_vegafusion_dark_mode(): chart = alt.Chart(get_data()).mark_point() formatter = get_formatter(chart) - with patch( - "marimo._output.formatters.altair_formatters.get_current_theme", - return_value="dark", - ): - 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"] == "black" + 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")