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.

fix: handle altair usermeta embedoptions manually (#2303)

Myles Scolnicka6c6630e3143
diff --git a/.github/workflows/test_be.yaml b/.github/workflows/test_be.yaml
index 2603a38f3a..370ba6ffb5 100644
--- a/.github/workflows/test_be.yaml
+++ b/.github/workflows/test_be.yaml
@@ -20,7 +20,8 @@ env:
jobs:
test_python:
- name: Tests on ${{ matrix.os }}, Python ${{ matrix.python-version }} with ${{ matrix.dependencies }} dependencies
+ # short name otherwise GitHub Actions will truncate it
+ name: ${{ matrix.os }} / Py ${{ matrix.python-version }} / ${{ matrix.dependencies }} deps
runs-on: ${{ matrix.os }}
timeout-minutes: 15
defaults:
diff --git a/marimo/_output/formatters/altair_formatters.py b/marimo/_output/formatters/altair_formatters.py
index a1d04319ff..a0906780d7 100644
--- a/marimo/_output/formatters/altair_formatters.py
+++ b/marimo/_output/formatters/altair_formatters.py
@@ -2,12 +2,16 @@
from __future__ import annotations
import json
+from typing import TYPE_CHECKING
from marimo._config.config import Theme
from marimo._messaging.mimetypes import KnownMimeType, MimeBundleOrTuple
from marimo._output.formatters.formatter_factory import FormatterFactory
from marimo._plugins.core.media import io_to_data_url
+if TYPE_CHECKING:
+ import altair # type: ignore[import-not-found,import-untyped,unused-ignore] # noqa: E501
+
class AltairFormatter(FormatterFactory):
@staticmethod
@@ -76,10 +80,34 @@ class AltairFormatter(FormatterFactory):
if "max_rows" not in alt.data_transformers.options:
alt.data_transformers.options["max_rows"] = 20_000
- # Return the chart as a vega-lite chart
+ chart = _apply_embed_options(chart)
+
+ # Return the chart as a vega-lite chart with embed options
return ("application/vnd.vegalite.v5+json", chart.to_json())
def apply_theme(self, theme: Theme) -> None:
import altair as alt # type: ignore
alt.themes.enable("dark" if theme == "dark" else "default") # type: ignore
+
+
+# This is only needed since it seems that altair does not
+# handle this internally.
+# https://github.com/marimo-team/marimo/issues/2302
+def _apply_embed_options(chart: altair.Chart) -> altair.Chart:
+ import altair as alt
+
+ # Respect user-set embed options
+ # Note:
+ # The python key is `embed_options`
+ # The javascript key is `embedOptions`
+ embed_options = alt.renderers.options.get("embed_options", {})
+ prev_usermeta = {} if alt.Undefined is chart.usermeta else chart.usermeta
+ chart["usermeta"] = {
+ **prev_usermeta,
+ "embedOptions": {
+ **embed_options,
+ **prev_usermeta.get("embedOptions", {}),
+ },
+ }
+ return chart
diff --git a/marimo/_smoke_tests/altair/embed_options.py b/marimo/_smoke_tests/altair/embed_options.py
new file mode 100644
index 0000000000..752e8a4f94
--- /dev/null
+++ b/marimo/_smoke_tests/altair/embed_options.py
@@ -0,0 +1,32 @@
+import marimo
+
+__generated_with = "0.8.14"
+app = marimo.App(width="medium")
+
+
+def __():
+ import marimo as mo
+ import altair as alt
+ import pandas as pd
+
+ data = pd.DataFrame({"x": range(10), "y": range(10)})
+
+ alt.renderers.set_embed_options(actions=False)
+ # altair.renderers.set_embed_options(actions=True)
+
+ # Plain chart
+ chart = alt.Chart(data).mark_line().encode(x="x", y="y")
+ chart
+ return alt, chart, data, mo, pd
+
+
+def __(chart, mo):
+ # Wrapped chart
+ mo.ui.altair_chart(chart)
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_output/formatters/test_altair.py b/tests/_output/formatters/test_altair.py
index 0401b825c9..4e290c7c79 100644
--- a/tests/_output/formatters/test_altair.py
+++ b/tests/_output/formatters/test_altair.py
@@ -48,6 +48,55 @@ async def test_altair(
assert json.loads(result[1])
+ not HAS_ALTAIR, reason="optional dependencies not installed"
+)
+async def test_altair_with_embed_options(
+ executing_kernel: Kernel, exec_req: ExecReqProvider
+) -> None:
+ from marimo._output.formatters.formatters import register_formatters
+
+ register_formatters()
+
+ await executing_kernel.run(
+ [
+ exec_req.get(
+ """
+ import altair as alt
+ import pandas as pd
+ from marimo._output.formatting import get_formatter
+
+ data = pd.DataFrame({"a": ["A", "B"], "b": [28, 55]})
+ chart = alt.Chart(data).mark_bar().encode(x="a", y="b")
+ formatter = get_formatter(chart)
+
+ before_options_result = formatter(chart)
+ alt.renderers.set_embed_options(actions=False)
+ after_options_result = formatter(chart)
+ """
+ )
+ ]
+ )
+
+ result = executing_kernel.globals["before_options_result"]
+ assert result is not None
+ assert result[0] == "application/vnd.vegalite.v5+json"
+ json_result = json.loads(result[1])
+ assert json_result["usermeta"] == {
+ "embedOptions": {},
+ }
+
+ result = executing_kernel.globals["after_options_result"]
+ assert result is not None
+ assert result[0] == "application/vnd.vegalite.v5+json"
+ json_result = json.loads(result[1])
+ assert json_result["usermeta"] == {
+ "embedOptions": {
+ "actions": False,
+ },
+ }
+
+
@pytest.mark.skipif(
not HAS_ALTAIR or not HAS_VL_CONVERT,
reason="optional dependencies not installed and fails on mac",