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: mo.show_code followups (#4059)

Myles Scolnick33bd1b4e719f
diff --git a/marimo/_output/show_code.py b/marimo/_output/show_code.py
index 0e47688351..4909956109 100644
--- a/marimo/_output/show_code.py
+++ b/marimo/_output/show_code.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import re
+from typing import Literal
from marimo._output.formatting import as_html
from marimo._output.hypertext import Html
@@ -14,7 +15,14 @@ from marimo._runtime.context.types import ContextNotInitializedError
def substitute_show_code_with_arg(code: str) -> str:
pattern = r"mo\.show_code\((.*)\)"
modified_code = re.sub(pattern, r"\1", code, flags=re.DOTALL).strip()
- # Remove code_first=True or code_first=False from the end
+ # Remove position=above or position=below from the end
+ modified_code = re.sub(
+ r",?\s*position\s*=\s*[\"']?(above|below)[\"']?,?\s*\)?$",
+ "",
+ modified_code,
+ flags=re.DOTALL,
+ ).strip()
+ # For backward compatibility, also handle code_first
modified_code = re.sub(
r",?\s*code_first\s*=\s*(True|False),?\s*\)?$",
"",
@@ -24,7 +32,9 @@ def substitute_show_code_with_arg(code: str) -> str:
return modified_code
-def show_code(output: object = None, code_first: bool = False) -> Html:
+def show_code(
+ output: object = None, *, position: Literal["above", "below"] = "below"
+) -> Html:
"""Display an output along with the code of the current cell.
Use `mo.show_code` to show the code of the current cell along with
@@ -61,15 +71,20 @@ def show_code(output: object = None, code_first: bool = False) -> Html:
**Args:**
- - `output`: the output to display above the cell's code; omit the output
- to just show the cell's code, without an output.
- - `code_first` : If `True`, the code will be displayed above the output.
- If `False` (default), the output appears above the code.
+ - `output`: the output to display with the cell's code; omit the output
+ to just show the cell's code.
+ - `position`: Where to display the code relative to the output.
+ Use "above" to show code above the output, or "below" (default) to show
+ code below the output.
**Returns:**
HTML of the `output` arg displayed with its code.
"""
+ assert position in ["above", "below"], (
+ "position must be 'above' or 'below'"
+ )
+
try:
context = get_context()
except ContextNotInitializedError:
@@ -83,7 +98,7 @@ def show_code(output: object = None, code_first: bool = False) -> Html:
code = substitute_show_code_with_arg(cell.code)
if output is not None:
- if code_first:
+ if position == "above":
return vstack(
[
code_editor(value=code, disabled=True, min_height=1),
diff --git a/tests/_output/test_show_code.py b/tests/_output/test_show_code.py
index 44212314a5..e947903b3e 100644
--- a/tests/_output/test_show_code.py
+++ b/tests/_output/test_show_code.py
@@ -1,4 +1,8 @@
-from marimo._output.show_code import substitute_show_code_with_arg
+from __future__ import annotations
+
+import pytest
+
+from marimo._output.show_code import show_code, substitute_show_code_with_arg
from marimo._runtime.runtime import Kernel
from tests.conftest import ExecReqProvider
@@ -82,13 +86,13 @@ hello, world
)
-async def test_show_code_code_first_true(
+async def test_show_code_position_above(
k: Kernel, exec_req: ExecReqProvider
) -> None:
await k.run(
[
exec_req.get(
- "import marimo as mo; x = mo.show_code(1, code_first=True)"
+ 'import marimo as mo; x = mo.show_code(1, position="above")'
)
]
)
@@ -104,13 +108,13 @@ async def test_show_code_code_first_true(
), "Code should appear before output"
-async def test_show_code_code_first_false(
+async def test_show_code_position_below(
k: Kernel, exec_req: ExecReqProvider
) -> None:
await k.run(
[
exec_req.get(
- "import marimo as mo; x = mo.show_code(1, code_first=False)"
+ 'import marimo as mo; x = mo.show_code(1, position="below")'
)
]
)
@@ -126,7 +130,7 @@ async def test_show_code_code_first_false(
), "Output should appear before code"
-async def test_show_code_code_first_default(
+async def test_show_code_position_default(
k: Kernel, exec_req: ExecReqProvider
) -> None:
await k.run([exec_req.get("import marimo as mo; x = mo.show_code(1)")])
@@ -142,22 +146,40 @@ async def test_show_code_code_first_default(
), "Output should appear before code"
-def test_substitute_show_code_removes_code_first() -> None:
+def test_fails_if_with_bad_args() -> None:
+ with pytest.raises(AssertionError):
+ show_code(1, position="foo")
+
+ with pytest.raises(TypeError):
+ show_code(1, "below")
+
+ with pytest.raises(AssertionError):
+ show_code(1, position=None)
+
+
+def test_substitute_show_code_removes_position() -> None:
# Basic cases
- code = "mo.show_code(1, code_first=True)"
+ code = 'mo.show_code(1, position="above")'
assert substitute_show_code_with_arg(code) == "1"
- code = "mo.show_code(1, code_first=False)"
+ code = 'mo.show_code(1, position="below")'
assert substitute_show_code_with_arg(code) == "1"
- code = "mo.show_code(foo(1) + 1, code_first=True)"
+ code = 'mo.show_code(foo(1) + 1, position="above")'
assert substitute_show_code_with_arg(code) == "foo(1) + 1"
+ # Single quote in string
+ code = "mo.show_code('hello, world', position='above')"
+ assert substitute_show_code_with_arg(code) == "'hello, world'"
+
+ code = "mo.show_code('hello, world', position='below')"
+ assert substitute_show_code_with_arg(code) == "'hello, world'"
+
# Case with extra spaces
- code = "mo.show_code( 42 , code_first = True )"
+ code = 'mo.show_code( 42 , position = "above" )'
assert substitute_show_code_with_arg(code) == "42"
- code = "mo.show_code(foo(2), bar(3), code_first=True)"
+ code = 'mo.show_code(foo(2), bar(3), position="above")'
assert substitute_show_code_with_arg(code) == "foo(2), bar(3)"
code = "mo.show_code(100)"
@@ -166,62 +188,95 @@ def test_substitute_show_code_removes_code_first() -> None:
code = "mo.show_code(foo(bar(5)))"
assert substitute_show_code_with_arg(code) == "foo(bar(5))"
- code = "mo.show_code(mo.show_code(1), code_first=True)"
+ code = 'mo.show_code(mo.show_code(1), position="above")'
assert substitute_show_code_with_arg(code) == "mo.show_code(1)"
- code = "mo.show_code(mo.show_code(foo(3) + 1, code_first=False), code_first=True)"
+ code = 'mo.show_code(mo.show_code(foo(3) + 1, position="below"), position="above")'
assert (
substitute_show_code_with_arg(code)
- == "mo.show_code(foo(3) + 1, code_first=False)"
+ == 'mo.show_code(foo(3) + 1, position="below")'
)
- code = "mo.show_code((x + y) * 2, code_first=True)"
+ code = 'mo.show_code((x + y) * 2, position="above")'
assert substitute_show_code_with_arg(code) == "(x + y) * 2"
- code = "mo.show_code({'a': 1, 'b': 2}, code_first=False)"
+ code = "mo.show_code({'a': 1, 'b': 2}, position=\"below\")"
assert substitute_show_code_with_arg(code) == "{'a': 1, 'b': 2}"
- code = "mo.show_code([i for i in range(10)], code_first=True)"
+ code = 'mo.show_code([i for i in range(10)], position="above")'
assert substitute_show_code_with_arg(code) == "[i for i in range(10)]"
code = "mo.show_code()"
assert substitute_show_code_with_arg(code) == ""
- code = "mo.show_code(, code_first=True)"
+ code = 'mo.show_code(, position="above")'
assert substitute_show_code_with_arg(code) == ""
- code = 'mo.show_code("hello, world", code_first=True)'
+ code = 'mo.show_code("hello, world", position="above")'
assert substitute_show_code_with_arg(code) == '"hello, world"'
- code = "mo.show_code('single quotes test', code_first=False)"
+ code = "mo.show_code('single quotes test', position=\"below\")"
assert substitute_show_code_with_arg(code) == "'single quotes test'"
- code = "mo.show_code(mo.show_code(1, code_first=False), code_first=True)"
+ code = 'mo.show_code(mo.show_code(1, position="below"), position="above")'
assert (
substitute_show_code_with_arg(code)
- == "mo.show_code(1, code_first=False)"
+ == 'mo.show_code(1, position="below")'
)
- code = "mo.show_code(mo.show_code(foo(3) + 1, code_first=False), code_first=True)"
+ code = 'mo.show_code(mo.show_code(foo(3) + 1, position="below"), position="above")'
assert (
substitute_show_code_with_arg(code)
- == "mo.show_code(foo(3) + 1, code_first=False)"
+ == 'mo.show_code(foo(3) + 1, position="below")'
)
- code = "mo.show_code(mo.show_code(42, code_first=True), code_first=False)"
+ code = 'mo.show_code(mo.show_code(42, position="above"), position="below")'
assert (
substitute_show_code_with_arg(code)
- == "mo.show_code(42, code_first=True)"
+ == 'mo.show_code(42, position="above")'
)
code = """mo.show_code(
42,
- code_first=True,
+ position=\"above\",
)"""
assert substitute_show_code_with_arg(code) == "42"
code = """mo.show_code(
42,
- code_first=True,
+ position=\"above\",
)"""
assert substitute_show_code_with_arg(code) == "42"
+
+ # Nested calls with different positions
+ code = 'mo.show_code(mo.show_code(1, position="above"), position="below")'
+ assert (
+ substitute_show_code_with_arg(code)
+ == 'mo.show_code(1, position="above")'
+ )
+
+ # Calls with additional whitespace and line breaks
+ code = 'mo.show_code(\n mo.show_code(42, position="above"),\n position="below"\n)'
+ assert (
+ substitute_show_code_with_arg(code)
+ == 'mo.show_code(42, position="above")'
+ )
+
+ # Calls with complex expressions
+ code = 'mo.show_code((lambda x: x + 1)(5), position="above")'
+ assert substitute_show_code_with_arg(code) == "(lambda x: x + 1)(5)"
+
+ code = 'mo.show_code([i**2 for i in range(5)], position="below")'
+ assert substitute_show_code_with_arg(code) == "[i**2 for i in range(5)]"
+
+ code = 'mo.show_code({"key": "value"}, position="above")'
+ assert substitute_show_code_with_arg(code) == '{"key": "value"}'
+
+
+ reason="Doesn't handle complex expressions for position",
+ strict=True,
+)
+def test_fails_if_complex_expression_for_position() -> None:
+ code = 'mo.show_code(10, position="above" if 1 else "below")'
+ assert substitute_show_code_with_arg(code) == "10"