A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
docs: improve state docs (#380)
Akshay Agrawal6f6328a34180
diff --git a/docs/_static/docs-state-update.gif b/docs/_static/docs-state-update.gifdeleted file mode 100644index e2badd31d5..0000000000Binary files a/docs/_static/docs-state-update.gif and /dev/null differdiff --git a/docs/api/index.md b/docs/api/index.mdindex fe6403cb7b..3e5f308ddb 100644--- a/docs/api/index.md+++ b/docs/api/index.md@@ -37,5 +37,5 @@ Use the marimo library in marimo notebooks (`import marimo as mo`) to| {doc}`outputs` | Modify cell outputs, redirect console output || {doc}`control_flow` | Control how cells execute || {doc}`html` | Manipulate HTML objects |-| {doc}`state` | Make stateful apps with `mo.state` |+| {doc}`state` | Synchronize multiple UI elements with `mo.state` || {doc}`debugging` | Debugging utilities |diff --git a/docs/api/state.md b/docs/api/state.mdindex b95dfca7e1..e28e296296 100644--- a/docs/api/state.md+++ b/docs/api/state.md@@ -1,5 +1,40 @@# State+```{admonition} Advanced topic!+:class: warning++This API doc covers reactive state (`mo.state`), an advanced topic.++**You likely don't need reactive state**. UI elements already have built-in+state, their associated value, which you can access with their `value` attribute.+For example, `mo.ui.slider()` has a value that is its current position on an+interval, while `mo.ui.button()` has a value that can be configured to+count the number of times it has been clicked, or to toggle between `True` and+`False`. Additionally, interacting with UI elements bound to global variables+[automatically executes cells](guides/interactivity) that reference those+variables, letting you react to changes by just reading their+`value` attributes. This functional paradigm is the preferred way of+reacting to UI interactions in marimo. So if you+think you need to use `mo.state`, make sure to first read the [guide on+interactivity](/guides/interactivity.md). Chances are, the reactive execution+built into UI elements will suffice. (For example, [you don't need reactive+state to handle a button click](/recipes.md#working-with-buttons).)+++That said, here are some signs you might need `mo.state`:+- you need to maintain historical state related to a UI element that can't+ be computed from its built-in `value` (_e.g._, all values the user has+ ever input into a form)+- you need to synchronize two different UI elements (_e.g._, so that+ interacting with either one controls the other)+- you need to introduce cycles across cells++If one of these cases applies to you, then read on. `mo.state` lets you+make all kinds of interesting applications, but like mutable state in general,+it can complicate notebook development and has the potential to+introduce hard-to-find bugs.+```+```{eval-rst}.. autofunction:: marimo.state```diff --git a/docs/guides/index.md b/docs/guides/index.mdindex bb68437167..5b16218820 100644--- a/docs/guides/index.md+++ b/docs/guides/index.md@@ -25,7 +25,7 @@ These guides cover marimo's core concepts.| {doc}`interactivity` | Using interactive UI elements || {doc}`outputs` | Markdown, plots, and other visual outputs || {doc}`plotting` | Interactive and reactive plots |-| {doc}`state` | Reacting to interactions with state |+| {doc}`state` | Mutable, reactive state || {doc}`apps` | Running notebooks as apps || {doc}`scripts` | Running notebooks as scripts || {doc}`best_practices` | Best practices when working with marimo |diff --git a/docs/guides/state.md b/docs/guides/state.mdindex 7bac3380c4..c7097b5d8f 100644--- a/docs/guides/state.md+++ b/docs/guides/state.md@@ -1,12 +1,38 @@-# State+# Reactive State```{admonition} Advanced topic!:class: warning-This guide covers reactive state, an advanced topic. You likely don't need-state for day-to-day exploratory data analysis or numerical experiments. We-recommend skipping this guide and only returning here if you feel limited in-your options to responding to user interactions.+This guide covers reactive state (`mo.state`), an advanced topic.++**You likely don't need reactive state**. UI elements already have built-in+state, their associated value, which you can access with their `value` attribute.+For example, `mo.ui.slider()` has a value that is its current position on an+interval, while `mo.ui.button()` has a value that can be configured to+count the number of times it has been clicked, or to toggle between `True` and+`False`. Additionally, interacting with UI elements bound to global variables+[automatically executes cells](guides/interactivity) that reference those+variables, letting you react to changes by just reading their+`value` attributes. This functional paradigm is the preferred way of+reacting to UI interactions in marimo. So if you+think you need to use `mo.state`, make sure to first read the [guide on+interactivity](/guides/interactivity.md). Chances are, the reactive execution+built into UI elements will suffice. (For example, [you don't need reactive+state to handle a button click](/recipes.md#working-with-buttons).)+++That said, here are some signs you might need `mo.state`:+- you need to maintain historical state related to a UI element that can't+ be computed from its built-in `value` (_e.g._, all values the user has+ ever input into a form)+- you need to synchronize two different UI elements (_e.g._, so that+ interacting with either one controls the other)+- you need to introduce cycles across cells++If one of these cases applies to you, then read on. `mo.state` lets you make+all kinds of interesting applications, but like mutable state in general,+it can complicate notebook development and has the potential to+introduce hard-to-find bugs.```You can build powerful, interactive notebooks and apps using just `mo.ui` and@@ -111,13 +137,6 @@ automatically runs all _other_ cells that reference any **global** variablesassigned to the state getter.```-<div align="center" style="margin-bottom: 2rem; margin-top:2rem">-<figure>-<img src="/_static/docs-state-update.gif"/>-</figure>-<figcaption>Calling `set_counter` in the second cell triggers the third cell (which refs `get_counter`) to run.</figcaption>-</div>-This rule has some important aspects:1. Only cells that read the state getter via a global variable will be run.@@ -177,7 +196,7 @@ mo.hstack([increment, decrement], justify="center")```pythonmo.md(f"""- The counter's current value is **{counter.value}**!+ The counter's current value is **{get_counter()}**!This cell runs automatically on button click, even though itdoesn't reference either button.