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: as_html for svg and add tests (#5947)

Myles Scolnick546c34cc00fe
diff --git a/marimo/_output/formatting.py b/marimo/_output/formatting.py
index 01347d16ea..e24bc8ab4c 100644
--- a/marimo/_output/formatting.py
+++ b/marimo/_output/formatting.py
@@ -336,6 +336,8 @@ def mime_to_html(mimetype: KnownMimeType, data: Any) -> Html:
# Flatten the HTML text to avoid indentation issues
# when interpolating into markdown/a multiline string
return Html(flatten_string(f"<span>{escape(data)}</span>"))
+ elif mimetype == "image/svg+xml":
+ return Html(data)
elif mimetype.startswith("image"):
return Html(flatten_string(f'<img src="{data}" alt="" />'))
elif mimetype == "application/json":
diff --git a/tests/_output/formatters/test_formatters.py b/tests/_output/formatters/test_formatters.py
index 8fc665f457..a7fa8e4a1a 100644
--- a/tests/_output/formatters/test_formatters.py
+++ b/tests/_output/formatters/test_formatters.py
@@ -520,3 +520,236 @@ def test_polars_dot_to_mermaid_handles_urls() -> None:
== """graph TD
\tp1[\"Check [<a href='https://example.com'>https://example.com</a>] for more\"]"""
)
+
+
+def test_as_html_basic_types() -> None:
+ """Test as_html with basic Python types."""
+ register_formatters()
+
+ # String
+ result = as_html("hello world")
+ assert result.text == "<span>hello world</span>"
+
+ # Integer
+ result = as_html(42)
+ assert result.text == "<span>42</span>"
+
+ # Float
+ result = as_html(3.14)
+ assert result.text == "<span>3.14</span>"
+
+ # Boolean
+ result = as_html(True)
+ assert result.text == "<span>True</span>"
+
+ # None
+ result = as_html(None)
+ assert result.text == "<span>None</span>"
+
+ # List
+ result = as_html([1, 2, 3])
+ assert (
+ result.text
+ == "<marimo-json-output data-json-data='[1, 2, 3]' data-value-types='&quot;python&quot;'></marimo-json-output>"
+ )
+
+ # Dict
+ result = as_html({"key": "value"})
+ assert "<marimo-json" in result.text
+
+
+def test_as_html_with_html_object() -> None:
+ """Test as_html when passed an Html object - should return it unchanged."""
+ from marimo._output.hypertext import Html
+
+ html_obj = Html("<h1>Hello</h1>")
+ result = as_html(html_obj)
+
+ # Should return the same object
+ assert result is html_obj
+ assert result.text == "<h1>Hello</h1>"
+
+
+def test_as_html_with_repr_html() -> None:
+ """Test as_html with objects that have _repr_html_ method."""
+ register_formatters()
+
+ class CustomHTML:
+ def _repr_html_(self):
+ return "<div>Custom HTML content</div>"
+
+ obj = CustomHTML()
+ result = as_html(obj)
+ assert result.text == "<div>Custom HTML content</div>"
+
+
+def test_as_html_with_repr_markdown() -> None:
+ """Test as_html with objects that have _repr_markdown_ method."""
+ register_formatters()
+
+ class CustomMarkdown:
+ def _repr_markdown_(self):
+ return "# Markdown Title"
+
+ obj = CustomMarkdown()
+ result = as_html(obj)
+ assert '<span class="markdown prose dark:prose-invert">' in result.text
+ assert '<h1 id="markdown-title">Markdown Title</h1>' in result.text
+
+
+def test_as_html_with_repr_json() -> None:
+ """Test as_html with objects that have _repr_json_ method."""
+ register_formatters()
+
+ class CustomJSON:
+ def _repr_json_(self):
+ import json
+
+ return json.dumps({"message": "Hello, World!", "count": 42})
+
+ obj = CustomJSON()
+ result = as_html(obj)
+ assert "<marimo-json" in result.text
+
+
+def test_as_html_with_repr_svg() -> None:
+ """Test as_html with objects that have _repr_svg_ method."""
+ register_formatters()
+
+ class CustomSVG:
+ def _repr_svg_(self):
+ return '<svg width="100" height="100"><circle cx="50" cy="50" r="40"/></svg>'
+
+ obj = CustomSVG()
+ result = as_html(obj)
+ assert (
+ result.text
+ == '<svg width="100" height="100"><circle cx="50" cy="50" r="40"/></svg>'
+ )
+
+
+def test_as_html_with_repr_png() -> None:
+ """Test as_html with objects that have _repr_png_ method."""
+ register_formatters()
+
+ class CustomPNG:
+ def _repr_png_(self):
+ return (
+ "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABaElEQVR42mNk"
+ )
+
+ obj = CustomPNG()
+ result = as_html(obj)
+ assert (
+ '<img src="iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABaElEQVR42mNk" alt="" />'
+ in result.text
+ )
+
+
+def test_as_html_with_repr_jpeg() -> None:
+ """Test as_html with objects that have _repr_jpeg_ method."""
+ register_formatters()
+
+ class CustomJPEG:
+ def _repr_jpeg_(self):
+ return (
+ "/9j/4AAQSkZJRgABAQEAYABgAAD/4QBoRXhpZgAATU0AKgAAAAgAA1IBAAAB"
+ )
+
+ obj = CustomJPEG()
+ result = as_html(obj)
+ assert (
+ '<img src="/9j/4AAQSkZJRgABAQEAYABgAAD/4QBoRXhpZgAATU0AKgAAAAgAA1IBAAAB" alt="" />'
+ in result.text
+ )
+
+
+def test_as_html_with_plain_wrapper() -> None:
+ """Test as_html with Plain wrapper to bypass opinionated formatting."""
+ register_formatters()
+
+ # Test with a list that would normally get JSON formatting
+ plain_list = Plain([1, 2, 3])
+ result = as_html(plain_list)
+ assert (
+ result.text
+ == "<marimo-json-output data-json-data='[1, 2, 3]' data-value-types='&quot;python&quot;'></marimo-json-output>"
+ )
+
+ # Test with a dict that would normally get marimo-json formatting
+ plain_dict = Plain({"key": "value"})
+ result = as_html(plain_dict)
+ assert (
+ result.text
+ == "<marimo-json-output data-json-data='{&quot;key&quot;: &quot;value&quot;}' data-value-types='&quot;python&quot;'></marimo-json-output>"
+ )
+
+
+def test_as_html_with_no_formatter() -> None:
+ """Test as_html with objects that have no registered formatter."""
+ register_formatters()
+
+ class NoFormatter:
+ def __str__(self):
+ return "Custom string representation"
+
+ obj = NoFormatter()
+ result = as_html(obj)
+ assert result.text == "<span>Custom string representation</span>"
+
+
+def test_as_html_with_broken_formatter() -> None:
+ """Test as_html behavior when formatter raises an exception."""
+ register_formatters()
+
+ class BrokenFormatter:
+ def _repr_html_(self):
+ raise ValueError("Formatter is broken")
+
+ def __str__(self):
+ return "fallback string"
+
+ obj = BrokenFormatter()
+ # Should fall back gracefully
+ with pytest.raises(ValueError):
+ as_html(obj)
+
+
[email protected](not HAS_DEPS, reason="optional dependencies not installed")
+def test_as_html_with_dataframes() -> None:
+ """Test as_html with pandas and polars DataFrames."""
+ register_formatters()
+
+ import pandas as pd
+ import polars as pl
+
+ # Pandas DataFrame
+ pd_df = pd.DataFrame({"A": [1, 2, 3], "B": ["x", "y", "z"]})
+ result = as_html(pd_df)
+ assert "<marimo-table" in result.text
+
+ # Polars DataFrame
+ pl_df = pl.DataFrame({"A": [1, 2, 3], "B": ["x", "y", "z"]})
+ result = as_html(pl_df)
+ assert "<marimo-table" in result.text
+
+ # Polars LazyFrame
+ pl_ldf = pl_df.lazy()
+ result = as_html(pl_ldf)
+ assert "<marimo-mermaid" in result.text
+
+
+def test_as_html_with_display_protocol() -> None:
+ """Test as_html with objects implementing _display_ protocol."""
+ register_formatters()
+
+ class DisplayProtocol:
+ def _display_(self):
+ return "display protocol content"
+
+ def _repr_html_(self):
+ return "<h1>Should not be used</h1>"
+
+ obj = DisplayProtocol()
+ result = as_html(obj)
+ assert result.text == "<span>display protocol content</span>"