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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Preserve bracketed widget values in Markdown (#11385)
Trevor Manzc716635c0949
diff --git a/marimo/_plugins/core/web_component.py b/marimo/_plugins/core/web_component.pyindex 2dfbda575f..e4be8d150a 100644--- a/marimo/_plugins/core/web_component.py+++ b/marimo/_plugins/core/web_component.py@@ -54,7 +54,13 @@ def _build_attr(name: str, value: JSONType) -> str:# when embedding in markdown# - dollar sign, when unescaped can incorrectly be recognized as# latex delimiter when embedding into markdown- processed = processed.replace("\\", "\").replace("$", "$")+ # - brackets can be interpreted as markdown links or images+ processed = (+ processed.replace("\\", "\")+ .replace("$", "$")+ .replace("[", "[")+ .replace("]", "]")+ )return f"data-{name}='{processed}'"diff --git a/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txt b/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txtindex 6804cd7cf4..7fac4dfd35 100644--- a/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txt+++ b/tests/_cli/snapshots/export/ipynb/ipynb_with_media_outputs.txt@@ -73,7 +73,7 @@{"data": {"text/markdown": [- "<marimo-slider data-initial-value='0' data-label='null' data-start='0' data-stop='10' data-steps='[]' data-debounce='false' data-disabled='false' data-orientation='&quot;horizontal&quot;' data-show-value='false' data-include-input='false' data-full-width='false'></marimo-slider>"+ "<marimo-slider data-initial-value='0' data-label='null' data-start='0' data-stop='10' data-steps='&#91;&#93;' data-debounce='false' data-disabled='false' data-orientation='&quot;horizontal&quot;' data-show-value='false' data-include-input='false' data-full-width='false'></marimo-slider>"]},"metadata": {},diff --git a/tests/_islands/snapshots/island-mimetypes.txt b/tests/_islands/snapshots/island-mimetypes.txtindex 53369d3476..6a75b888c0 100644--- a/tests/_islands/snapshots/island-mimetypes.txt+++ b/tests/_islands/snapshots/island-mimetypes.txt@@ -34,7 +34,7 @@data-reactive="true"><marimo-cell-output>- <marimo-json-output data-json-data='[1,"text/markdown:\u003cspan class=\"markdown prose dark:prose-invert contents\"\u003e\u003cspan class=\"paragraph\"\u003eHello\u003c/span\u003e\u003c/span\u003e"]' data-value-types='"python"'></marimo-json-output>+ <marimo-json-output data-json-data='[1,"text/markdown:\u003cspan class=\"markdown prose dark:prose-invert contents\"\u003e\u003cspan class=\"paragraph\"\u003eHello\u003c/span\u003e\u003c/span\u003e"]' data-value-types='"python"'></marimo-json-output></marimo-cell-output><marimo-cell-code hidden>%5B1%2C%20mo.md('Hello')%5D</marimo-cell-code></marimo-island>\ No newline at end of filediff --git a/tests/_output/formatters/test_formatters.py b/tests/_output/formatters/test_formatters.pyindex c53a594858..bdda3c5317 100644--- a/tests/_output/formatters/test_formatters.py+++ b/tests/_output/formatters/test_formatters.py@@ -707,7 +707,7 @@ def test_as_html_basic_types() -> None:result = as_html([1, 2, 3])assert (result.text- == "<marimo-json-output data-json-data='[1,2,3]' data-value-types='"python"'></marimo-json-output>"+ == "<marimo-json-output data-json-data='[1,2,3]' data-value-types='"python"'></marimo-json-output>")# Dict@@ -833,7 +833,7 @@ def test_as_html_with_plain_wrapper() -> None:result = as_html(plain_list)assert (result.text- == "<marimo-json-output data-json-data='[1,2,3]' data-value-types='"python"'></marimo-json-output>"+ == "<marimo-json-output data-json-data='[1,2,3]' data-value-types='"python"'></marimo-json-output>")# Test with a dict that would normally get marimo-json formattingdiff --git a/tests/_plugins/core/test_web_component.py b/tests/_plugins/core/test_web_component.pyindex 60b6eac13d..2678ddf1a0 100644--- a/tests/_plugins/core/test_web_component.py+++ b/tests/_plugins/core/test_web_component.py@@ -3,6 +3,9 @@ from __future__ import annotationsimport re+import pytest++import marimo as mofrom marimo._output.formatting import as_htmlfrom marimo._output.md import mdfrom marimo._plugins.core.web_component import (@@ -51,6 +54,22 @@ def test_embed_dollar_sign_in_md() -> None:assert "arithmatex" not in markdown.text+ "value",+ [+ "C[A](C)",+ "[link](https://example.com)",+ "",+ "[a][b]",+ ],+)+def test_embed_markdown_link_in_md(value: str) -> None:+ element = mo.ui.text(value=value, label="**Smile String** :")+ rendered = md(f"{element}\n\n[b]: https://example.com")++ assert parse_initial_value(rendered.text) == value++# DOMPurify's SAFE_FOR_XML rule strips any attribute whose value matches# `]>`, `-->`, `--!>`, or `</(style|script|title|xmp|textarea|noscript|iframe# |noembed|noframes)`. `_build_attr` must emit JSON-escaped `<`/`>` so thediff --git a/tests/_plugins/stateless/test_accordion.py b/tests/_plugins/stateless/test_accordion.pyindex ea447488dd..68d583c76b 100644--- a/tests/_plugins/stateless/test_accordion.py+++ b/tests/_plugins/stateless/test_accordion.py@@ -103,12 +103,16 @@ def test_accordion_expanded(@pytest.mark.parametrize("multiple", [False, True])def test_empty_accordion_expanded(multiple: bool) -> None:result = accordion({}, multiple=multiple, expanded=True)- assert "data-expanded='[]'" in result.text+ match = re.search("data-expanded='(.*?)'", result.text)+ assert match is not None+ assert json.loads(html.unescape(match[1])) == []def test_accordion_default_collapsed() -> None:result = accordion({"Summary": "Overview"})- assert "data-expanded='[]'" in result.text+ match = re.search("data-expanded='(.*?)'", result.text)+ assert match is not None+ assert json.loads(html.unescape(match[1])) == []@pytest.mark.parametrize("multiple", [False, True])diff --git a/tests/_plugins/stateless/test_routes.py b/tests/_plugins/stateless/test_routes.pyindex b37149bcae..624926b009 100644--- a/tests/_plugins/stateless/test_routes.py+++ b/tests/_plugins/stateless/test_routes.py@@ -1,6 +1,10 @@# Copyright 2026 Marimo. All rights reserved.from __future__ import annotations+import html+import json+import re+from marimo._runtime.context import get_contextfrom marimo._runtime.context.types import RuntimeContextfrom marimo._runtime.runtime import Kernel@@ -74,4 +78,6 @@ async def test_routes_non_lazy(k: Kernel, exec_req: ExecReqProvider) -> None:routes = k.globals["routes"]children = "<span>42</span><span>45</span>"assert children in routes.text- assert "data-routes='["#/","{/*path}"]'" in routes.text+ match = re.search("data-routes='(.*?)'", routes.text)+ assert match is not None+ assert json.loads(html.unescape(match[1])) == ["#/", "{/*path}"]