Skip to content

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

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

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

clear() -> None

Clear a cell's output.

marimo.output.replace_at_index

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.

Display cell code in marimo's app views

Use mo.show_code() to display the cell's code in the output area, which will then be visible in all app views.

marimo.show_code

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 the cell's output. This is useful if you want a cell's code to appear in the app preview or when running the notebook as an app with marimo run.

In the displayed code, all occurrences of mo.show_code(...) will be replaced with ...

Show code that produces the output factorial(5):

def factorial(n: int) -> int:
    if n == 0:
        return 1
    return n * factorial(n - 1)


mo.show_code(factorial(5))

Show code of a cell, without an output:

def factorial(n: int) -> int:
    if n == 0:
        return 1
    return n * factorial(n - 1)


mo.show_code()

Args:

  • output: the output to display above the cell's code; omit the output to just show the cell's code, without an output.

Returns:

HTML of the output arg displayed with its code.

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

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

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

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

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()