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: if mimebundle only contains markdown/latex, convert to html (#2881)

Myles Scolnickc3e7275e509e
diff --git a/frontend/src/components/editor/Output.tsx b/frontend/src/components/editor/Output.tsx
index 127e49f83f..249aab82ec 100644
--- a/frontend/src/components/editor/Output.tsx
+++ b/frontend/src/components/editor/Output.tsx
@@ -153,6 +153,8 @@ export const OutputRenderer: React.FC<{
<LazyAnyLanguageCodeMirror
theme={theme === "dark" ? "dark" : "light"}
value={data}
+ readOnly={true}
+ editable={false}
language="markdown"
/>
);
@@ -210,11 +212,20 @@ const MimeBundleOutputRenderer: React.FC<{
);
}
+ const mimeEntries = Objects.entries(mimebundle);
+ // Sort HTML first
+ mimeEntries.sort(([mimeA], [mimeB]) => {
+ if (mimeA === "text/html") {
+ return -1;
+ }
+ return 0;
+ });
+
return (
<Tabs defaultValue={first} orientation="vertical">
<div className="flex">
<TabsList className="self-start max-h-none flex flex-col gap-2 mr-4 flex-shrink-0">
- {Object.keys(mimebundle).map((mime) => (
+ {mimeEntries.map(([mime]) => (
<TabsTrigger
key={mime}
value={mime}
@@ -227,7 +238,7 @@ const MimeBundleOutputRenderer: React.FC<{
))}
</TabsList>
<div className="flex-1">
- {Objects.entries(mimebundle).map(([mime, output]) => (
+ {mimeEntries.map(([mime, output]) => (
<TabsContent key={mime} value={mime}>
<ErrorBoundary>
<OutputRenderer
diff --git a/frontend/src/components/scratchpad/scratchpad.tsx b/frontend/src/components/scratchpad/scratchpad.tsx
index 14f8749fe4..bda357cb28 100644
--- a/frontend/src/components/scratchpad/scratchpad.tsx
+++ b/frontend/src/components/scratchpad/scratchpad.tsx
@@ -35,9 +35,9 @@ import {
scratchpadHistoryAtom,
historyVisibleAtom,
} from "./scratchpad-history";
-import AnyLanguageCodeMirror from "@/plugins/impl/code/any-language-editor";
import { cn } from "@/utils/cn";
import type { CellConfig } from "@/core/network/types";
+import { LazyAnyLanguageCodeMirror } from "@/plugins/impl/code/LazyAnyLanguageCodeMirror";
const scratchpadCellConfig: CellConfig = {
hide_code: false,
@@ -184,7 +184,7 @@ export const ScratchPad: React.FC = () => {
className="border rounded-md hover:shadow-sm cursor-pointer hover:border-input overflow-hidden"
onClick={() => handleSelectHistoryItem(item)}
>
- <AnyLanguageCodeMirror
+ <LazyAnyLanguageCodeMirror
language="python"
theme={theme}
basicSetup={{
diff --git a/marimo/_output/formatters/repr_formatters.py b/marimo/_output/formatters/repr_formatters.py
index e76b389d29..29095cac02 100644
--- a/marimo/_output/formatters/repr_formatters.py
+++ b/marimo/_output/formatters/repr_formatters.py
@@ -56,6 +56,18 @@ def maybe_get_repr_formatter(
# (e.g. ibis)
if "text/plain" in contents and len(contents) > 1:
contents.pop("text/plain")
+ # Convert markdown/latex to text/html if text/html is
+ # not present
+ for md_mime_type in md_mime_types:
+ if (
+ "text/html" not in contents
+ and md_mime_type in contents
+ ):
+ from marimo._output.md import md
+
+ contents["text/html"] = md(
+ str(contents[md_mime_type])
+ ).text
else:
contents = method()
diff --git a/tests/_output/formatters/test_formatters.py b/tests/_output/formatters/test_formatters.py
index a62aec1515..1bcd04da62 100644
--- a/tests/_output/formatters/test_formatters.py
+++ b/tests/_output/formatters/test_formatters.py
@@ -369,3 +369,57 @@ class CustomTuple(tuple[int, int]):
def test_format_extend_tuple():
my_tuple = CustomTuple((1, 2, 3))
assert as_dom_node(my_tuple).text == "<h1>1, 2, 3</h1>"
+
+
+def test_repr_mimebundle_with_markdown():
+ class ReprMimeBundleWithMarkdown:
+ def _repr_mimebundle_(self):
+ return {
+ "text/markdown": "# Hello, World!",
+ }
+
+ obj = ReprMimeBundleWithMarkdown()
+ formatter = get_formatter(obj)
+ assert formatter
+ mime, content = formatter(obj)
+ assert mime == "application/vnd.marimo+mimebundle"
+ assert content == {
+ "text/html": '<span class="markdown prose dark:prose-invert"><h1 id="hello-world">Hello, World!</h1></span>',
+ "text/markdown": "# Hello, World!",
+ }
+
+ # Does not convert markdown to html if html is already present
+ class ReprMimeBundleWithMarkdownAndHtml:
+ def _repr_mimebundle_(self):
+ return {
+ "text/html": "<h1>Hello, World!</h1>",
+ "text/markdown": "# Hello, World!",
+ }
+
+ obj = ReprMimeBundleWithMarkdownAndHtml()
+ formatter = get_formatter(obj)
+ assert formatter
+ mime, content = formatter(obj)
+ assert mime == "application/vnd.marimo+mimebundle"
+ assert content == {
+ "text/html": "<h1>Hello, World!</h1>",
+ "text/markdown": "# Hello, World!",
+ }
+
+
+def test_repr_mimebundle_with_latex():
+ class ReprMimeBundleWithLatex:
+ def _repr_mimebundle_(self):
+ return {
+ "text/latex": r"$e^x$",
+ }
+
+ obj = ReprMimeBundleWithLatex()
+ formatter = get_formatter(obj)
+ assert formatter
+ mime, content = formatter(obj)
+ assert mime == "application/vnd.marimo+mimebundle"
+ assert content == {
+ "text/html": '<span class="markdown prose dark:prose-invert"><span class="paragraph"><marimo-tex class="arithmatex">||(e^x||)</marimo-tex></span></span>',
+ "text/latex": r"$e^x$",
+ }