Skip to content

Control flow

Controlling when cells run

Lazy execution

In addition to these utilities, you can configure the runtime to be lazy, marking cells as stale instead of automatically running them.

marimo.stop

stop(
    predicate: bool, output: Optional[object] = None
) -> None

Stops execution of a cell when predicate is True

When predicate is True, this function raises a MarimoStopError. If uncaught, this exception stops execution of the current cell and makes output its output. Any descendants of this cell that were previously scheduled to run will not be run, and their defs will be removed from program memory.

Examples:

mo.stop(form.value is None, mo.md("**Submit the form to continue.**"))
PARAMETER DESCRIPTION
predicate

The predicate indicating whether to stop.

TYPE: bool

output

The output to be assigned to the current cell.

TYPE: bool DEFAULT: None

RAISES DESCRIPTION
MarimoStopError

When predicate is True

Threading

marimo.Thread

Thread(*args: Any, **kwargs: Any)

Bases: Thread

A Thread subclass that can communicate with the frontend.

mo.Thread has the same API as threading.Thread, but mo.Threads are able to communicate with the marimo frontend, whereas threading.Thread can't.

Threads can append to a cell's output using mo.output.append, or to the console output area using print. The corresponding outputs will be forwarded to the frontend.

Writing directly to sys.stdout or sys.stderr, or to file descriptors 1 and 2, is not yet supported.

Thread lifecycle. When the cell that spawned this thread is invalidated (re-run, deleted, interrupted, or otherwise errored), this thread's should_exit property will evaluate to True, at which point it is the developer's responsibility to clean up their thread.

Example.

def target():
    import time
    import marimo as mo

    thread = mo.current_thread()
    while not thread.should_exit:
        time.sleep(1)
        print("hello")
import marimo as mo

mo.Thread(target=target).start()

should_exit property

should_exit: bool

Whether this thread should exit.

Returns True when the cell that spawned this thread has been invalidated; for example, if the cell:

  • was re-run
  • was deleted
  • was interrupted

then this property evaluates to True.

It is the developer's responsibility to clean up and finish their thread when this flag is set. Retrieve the current mo.Thread with

import marimo as mo

mo.current_thread()

run

run() -> None

marimo.current_thread

current_thread() -> Thread

Return the marimo.Thread object for the caller's thread of control.

RETURNS DESCRIPTION
Thread

The current marimo.Thread object.

RAISES DESCRIPTION
RuntimeError

If the current thread of control is not a marimo.Thread.