Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ uv.lock

# WIP files
.wip/
wip/

# editors
*.swp
Expand Down
10 changes: 10 additions & 0 deletions marimo/_output/formatters/altair_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -82,6 +83,15 @@ 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
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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does require a re-run and won't work when the mode is set to system. Should we just always make it transparent? Or inherit


return (
"application/vnd.vega.v5+json",
chart_to_json(chart=chart, spec_format="vega"),
Expand Down
25 changes: 25 additions & 0 deletions tests/_output/formatters/test_altair_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading