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
improvement: add _repr_markdown_ to Md component, and _repr_html_ to Html component (#3021)
Myles Scolnick6b6144ea1705
diff --git a/marimo/_output/hypertext.py b/marimo/_output/hypertext.pyindex d961c7cd0a..741b1aaa55 100644--- a/marimo/_output/hypertext.py+++ b/marimo/_output/hypertext.py@@ -257,6 +257,9 @@ class Html(MIME):return _style.style(self, style=style, **kwargs)+ def _repr_html_(self) -> str:+ return self.text+def _js(text: str) -> Html:# TODO: interpolation of Python values to javascriptdiff --git a/marimo/_output/md.py b/marimo/_output/md.pyindex c341d2f46a..79b5b3c129 100644--- a/marimo/_output/md.py+++ b/marimo/_output/md.py@@ -36,56 +36,66 @@ extension_configs = {MarkdownSize = Literal["sm", "base", "lg", "xl", "2xl"]-def _md(- text: str,- apply_markdown_class: bool = True,- size: Optional[MarkdownSize] = None,-) -> Html:- # cleandoc uniformly strips leading whitespace; useful for- # indented multiline strings- text = cleandoc(text)- # markdown.markdown appends a newline, hence strip- html_text = markdown.markdown(- text,- extensions=[- # Syntax highlighting- "codehilite",- # Markdown tables- "tables",- # LaTeX- "pymdownx.arithmatex",- # Subscripts and strikethrough- "pymdownx.tilde",- # Better code blocks- "pymdownx.superfences",- # Table of contents- # This adds ids to the HTML headers- "toc",- # Footnotes- "footnotes",- # Admonitions- "admonition",- # Sane lists, to include <ol start="n">- "sane_lists",- # Links- ExternalLinksExtension(),- # Iconify- IconifyExtension(),- ],- extension_configs=extension_configs, # type: ignore[arg-type]- ).strip()- # replace <p> tags with <span> as HTML doesn't allow nested <div>s in <p>s- html_text = html_text.replace("<p>", '<span class="paragraph">').replace(- "</p>", "</span>"- )-- if apply_markdown_class:- classes = ["markdown", "prose", "dark:prose-invert"]- if size is not None:- classes.append(f"prose-{size}")- return Html(f'<span class="{" ".join(classes)}">{html_text}</span>')- else:- return Html(html_text)+class _md(Html):+ def __init__(+ self,+ text: str,+ *,+ apply_markdown_class: bool = True,+ size: Optional[MarkdownSize] = None,+ ) -> None:+ # cleandoc uniformly strips leading whitespace; useful for+ # indented multiline strings+ text = cleandoc(text)+ self._markdown_text = text++ # markdown.markdown appends a newline, hence strip+ html_text = markdown.markdown(+ text,+ extensions=[+ # Syntax highlighting+ "codehilite",+ # Markdown tables+ "tables",+ # LaTeX+ "pymdownx.arithmatex",+ # Subscripts and strikethrough+ "pymdownx.tilde",+ # Better code blocks+ "pymdownx.superfences",+ # Table of contents+ # This adds ids to the HTML headers+ "toc",+ # Footnotes+ "footnotes",+ # Admonitions+ "admonition",+ # Sane lists, to include <ol start="n">+ "sane_lists",+ # Links+ ExternalLinksExtension(),+ # Iconify+ IconifyExtension(),+ ],+ extension_configs=extension_configs, # type: ignore[arg-type]+ ).strip()+ # replace <p> tags with <span> as HTML doesn't allow nested <div>s in <p>s+ html_text = html_text.replace(+ "<p>", '<span class="paragraph">'+ ).replace("</p>", "</span>")++ if apply_markdown_class:+ classes = ["markdown", "prose", "dark:prose-invert"]+ if size is not None:+ classes.append(f"prose-{size}")+ super().__init__(+ f'<span class="{" ".join(classes)}">{html_text}</span>'+ )+ else:+ super().__init__(html_text)++ def _repr_markdown_(self) -> str:+ return self._markdown_text@mddocdiff --git a/tests/_output/test_hypertext.py b/tests/_output/test_hypertext.pyindex af858a84c4..6a0ce002c2 100644--- a/tests/_output/test_hypertext.py+++ b/tests/_output/test_hypertext.py@@ -121,3 +121,8 @@ def test_js_multiple_lines():assert "console.log('Line 1');" in js_html.textassert "console.log('Line 2');" in js_html.textassert "</script>" in js_html.text+++def test_html_repr_html():+ html = Html("<p>Hello</p>")+ assert html._repr_html_() == "<p>Hello</p>"diff --git a/tests/_output/test_md.py b/tests/_output/test_md.pyindex c4d54a1aa6..552cf08f55 100644--- a/tests/_output/test_md.py+++ b/tests/_output/test_md.py@@ -85,3 +85,9 @@ def test_md_sane_lists() -> None:input_text = "2. hey\n3. hey"expected_output = '<ol start="2">\n<li>hey</li>\n<li>hey</li>\n</ol>'assert _md(input_text, apply_markdown_class=False).text == expected_output+++def test_md_repr_markdown():+ input_text = "This is **bold** and this is _italic_."+ md = _md(input_text)+ assert md._repr_markdown_() == input_text