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.

improve: Python-like values in JSON outputs (#2105)

Akshay Agrawal45179b28fa47
diff --git a/frontend/src/components/editor/output/JsonOutput.tsx b/frontend/src/components/editor/output/JsonOutput.tsx
index 1e3da4d599..a7208af9b6 100644
--- a/frontend/src/components/editor/output/JsonOutput.tsx
+++ b/frontend/src/components/editor/output/JsonOutput.tsx
@@ -1,6 +1,12 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { memo } from "react";
-import { type DataType, JsonViewer } from "@textea/json-viewer";
+import {
+ type DataType,
+ JsonViewer,
+ booleanType,
+ defineDataType,
+ nullType,
+} from "@textea/json-viewer";
import { HtmlOutput } from "./HtmlOutput";
import { ImageOutput } from "./ImageOutput";
@@ -82,13 +88,25 @@ const LEAF_RENDERERS = {
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
-const VALUE_TYPE: Array<DataType<any>> = Object.entries(LEAF_RENDERERS).map(
+const MIME_TYPES: Array<DataType<any>> = Object.entries(LEAF_RENDERERS).map(
([leafType, render]) => ({
is: (value) => typeof value === "string" && value.startsWith(leafType),
Component: (props) => renderLeaf(props.value, render),
}),
);
+const PYTHON_BOOLEAN_TYPE = defineDataType<boolean>({
+ ...booleanType,
+ Component: ({ value }) => <span>{value ? "True" : "False"}</span>,
+});
+
+const PYTHON_NONE_TYPE = defineDataType<null>({
+ ...nullType,
+ Component: () => <span>None</span>,
+});
+
+const VALUE_TYPE = [...MIME_TYPES, PYTHON_BOOLEAN_TYPE, PYTHON_NONE_TYPE];
+
function leafData(leaf: string): string {
const delimIndex = leaf.indexOf(":");
if (delimIndex === -1) {
diff --git a/marimo/_output/formatters/structures.py b/marimo/_output/formatters/structures.py
index baefa178cf..53368a74f0 100644
--- a/marimo/_output/formatters/structures.py
+++ b/marimo/_output/formatters/structures.py
@@ -11,14 +11,21 @@ from marimo._output.formatters.formatter_factory import FormatterFactory
from marimo._utils.flatten import CyclicStructureError, flatten
-def _leaf_formatter(value: object) -> str:
+def _leaf_formatter(value: object) -> bool | None | str:
formatter = formatting.get_formatter(value)
- if formatter is None:
+ if formatter is not None:
+ return ":".join(formatter(value))
+ if isinstance(value, bool):
+ return value
+ elif value is None:
+ return value
+ else:
+ # floats and ints are still converted to strings because JavaScript
+ # can't reliably distinguish between them (eg 1 and 1.0)
try:
return f"text/plain:{json.dumps(value)}"
except TypeError:
return f"text/plain:{value}"
- return ":".join(formatter(value))
def format_structure(
diff --git a/tests/_output/formatters/test_structures.py b/tests/_output/formatters/test_structures.py
index d36f987692..6e2aff6559 100644
--- a/tests/_output/formatters/test_structures.py
+++ b/tests/_output/formatters/test_structures.py
@@ -1,6 +1,9 @@
from __future__ import annotations
+from typing import Any, List, cast
+
from marimo._dependencies.dependencies import DependencyManager
+from marimo._output.formatters.structures import format_structure
from marimo._runtime.runtime import Kernel
from tests.conftest import ExecReqProvider
@@ -33,3 +36,15 @@ async def test_matplotlib_special_case(
lines = executing_kernel.globals["lines"]
assert formatter is not None
assert formatter(lines)[0].startswith("image")
+
+
+def test_format_structure_types() -> None:
+ formatted = cast(
+ List[Any], format_structure(["hello", True, False, None, 1, 1.0])
+ )
+ assert formatted[0] == 'text/plain:"hello"'
+ assert formatted[1] is True
+ assert formatted[2] is False
+ assert formatted[3] is None
+ assert formatted[4] == "text/plain:1"
+ assert formatted[5] == "text/plain:1.0"