Skip to content

Commit 6c58c1a

Browse files
authored
altair: set background to transparent for vegafusion if no background is set (#6614)
## 📝 Summary <!-- Provide a concise summary of what this pull request is addressing. If this PR fixes any issues, list them here by number (e.g., Fixes #123). --> Fixes #6601 . dark: <img width="749" height="365" alt="CleanShot 2025-10-01 at 02 17 28" src="https://github.com/user-attachments/assets/35fdf4ca-77cc-40c4-846d-5ac2b4c3960c" /> light: <img width="785" height="574" alt="CleanShot 2025-10-01 at 02 17 04" src="https://github.com/user-attachments/assets/2160f54a-8029-44ed-8387-4421c9e4eace" /> ## 🔍 Description of Changes <!-- Detail the specific changes made in this pull request. Explain the problem addressed and how it was resolved. If applicable, provide before and after comparisons, screenshots, or any relevant details to help reviewers understand the changes easily. --> ## 📋 Checklist - [x] I have read the [contributor guidelines](https://github.com/marimo-team/marimo/blob/main/CONTRIBUTING.md). - [ ] For large changes, or changes that affect the public API: this change was discussed or approved through an issue, on [Discord](https://marimo.io/discord?ref=pr), or the community [discussions](https://github.com/marimo-team/marimo/discussions) (Please provide a link if applicable). - [x] I have added tests for the changes made. - [x] I have run the code and verified that it works as expected.
1 parent 4f33480 commit 6c58c1a

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ uv.lock
9696

9797
# WIP files
9898
.wip/
99+
wip/
99100

100101
# editors
101102
*.swp

marimo/_output/formatters/altair_formatters.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ def _show_chart(chart: AltairChartType) -> tuple[KnownMimeType, str]:
8282

8383
# If vegafusion is enabled, just wrap in altair_chart
8484
if alt.data_transformers.active.startswith("vegafusion"):
85+
# Bug https://github.com/marimo-team/marimo/issues/6601. Vegafusion defaults to white background
86+
# So, we set the background to transparent
87+
if (
88+
chart._get("background") is alt.Undefined # type: ignore
89+
):
90+
LOGGER.debug("setting background to transparent")
91+
chart = chart.properties(background="transparent")
92+
8593
return (
8694
"application/vnd.vega.v5+json",
8795
chart_to_json(chart=chart, spec_format="vega"),

tests/_output/formatters/test_altair_formatters.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,27 @@ def test_altair_formatter_full_width(mock_make_full_width: MagicMock):
100100
mock_make_full_width.assert_called_once()
101101

102102

103+
@pytest.mark.skipif(not HAS_DEPS, reason="altair not installed")
104+
def test_altair_formatter_vegafusion_dark_mode():
105+
AltairFormatter().register()
106+
107+
import altair as alt
108+
109+
with alt.data_transformers.enable("vegafusion"):
110+
chart = alt.Chart(get_data()).mark_point()
111+
formatter = get_formatter(chart)
112+
113+
assert formatter is not None
114+
res = formatter(chart)
115+
assert res is not None
116+
mime, content = res
117+
assert mime == "application/vnd.vega.v5+json"
118+
assert isinstance(content, str)
119+
json_data = json.loads(content)
120+
assert "background" in json_data
121+
assert json_data["background"] == "transparent"
122+
123+
103124
@pytest.mark.skipif(not HAS_DEPS, reason="altair not installed")
104125
def test_altair_formatter_mimebundle():
105126
AltairFormatter().register()

0 commit comments

Comments
 (0)