In normal Python, the following two lines of code could run in two cells that follow eachother.
x = 1
x = x + 1
But in Marimo, this won't work. After all, how is Marimo to figure out in which order these cells should run? It could try and make intelligent guesses, but it prefers to have state be handled more explicitly.
Working with state
In Marimo you can declare state explicitly by using the mo.state
call.
get_value, set_value = mo.state(0)
By defining the state like this you can two functions. The first one can be used to get the current value of the state, the second one can be used to update the state. Typically you would not call these functions directly but rather use them in combination with a user interface.
slider = mo.ui.slider(0, 100, 1, value=get_value(), on_change=set_value)
number = mo.ui.number(0, 100, 1, value=get_value(), on_change=set_value)
In this example, we've created a slider and a number input that both use the state functions to correctly deal with input changes. Both of these inputs are connected to the same state which means that if you change the value of the slider, the number input will also update.
If you are keen to learn more, make sure to check the marimo documentation on state.