State#

marimo.state(value: T) tuple[marimo._runtime.state.State[T], Callable[[T], NoneType]]#

Mutable reactive state

This function takes an initial value and returns:

  • a getter function that reads the state value

  • a setter function to set the state’s value

When you call the setter function and update the state value in one cell, all other cells that read any global variables assigned to the getter will automatically run.

You can use this function in conjunction with UIElement on_change handlers to trigger side-effects when an element’s value is updated. For example, you can tie multiple UI elements to derive their values from shared state.

Basic Usage.

Create state:

get_count, set_count = mo.state(0)

Read the value:

get_count()

Update the state:

set_count(1)

Update the state based on the current value:

set_count(lambda value: value + 1)

Note: Never mutate the state directly. You should only change its value through its setter.

Synchronizing multiple UI elements.

get_state, set_state = mo.state(0)
# updating the state through the slider will recreate the number (below)
slider = mo.ui.slider(0, 100, value=get_state(), on_change=set_state)
# updating the state through the number will recreate the slider (above)
number = mo.ui.number(0, 100, value=get_state(), on_change=set_state)
# slider and number are synchronized to have the same value (try it!)
[slider, number]

Args:

  • value: initial value of the state

Returns:

  • getter function that retrieves the state value

  • setter function that takes a new value, or a function taking the current value as its argument and returning a new value