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 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 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.replace_at_index
¶
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 objectidx
: 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:
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.