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: print logs when running 'marimo export' (#4969)

Myles Scolnick11cb920b2c08
diff --git a/marimo/_server/export/__init__.py b/marimo/_server/export/__init__.py
index 4b9027762a..c83b8fdac9 100644
--- a/marimo/_server/export/__init__.py
+++ b/marimo/_server/export/__init__.py
@@ -7,6 +7,7 @@ import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING, Any, Callable, Literal, Optional, Union, cast
+from marimo import _loggers
from marimo._cli.print import echo
from marimo._config.config import RuntimeConfig
from marimo._config.manager import (
@@ -29,6 +30,8 @@ from marimo._types.ids import ConsumerId
from marimo._utils.marimo_path import MarimoPath
from marimo._utils.parse_dataclass import parse_raw
+LOGGER = _loggers.marimo_logger()
+
if TYPE_CHECKING:
from pathlib import Path
@@ -233,12 +236,13 @@ async def run_app_until_completion(
# Print errors to stderr
if message[0] == "cell-op":
op_data = message[1]
+ output = op_data.get("output")
+ console_output = op_data.get("console")
if (
- op_data.get("output")
- and op_data["output"]["channel"]
- == CellChannel.MARIMO_ERROR
+ output
+ and output["channel"] == CellChannel.MARIMO_ERROR
):
- errors = cast(list[Any], op_data["output"]["data"])
+ errors = cast(list[Any], output["data"])
@dataclass
class Container:
@@ -253,6 +257,26 @@ async def run_app_until_completion(
file=sys.stderr,
)
self.did_error = True
+
+ if console_output:
+ console_as_list: list[dict[str, Any]] = (
+ console_output
+ if isinstance(console_output, list)
+ else [console_output]
+ )
+ try:
+ for line in console_as_list:
+ # We print to stderr to not interfere with the
+ # piped output
+ mimetype = line.get("mimetype")
+ if mimetype == "text/plain":
+ echo(
+ line["data"], file=sys.stderr, nl=False
+ )
+ except Exception:
+ LOGGER.warning("Error printing console output")
+ pass
+
if message[0] == "completed-run":
instantiated_event.set()
diff --git a/marimo/_smoke_tests/scripts/logging_in_scripts.py b/marimo/_smoke_tests/scripts/logging_in_scripts.py
new file mode 100644
index 0000000000..706db83c51
--- /dev/null
+++ b/marimo/_smoke_tests/scripts/logging_in_scripts.py
@@ -0,0 +1,23 @@
+import marimo
+
+__generated_with = "0.13.10"
+app = marimo.App(width="medium")
+
+
+def _():
+ import marimo as mo
+ import time
+ return mo, time
+
+
+def _(mo, time):
+ for i in mo.status.progress_bar(range(10)):
+ print(f"Step {i}")
+ time.sleep(1)
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_server/export/snapshots/run_until_completion_with_console_output.txt b/tests/_server/export/snapshots/run_until_completion_with_console_output.txt
new file mode 100644
index 0000000000..61816158d2
--- /dev/null
+++ b/tests/_server/export/snapshots/run_until_completion_with_console_output.txt
@@ -0,0 +1,16 @@
+[
+ {
+ "output": "",
+ "console": [
+ "hello stderr"
+ ],
+ "status": "idle"
+ },
+ {
+ "output": "",
+ "console": [
+ "hello stdout"
+ ],
+ "status": "idle"
+ }
+]
\ No newline at end of file
diff --git a/tests/_server/export/test_exporter.py b/tests/_server/export/test_exporter.py
index 430b0846c3..81e3806046 100644
--- a/tests/_server/export/test_exporter.py
+++ b/tests/_server/export/test_exporter.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import json
import sys
from typing import TYPE_CHECKING, Any
+from unittest.mock import MagicMock, patch
import pytest
@@ -428,3 +429,38 @@ def _delete_lines_with_files(output: str) -> str:
return line[0:start] + line[end:]
return "\n".join(remove_file_name(line) for line in output.splitlines())
+
+
+@patch("marimo._server.export.echo")
+async def test_run_until_completion_with_console_output(mock_echo: MagicMock):
+ app = App()
+
+ @app.cell()
+ def _():
+ sys.stdout.write("hello stdout")
+ None
+ return
+
+ @app.cell()
+ def _():
+ import sys
+
+ sys.stderr.write("hello stderr")
+ None
+ return (sys,)
+
+ file_manager = AppFileManager.from_app(InternalApp(app))
+
+ session_view, did_error = await run_app_until_completion(
+ file_manager,
+ cli_args={},
+ argv=None,
+ )
+ assert did_error is False
+ mock_echo.assert_any_call("hello stdout", file=sys.stderr, nl=False)
+ mock_echo.assert_any_call("hello stderr", file=sys.stderr, nl=False)
+ cell_ops = [op for op in session_view.operations if isinstance(op, CellOp)]
+ snapshot(
+ "run_until_completion_with_console_output.txt",
+ _print_messages(cell_ops),
+ )