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
fix: show code should be greedy, remove position (#2350)
Akshay Agrawala1951827f5a3
diff --git a/marimo/_output/show_code.py b/marimo/_output/show_code.pyindex e863d202f8..48fa58527a 100644--- a/marimo/_output/show_code.py+++ b/marimo/_output/show_code.py@@ -2,7 +2,6 @@from __future__ import annotationsimport re-from typing import Literalfrom marimo._output.formatting import as_htmlfrom marimo._output.hypertext import Html@@ -12,9 +11,12 @@ from marimo._runtime.context import get_contextfrom marimo._runtime.context.types import ContextNotInitializedError-def show_code(- output: object = None, position: Literal["above", "below"] = "below"-) -> Html:+def substitute_show_code_with_arg(code: str) -> str:+ pattern = r"mo\.show_code\((.*)\)"+ return re.sub(pattern, r"\1", code, flags=re.DOTALL).strip()+++def show_code(output: object = None) -> 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@@ -53,7 +55,6 @@ def show_code(- output: the output to display above the cell's code; omit the outputto just show the cel's code, without an output.- - position: whether the output should be above or below the code**Returns:**@@ -69,8 +70,7 @@ def show_code(return as_html(output)cell = context.graph.cells[cell_id]- pattern = r"mo\.show_code\((.*?)\)"- code = re.sub(pattern, r"\1", cell.code, flags=re.DOTALL).strip()+ code = substitute_show_code_with_arg(cell.code)if output is not None:return vstack(@@ -78,11 +78,6 @@ def show_code(code_editor(value=code, disabled=True, min_height=1),as_html(output),]- if position == "below"- else [- as_html(output),- code_editor(value=code, disabled=True, min_height=1),- ])else:return code_editor(value=code, disabled=True, min_height=1)diff --git a/tests/_output/test_show_code.py b/tests/_output/test_show_code.pyindex e377f274c0..a34dc5285e 100644--- a/tests/_output/test_show_code.py+++ b/tests/_output/test_show_code.py@@ -1,3 +1,4 @@+from marimo._output.show_code import substitute_show_code_with_argfrom marimo._runtime.runtime import Kernelfrom tests.conftest import ExecReqProvider@@ -37,3 +38,45 @@ async def test_show_code_multiline(assert "(" not in k.globals["x"].textassert ")" not in k.globals["x"].textassert "mo.show_code" not in k.globals["x"].text+++def test_substitute_show_code() -> None:+ code = "mo.show_code()"+ assert substitute_show_code_with_arg(code) == ""++ code = "mo.show_code(1)"+ assert substitute_show_code_with_arg(code) == "1"++ code = "mo.show_code(foo(1) + 1)"+ assert substitute_show_code_with_arg(code) == "foo(1) + 1"++ code = "mo.show_code(mo.show_code(1))"+ assert substitute_show_code_with_arg(code) == "mo.show_code(1)"++ code = """+mo.show_code(+'''+hello, world+'''+)+"""+ assert (+ substitute_show_code_with_arg(code)+ == """'''+hello, world+'''"""+ )++ code = """+mo.show_code(+foo(1) + '''+hello, world+'''+)+"""+ assert (+ substitute_show_code_with_arg(code)+ == """foo(1) + '''+hello, world+'''"""+ )