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.

improvement: use vega 6 mimetype, capture screenshots on pdf export (#7906)

Myles Scolnick90418caad8b8
diff --git a/.github/workflows/test_cli.yaml b/.github/workflows/test_cli.yaml
index 10fbd9b7db..2c07ae8b5c 100644
--- a/.github/workflows/test_cli.yaml
+++ b/.github/workflows/test_cli.yaml
@@ -151,13 +151,9 @@ jobs:
- name: Test CLI
shell: bash
run: |
- export MARIMO_WHEEL_PATH=$(ls $PWD/marimo*.whl)
- uv run \
- --group=test-optional \
- --group=test \
- --group=dev \
- --with="${MARIMO_WHEEL_PATH}" \
- pytest -v tests/_cli/test_cli* --maxfail=2
+ uv venv
+ uv pip install --group=test-optional --group=test --group=dev marimo*.whl
+ uv run --no-project pytest -v tests/_cli/test_cli* --maxfail=2
test_examples:
needs: [changes]
diff --git a/frontend/src/components/editor/Output.tsx b/frontend/src/components/editor/Output.tsx
index d2313564d4..16ba74658e 100644
--- a/frontend/src/components/editor/Output.tsx
+++ b/frontend/src/components/editor/Output.tsx
@@ -90,6 +90,8 @@ export const OutputRenderer: React.FC<{
case "application/vnd.marimo+mimebundle":
case "application/vnd.vegalite.v5+json":
case "application/vnd.vega.v5+json":
+ case "application/vnd.vegalite.v6+json":
+ case "application/vnd.vega.v6+json":
return typeof data === "string" ? JSON.parse(data) : data;
default:
return;
@@ -199,6 +201,8 @@ export const OutputRenderer: React.FC<{
);
case "application/vnd.vegalite.v5+json":
case "application/vnd.vega.v5+json":
+ case "application/vnd.vegalite.v6+json":
+ case "application/vnd.vega.v6+json":
return (
<Suspense fallback={<ChartLoadingState />}>
<LazyVegaEmbed
diff --git a/frontend/src/components/editor/renderMimeIcon.tsx b/frontend/src/components/editor/renderMimeIcon.tsx
index e1cc6b58ac..01e6a9274f 100644
--- a/frontend/src/components/editor/renderMimeIcon.tsx
+++ b/frontend/src/components/editor/renderMimeIcon.tsx
@@ -29,6 +29,8 @@ export function renderMimeIcon(mime: string) {
return "📝";
case "application/vnd.vegalite.v5+json":
case "application/vnd.vega.v5+json":
+ case "application/vnd.vegalite.v6+json":
+ case "application/vnd.vega.v6+json":
return "📊";
case "application/vnd.marimo+mimebundle":
return "📦";
diff --git a/frontend/src/core/export/hooks.ts b/frontend/src/core/export/hooks.ts
index b600317499..386bbf5f3c 100644
--- a/frontend/src/core/export/hooks.ts
+++ b/frontend/src/core/export/hooks.ts
@@ -85,6 +85,15 @@ export function useAutoExport() {
// We track cells that need screenshots, these will be exported to IPYNB
const richCellsToOutputAtom = atom<Record<CellId, unknown>>({});
+// MIME types to capture screenshots for
+const MIME_TYPES_TO_CAPTURE_SCREENSHOTS = new Set([
+ "text/html",
+ "application/vnd.vegalite.v5+json",
+ "application/vnd.vega.v5+json",
+ "application/vnd.vegalite.v6+json",
+ "application/vnd.vega.v6+json",
+]);
+
/**
* Take screenshots of cells with HTML outputs. These images will be sent to the backend to be exported to IPYNB.
* @returns A map of cell IDs to their screenshots data.
@@ -103,7 +112,7 @@ export function useEnrichCellOutputs() {
// Track latest output for this cell
trackedCellsOutput[cellId] = outputData;
if (
- runtime.output?.mimetype === "text/html" &&
+ MIME_TYPES_TO_CAPTURE_SCREENSHOTS.has(runtime.output?.mimetype ?? "") &&
outputData &&
outputHasChanged
) {
diff --git a/marimo/_messaging/mimetypes.py b/marimo/_messaging/mimetypes.py
index d067dec236..85c38d9cbc 100644
--- a/marimo/_messaging/mimetypes.py
+++ b/marimo/_messaging/mimetypes.py
@@ -13,6 +13,8 @@ KnownMimeType = Literal[
"application/vnd.marimo+mimebundle",
"application/vnd.vega.v5+json",
"application/vnd.vegalite.v5+json",
+ "application/vnd.vega.v6+json",
+ "application/vnd.vegalite.v6+json",
"application/vnd.jupyter.widget-view+json",
"image/png",
"image/svg+xml",
diff --git a/marimo/_output/formatters/altair_formatters.py b/marimo/_output/formatters/altair_formatters.py
index 693bdf9842..3935f94180 100644
--- a/marimo/_output/formatters/altair_formatters.py
+++ b/marimo/_output/formatters/altair_formatters.py
@@ -14,6 +14,7 @@ from marimo._plugins.core.media import io_to_data_url
from marimo._plugins.ui._impl.altair_chart import (
AltairChartType,
chart_to_json,
+ get_chart_mimetype,
maybe_fix_vegafusion_background,
maybe_make_full_width,
)
@@ -65,6 +66,8 @@ class AltairFormatter(FormatterFactory):
"image/png",
"application/vnd.vega.v5+json",
"application/vnd.vegalite.v5+json",
+ "application/vnd.vega.v6+json",
+ "application/vnd.vegalite.v6+json",
]
for mime_type in non_html_mime_types:
if mime_type in mimebundle:
@@ -83,7 +86,7 @@ class AltairFormatter(FormatterFactory):
# If vegafusion is enabled, just wrap in altair_chart
if alt.data_transformers.active.startswith("vegafusion"):
return (
- "application/vnd.vega.v5+json",
+ get_chart_mimetype(spec_format="vega"),
chart_to_json(chart=chart, spec_format="vega"),
)
@@ -94,7 +97,7 @@ class AltairFormatter(FormatterFactory):
# Return the chart as a vega-lite chart with embed options
return (
- "application/vnd.vegalite.v5+json",
+ get_chart_mimetype(spec_format="vega-lite"),
chart_to_json(chart=chart, validate=False),
)
diff --git a/marimo/_output/mime.py b/marimo/_output/mime.py
index 15d3e8e2ec..69b81fa413 100644
--- a/marimo/_output/mime.py
+++ b/marimo/_output/mime.py
@@ -25,6 +25,8 @@ class MIME:
application/vnd.marimo+traceback
application/vnd.vega.v5+json
application/vnd.vegalite.v5+json
+ application/vnd.vega.v6+json
+ application/vnd.vegalite.v6+json
image/png
image/svg+xml
image/tiff
diff --git a/marimo/_plugins/ui/_impl/altair_chart.py b/marimo/_plugins/ui/_impl/altair_chart.py
index a0052c4303..e5760a76ae 100644
--- a/marimo/_plugins/ui/_impl/altair_chart.py
+++ b/marimo/_plugins/ui/_impl/altair_chart.py
@@ -659,7 +659,7 @@ class altair_chart(UIElement[ChartSelection, ChartDataType]):
def _mime_(self) -> tuple[KnownMimeType, str]:
if is_no_js():
return (
- "application/vnd.vega.v5+json",
+ get_chart_mimetype(spec_format="vega"),
chart_to_json(self._chart, validate=False),
)
return ("text/html", self.text)
@@ -1007,3 +1007,27 @@ def chart_to_json(
allow_nan=False,
default=str,
)
+
+
+def get_chart_mimetype(
+ spec_format: Literal["vega", "vega-lite"] = "vega-lite",
+) -> KnownMimeType:
+ """
+ Get the appropriate MIME type for a chart based on its schema version.
+
+ Returns the MIME type with the correct version (v5 or v6) based on the
+ chart's schema. Defaults to v6 if version cannot be determined.
+ """
+ try:
+ from altair import VEGA_VERSION
+
+ if spec_format == "vega":
+ return f"application/vnd.vega.v{VEGA_VERSION}+json" # type: ignore
+ else:
+ return f"application/vnd.vegalite.v{VEGA_VERSION}+json" # type: ignore
+ except Exception:
+ # Fallback to v6 if anything goes wrong
+ if spec_format == "vega":
+ return "application/vnd.vega.v6+json"
+ else:
+ return "application/vnd.vegalite.v6+json"
diff --git a/packages/openapi/api.yaml b/packages/openapi/api.yaml
index 4bb8b1eb15..26915e695a 100644
--- a/packages/openapi/api.yaml
+++ b/packages/openapi/api.yaml
@@ -462,7 +462,9 @@ components:
- application/vnd.marimo+mimebundle
- application/vnd.marimo+traceback
- application/vnd.vega.v5+json
+ - application/vnd.vega.v6+json
- application/vnd.vegalite.v5+json
+ - application/vnd.vegalite.v6+json
- image/avif
- image/bmp
- image/gif
@@ -4056,7 +4058,9 @@ components:
- application/vnd.marimo+mimebundle
- application/vnd.marimo+traceback
- application/vnd.vega.v5+json
+ - application/vnd.vega.v6+json
- application/vnd.vegalite.v5+json
+ - application/vnd.vegalite.v6+json
- image/avif
- image/bmp
- image/gif
@@ -4468,7 +4472,7 @@ components:
type: object
info:
title: marimo API
- version: 0.19.2
+ version: 0.19.4
openapi: 3.1.0
paths:
/@file/{filename_and_length}:
@@ -5793,3 +5797,4 @@ paths:
summary: Submit login form
tags:
- auth
+
diff --git a/packages/openapi/src/api.ts b/packages/openapi/src/api.ts
index 08397fe7e3..ed05991de0 100644
--- a/packages/openapi/src/api.ts
+++ b/packages/openapi/src/api.ts
@@ -3220,7 +3220,9 @@ export interface components {
| "application/vnd.marimo+mimebundle"
| "application/vnd.marimo+traceback"
| "application/vnd.vega.v5+json"
+ | "application/vnd.vega.v6+json"
| "application/vnd.vegalite.v5+json"
+ | "application/vnd.vegalite.v6+json"
| "image/avif"
| "image/bmp"
| "image/gif"
@@ -5255,7 +5257,9 @@ export interface components {
| "application/vnd.marimo+mimebundle"
| "application/vnd.marimo+traceback"
| "application/vnd.vega.v5+json"
+ | "application/vnd.vega.v6+json"
| "application/vnd.vegalite.v5+json"
+ | "application/vnd.vegalite.v6+json"
| "image/avif"
| "image/bmp"
| "image/gif"
diff --git a/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txt b/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txt
index b1f7e1ddab..d1cda28d2c 100644
--- a/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txt
+++ b/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txt
@@ -172,7 +172,7 @@
"outputs": [
{
"data": {
- "application/vnd.vegalite.v5+json": "{\n \"$schema\": \"https://vega.github.io/schema/vega-lite/v6.1.0.json\",\n \"config\": {\n \"view\": {\n \"continuousHeight\": 300,\n \"continuousWidth\": 300\n }\n },\n \"data\": {\n \"name\": \"data-a202e5dd04552c98b78a4bc5fc59f367\"\n },\n \"datasets\": {\n \"data-a202e5dd04552c98b78a4bc5fc59f367\": [\n {\n \"x\": 1,\n \"y\": 1\n }\n ]\n },\n \"encoding\": {\n \"x\": {\n \"field\": \"x\",\n \"type\": \"ordinal\"\n },\n \"y\": {\n \"field\": \"y\",\n \"type\": \"quantitative\"\n }\n },\n \"mark\": {\n \"type\": \"circle\"\n },\n \"usermeta\": {\n \"embedOptions\": {}\n },\n \"width\": \"container\"\n}"
+ "application/vnd.vegalite.v6+json": "{\n \"$schema\": \"https://vega.github.io/schema/vega-lite/v6.1.0.json\",\n \"config\": {\n \"view\": {\n \"continuousHeight\": 300,\n \"continuousWidth\": 300\n }\n },\n \"data\": {\n \"name\": \"data-a202e5dd04552c98b78a4bc5fc59f367\"\n },\n \"datasets\": {\n \"data-a202e5dd04552c98b78a4bc5fc59f367\": [\n {\n \"x\": 1,\n \"y\": 1\n }\n ]\n },\n \"encoding\": {\n \"x\": {\n \"field\": \"x\",\n \"type\": \"ordinal\"\n },\n \"y\": {\n \"field\": \"y\",\n \"type\": \"quantitative\"\n }\n },\n \"mark\": {\n \"type\": \"circle\"\n },\n \"usermeta\": {\n \"embedOptions\": {}\n },\n \"width\": \"container\"\n}"
},
"metadata": {},
"output_type": "display_data"
@@ -193,7 +193,7 @@
"outputs": [
{
"data": {
- "application/vnd.vega.v5+json": "{\n \"$schema\": \"https://vega.github.io/schema/vega-lite/v6.1.0.json\",\n \"config\": {\n \"view\": {\n \"continuousHeight\": 300,\n \"continuousWidth\": 300\n }\n },\n \"data\": {\n \"name\": \"data-a202e5dd04552c98b78a4bc5fc59f367\"\n },\n \"datasets\": {\n \"data-a202e5dd04552c98b78a4bc5fc59f367\": [\n {\n \"x\": 1,\n \"y\": 1\n }\n ]\n },\n \"encoding\": {\n \"x\": {\n \"field\": \"x\",\n \"type\": \"ordinal\"\n },\n \"y\": {\n \"field\": \"y\",\n \"type\": \"quantitative\"\n }\n },\n \"mark\": {\n \"type\": \"circle\"\n },\n \"usermeta\": {\n \"embedOptions\": {}\n }\n}"
+ "application/vnd.vega.v6+json": "{\n \"$schema\": \"https://vega.github.io/schema/vega-lite/v6.1.0.json\",\n \"config\": {\n \"view\": {\n \"continuousHeight\": 300,\n \"continuousWidth\": 300\n }\n },\n \"data\": {\n \"name\": \"data-a202e5dd04552c98b78a4bc5fc59f367\"\n },\n \"datasets\": {\n \"data-a202e5dd04552c98b78a4bc5fc59f367\": [\n {\n \"x\": 1,\n \"y\": 1\n }\n ]\n },\n \"encoding\": {\n \"x\": {\n \"field\": \"x\",\n \"type\": \"ordinal\"\n },\n \"y\": {\n \"field\": \"y\",\n \"type\": \"quantitative\"\n }\n },\n \"mark\": {\n \"type\": \"circle\"\n },\n \"usermeta\": {\n \"embedOptions\": {}\n }\n}"
},
"metadata": {},
"output_type": "display_data"
diff --git a/tests/_output/formatters/test_altair.py b/tests/_output/formatters/test_altair.py
index 4e290c7c79..39c2d22f43 100644
--- a/tests/_output/formatters/test_altair.py
+++ b/tests/_output/formatters/test_altair.py
@@ -12,6 +12,14 @@ HAS_ALTAIR = DependencyManager.altair.has()
HAS_VL_CONVERT = DependencyManager.has("vl-convert")
+def assert_vegalite_mimetype(mime: str) -> None:
+ """Assert that the mime type is a valid vega-lite mime type (v5 or v6)."""
+ assert mime in (
+ "application/vnd.vegalite.v5+json",
+ "application/vnd.vegalite.v6+json",
+ ), f"Expected vega-lite mime type (v5 or v6), got {mime}"
+
+
@pytest.mark.skipif(
not HAS_ALTAIR, reason="optional dependencies not installed"
)
@@ -41,7 +49,7 @@ async def test_altair(
result = executing_kernel.globals["result"]
assert result is not None
- assert result[0] == "application/vnd.vegalite.v5+json"
+ assert_vegalite_mimetype(result[0])
# Valid JSON
assert "$schema" in result[1]
assert isinstance(result[1], str)
@@ -80,7 +88,7 @@ async def test_altair_with_embed_options(
result = executing_kernel.globals["before_options_result"]
assert result is not None
- assert result[0] == "application/vnd.vegalite.v5+json"
+ assert_vegalite_mimetype(result[0])
json_result = json.loads(result[1])
assert json_result["usermeta"] == {
"embedOptions": {},
@@ -88,7 +96,7 @@ async def test_altair_with_embed_options(
result = executing_kernel.globals["after_options_result"]
assert result is not None
- assert result[0] == "application/vnd.vegalite.v5+json"
+ assert_vegalite_mimetype(result[0])
json_result = json.loads(result[1])
assert json_result["usermeta"] == {
"embedOptions": {
diff --git a/tests/_output/formatters/test_altair_formatters.py b/tests/_output/formatters/test_altair_formatters.py
index 0aeb10ff81..73e615e031 100644
--- a/tests/_output/formatters/test_altair_formatters.py
+++ b/tests/_output/formatters/test_altair_formatters.py
@@ -25,6 +25,22 @@ if TYPE_CHECKING:
from narwhals.typing import IntoDataFrame
+def assert_vegalite_mimetype(mime: str) -> None:
+ """Assert that the mime type is a valid vega-lite mime type (v5 or v6)."""
+ assert mime in (
+ "application/vnd.vegalite.v5+json",
+ "application/vnd.vegalite.v6+json",
+ ), f"Expected vega-lite mime type (v5 or v6), got {mime}"
+
+
+def assert_vega_mimetype(mime: str) -> None:
+ """Assert that the mime type is a valid vega mime type (v5 or v6)."""
+ assert mime in (
+ "application/vnd.vega.v5+json",
+ "application/vnd.vega.v6+json",
+ ), f"Expected vega mime type (v5 or v6), got {mime}"
+
+
def get_data():
import polars as pl
@@ -57,7 +73,7 @@ def test_altair_formatter_registration():
formatter = get_formatter(chart)
assert formatter is not None
mime, content = formatter(chart)
- assert mime == "application/vnd.vegalite.v5+json"
+ assert_vegalite_mimetype(mime)
assert isinstance(content, str)
# Verify it's valid JSON
json_content = json.loads(content)
@@ -92,7 +108,7 @@ def test_altair_formatter_full_width(mock_make_full_width: MagicMock):
res = formatter(chart)
assert res is not None
mime, content = res
- assert mime == "application/vnd.vegalite.v5+json"
+ assert_vegalite_mimetype(mime)
assert isinstance(content, str)
assert "container" in content
@@ -114,7 +130,7 @@ def test_altair_formatter_vegafusion_dark_mode():
res = formatter(chart)
assert res is not None
mime, content = res
- assert mime == "application/vnd.vega.v5+json"
+ assert_vega_mimetype(mime)
assert isinstance(content, str)
json_data = json.loads(content)
assert "background" in json_data
@@ -134,7 +150,7 @@ def test_altair_formatter_mimebundle():
"_repr_mimebundle_",
return_value={
"image/svg+xml": "<svg></svg>",
- "application/vnd.vegalite.v5+json": json.dumps({"test": "data"}),
+ "application/vnd.vegalite.v6+json": json.dumps({"test": "data"}),
},
):
formatter = get_formatter(mock_chart)
@@ -145,7 +161,7 @@ def test_altair_formatter_mimebundle():
assert mime == "application/vnd.marimo+mimebundle"
mimebundle = json.loads(content)
assert "image/svg+xml" in mimebundle
- assert "application/vnd.vegalite.v5+json" in mimebundle
+ assert "application/vnd.vegalite.v6+json" in mimebundle
@pytest.mark.skipif(not HAS_DEPS, reason="altair not installed")
@@ -197,7 +213,7 @@ def test_altair_formatter_sanitize_nan_infs(df: IntoDataFrame):
formatter = get_formatter(chart)
assert formatter is not None
mime, content = formatter(chart)
- assert mime == "application/vnd.vegalite.v5+json"
+ assert_vegalite_mimetype(mime)
assert isinstance(content, str)
for non_valid_value in ["NaN", "Infinity", "-Infinity"]: