Console outputs#

Outputs written to stdout or stderr, including print statements and logs, show up in a console output area below a cell.

By default, these console outputs don’t appear when running a marimo notebook as an app. If you do want them to appear in apps, marimo provides utility functions for capturing console outputs and redirecting them to cell outputs.

marimo.redirect_stdout() Iterator[None]#

Redirect stdout to a cell’s output area.

with mo.redirect_stdout():
    # These print statements will show up in the cell's output area
    print("Hello!")
    print("World!")
marimo.redirect_stderr() Iterator[None]#

Redirect stderr to a cell’s output area.

with mo.redirect_stdout():
    # These messages will show up in the cell's output area
    sys.stderr.write("Hello!")
    sys.stderr.write("World!")
marimo.capture_stdout() Iterator[StringIO]#

Capture standard output.

Use this context manager to capture print statements and other output sent to standard output.

Example.

with mo.capture_stdout() as buffer:
   print("Hello!")
output = buffer.getvalue()
marimo.capture_stderr() Iterator[StringIO]#

Capture standard error.

Use this context manager to capture output sent to standard error.

Example.

with mo.capture_stderr() as buffer:
   sys.stderr.write("Hello!")
output = buffer.getvalue()