Outputs

Cell outputs

Cell outputs

Every cell in a marimo notebook can have a visual output. When editing, outputs are displayed above cells. When running a notebook as an app, its UI is an arrangement of outputs.

A cell’s output is by default its last expression. You can also create outputs programmatically, using mo.output.replace() and mo.output.append().

marimo.output.replace(value: object) None

Replace a cell’s output with a new one.

Call mo.output.replace() to write to a cell’s output area, replacing the existing output, if any.

Args:

  • value: object to output

marimo.output.append(value: object) None

Append a new object to a cell’s output.

Call this function to incrementally build a cell’s output. Appended outputs are stacked vertically.

Args:

  • value: object to output

marimo.output.clear() None

Clear a cell’s output.

marimo.output.replace_at_index(value: object, idx: int) None

Replace a cell’s output at the given index with value.

Call this function to replace an existing object in a cell’s output. If idx is equal to the length of the output, this is equivalent to an append.

Args:

  • value: new object to replace an existing object

  • idx: index of output to replace

Last expression replaces existing output

Ending a cell with a non-None expression is the same as calling mo.output.replace() on it: the last expression replaces any output you may have already written. Wrap the last expression in mo.output.append if you want to add to an existing output instead of replacing it.

Console outputs

Console outputs

Text written to stdout/stderr, including print statements and logs, shows 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_stderr():
    # 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()