Skip to content

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.

docs: understanding errors (#4397)

Akshay Agrawal31aae8040ed8
diff --git a/docs/_static/docs_cycles_error.png b/docs/_static/docs_cycles_error.png
new file mode 100644
index 0000000000..5deece3e3a
Binary files /dev/null and b/docs/_static/docs_cycles_error.png differ
diff --git a/docs/_static/docs_import_star_error.png b/docs/_static/docs_import_star_error.png
new file mode 100644
index 0000000000..33437a5eb6
Binary files /dev/null and b/docs/_static/docs_import_star_error.png differ
diff --git a/docs/_static/docs_redefines_variables_error.png b/docs/_static/docs_redefines_variables_error.png
new file mode 100644
index 0000000000..a11283c656
Binary files /dev/null and b/docs/_static/docs_redefines_variables_error.png differ
diff --git a/docs/guides/coming_from/jupyter.md b/docs/guides/coming_from/jupyter.md
index 1f8dc2265f..db9b9d2b9f 100644
--- a/docs/guides/coming_from/jupyter.md
+++ b/docs/guides/coming_from/jupyter.md
@@ -3,14 +3,14 @@
If you're coming from Jupyter, here are a few tips to help you adapt to marimo
notebooks.
-## Adapting to marimo's execution model
+## How marimo runs cells
-The biggest difference between marimo and Jupyter is the execution model.
+The biggest difference between marimo and Jupyter is the [execution model](../reactivity.md).
A **Jupyter** notebook is a **REPL**: you execute blocks of code one at a time,
and Jupyter has no understanding of how different blocks are related to each
other. As a result a Jupyter notebook can easily
-accumulate "hidden state" (and hidden bugs) --- you might accidentally execute
+accumulate **"hidden state"** (and hidden bugs) --- you might accidentally execute
cells out of order, or you might run (or delete) a cell but forget to re-run
cells that depended on its variables. Because of this, Jupyter notebooks
suffer from a [reproducibility crisis](../../faq.md#faq-problems), with over
@@ -76,7 +76,7 @@ def __():
For more tips on adapting to marimo's execution model, see our guide
on [working with expensive notebooks](../expensive_notebooks.md).
-## Adapting to marimo's restriction on redefining variables
+## Redefining variables
marimo "compiles" your notebook cells into a directed graph on cells,
linked by variable declarations and references, reusing this graph to
@@ -125,7 +125,7 @@ augmented_df = df
augmented_df["another_column"] = [3, 4]
```
-## Adapting to marimo's file format
+## marimo's file format
marimo stores notebooks as Python, not JSON. This lets you version notebooks
with git, [execute them as scripts](../scripts.md), and import named
@@ -133,8 +133,8 @@ cells into other Python files. However, it does mean that your notebook outputs
(e.g., plots) are not stored in the file.
If you'd like to keep a visual record of your notebook work, [enable
-the "Auto-download as HTML" setting](../configuration/index.md), which will
-periodically snapshot your notebook as HTML to a `__marimo__` folder in the
+the "Auto-download as HTML/IPYNB" setting](../configuration/index.md), which will
+periodically snapshot your notebook as HTML or IPYNB to a `__marimo__` folder in the
notebook directory.
### Converting Jupyter notebooks to marimo notebooks
@@ -145,7 +145,18 @@ Convert Jupyter notebooks to marimo notebooks at the command-line:
marimo convert your_notebook.ipynb -o your_notebook.py
```
-## Adapting to the absence of magic commands
+### Exporting marimo notebooks to Jupyter notebooks
+
+Export to an `ipynb` file with
+
+```bash
+marimo export ipynb notebook.py -o notebook.ipynb
+```
+
+Note that some marimo library functions, including UI elements,
+won't work in Jupyter notebooks.
+
+## Magic commands
Because marimo notebooks are just Python (improving maintainability), marimo
doesn't support IPython magic commands or `!`-prefixed console commands. Here
diff --git a/docs/guides/index.md b/docs/guides/index.md
index 42773f11f1..9660502ce8 100644
--- a/docs/guides/index.md
+++ b/docs/guides/index.md
@@ -14,6 +14,7 @@ These guides cover marimo's core concepts.
| [Visualizing outputs](outputs.md) | Creating markdown, plots, and other visual outputs |
| [Migrating from Jupyter](coming_from/jupyter.md) | Tips for transitioning from Jupyter |
| [Expensive notebooks](expensive_notebooks.md) | Tips for working with expensive notebooks |
+| [Understanding errors](understanding_errors/index.md) | Understanding marimo's constraints on notebook code |
| [Working with data](working_with_data/index.md) | Using SQL cells, no-code dataframe, and reactive plots |
| [Package reproducibility](package_reproducibility.md) | Making notebooks reproducible down to the packages |
| [Editor features](editor_features/index.md) | View variables, dataframe schemas, docstrings, and more |
diff --git a/docs/guides/understanding_errors/cycles.md b/docs/guides/understanding_errors/cycles.md
new file mode 100644
index 0000000000..65dbd62dec
--- /dev/null
+++ b/docs/guides/understanding_errors/cycles.md
@@ -0,0 +1,49 @@
+# Cycles
+
+You're probably on this page because you just saw an error like this one:
+
+<div align="center">
+<figure>
+<img src="/_static/docs_cycle_error.png" width="700px"/>
+</figure>
+</div>
+
+marimo raises this error when a cell is involved in a cycle on variables. In
+this example, the first cell declares `a` and reads `b`, while the second cell
+declares `b` and reads `a`.
+
+## Why can't I have cycles?
+
+marimo parses your cells to understand the order in which they run:
+run a cell, and cells that refer to its defined variables need to run afterward.
+With a cycle, the execution order becomes ambiguous, while also introducing
+an infinite loop.
+
+??? question **What do I get in return?**
+
+ By accepting this constraint on variables, marimo makes your notebooks:
+
+ - **reproducible**, with a well-defined execution order, no hidden state, and no hidden bugs;
+ - **executable** as a script;
+ - **interactive** with UI elements that work without callbacks;
+ - **shareable as a web app**, with far better performance that streamlit.
+
+ As a bonus, you'll find that you end up with cleaner, reusable code.
+
+**How do I read the error message?** In the error message, each line says which
+cell defines a variable and which cell reads a variable. For example, `cell-0
+-> a` means `cell-0` defines `a`, and `a -> cell-1` means `cell-1` reads `a`.
+Similarly, `cell-1 -> b` means `cell-1` defines `b`, and `b -> cell-0` means
+`cell-0` reads `b`. This creates the cycle from `cell-0 -> cell-1 -> cell-0`.
+
+## How do I fix this error?
+
+Cycles usually indicate that your notebook has a bug. Still, you can
+fix the error by merging the cells involved in the cycle into a single cell:
+
+```python
+a = 0
+b = 1
+a = b
+b = a
+```
diff --git a/docs/guides/understanding_errors/import_star.md b/docs/guides/understanding_errors/import_star.md
new file mode 100644
index 0000000000..fab0876b5e
--- /dev/null
+++ b/docs/guides/understanding_errors/import_star.md
@@ -0,0 +1,56 @@
+# Star imports
+
+You're probably on this page because you just saw an error like this one:
+
+<div align="center">
+<figure>
+<img src="/_static/docs_import_star_error.png" width="700px"/>
+</figure>
+</div>
+
+marimo raises this error you attempt to use `import *`.
+
+In this example, `x` was already defined, and the subsequent cell raised an
+error when we tried to run it. In your case, perhaps your variable is a loop
+variable (`i`), a dataframe (`df`), or a plot (`fig`, `ax`).
+
+## Why can't I use `*` imports?
+
+Star imports are incompatible with marimo's git-friendly file format and reproducible reactive execution:
+
+- marimo's Python file format stores code in functions, so notebooks can be imported as regular Python modules without executing all their code. But Python disallows `import *` everywhere except at the top-level of a module.
+- Star imports would also silently add names to globals, which would be
+incompatible with [reactive execution](../reactivity.md).
+
+Even Python's [official style guide](https://peps.python.org/pep-0008/) discourages the use of `import *`, writing:
+
+> Wildcard imports (from <module> import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.
+
+**What do I get in return?**
+By accepting this constraint on imports, marimo makes your notebooks:
+
+- **reproducible**, with a well-defined execution order, no hidden state, and no hidden bugs;
+- **executable** as a script;
+- **interactive** with UI elements that work without callbacks;
+- **shareable as a web app**, with far better performance that streamlit.
+
+As a bonus, you'll find that you end up with cleaner, reusable code.
+
+## How do I fix this error?
+
+Fixing this error is simple: just import the module, and use `.` notation
+to access its members.
+
+```python
+import math
+
+math.pi
+```
+
+instead of
+
+```
+from math import *
+
+pi
+```
diff --git a/docs/guides/understanding_errors/index.md b/docs/guides/understanding_errors/index.md
new file mode 100644
index 0000000000..0aa38a3d03
--- /dev/null
+++ b/docs/guides/understanding_errors/index.md
@@ -0,0 +1,30 @@
+# Understanding errors
+
+marimo imposes a few constraints on your notebook code:
+
+* no multiply defined variables: each variable can be defined in only one cell
+* no cycles: if one cell declares variable `a` and reads `b`, then another cannot declare `b` and read `a`.
+* no `import *`: importing all symbols from a library is not allowed
+
+!!! question "Why these constraints?"
+ These constraints let marimo work its magic, making your notebooks:
+
+ - **reproducible**, with a well-defined execution order, no hidden state, and no hidden bugs;
+ - **executable** as a script;
+ - **interactive** with UI elements that work without callbacks;
+ - **shareable as a web app**, with far better performance that streamlit.
+
+ As a bonus, you'll find that you end up with cleaner, reusable code.
+
+When a cell violates any of these constraints, marimo doesn't run it and instead
+reports an error. In these guides, we explain these errors and provide tips for
+how to work around them.
+
+These errors might be surprising at first, but spend just a
+bit of time with marimo and adhering to these constraints will become second nature — and you'll get used to writing error-free code by default.
+
+| Guide | Description |
+|-------|-------------|
+| [Multiple definitions](multiple_definitions.md) | How to deal with variables defined in multiple cells |
+| [Import `*`](import_star.md) | Why you can't use `import *` |
+| [Cycles](cycles.md) | How to resolve cycle errors |
diff --git a/docs/guides/understanding_errors/multiple_definitions.md b/docs/guides/understanding_errors/multiple_definitions.md
new file mode 100644
index 0000000000..7c14fa09a7
--- /dev/null
+++ b/docs/guides/understanding_errors/multiple_definitions.md
@@ -0,0 +1,94 @@
+# Multiple definitions
+
+You're probably on this page because you just saw an error like this one:
+
+<div align="center">
+<figure>
+<img src="/_static/docs_redefines_variables_error.png" width="700px"/>
+</figure>
+</div>
+
+marimo raises this error when a variable is defined in multiple cells. In
+this example, `x` was already defined, and the subsequent cell raised
+an error when we tried to run it. In your case, perhaps your variable
+is a loop variable (`i`), a dataframe (`df`), or a plot (`fig`, `ax`).
+
+## Why can't I redefine variables?
+
+**Understanding the error message.** The error message tells you which other
+cells defined the multiply defined variable. You can click on the cell name,
+and marimo will highlight it.
+
+**Why can't I redefine variables?** marimo guarantees that the code on the page
+matches the outputs you see by determining a deterministic execution order on
+cells; when one cell runs, marimo knows which others should run. But if two
+cells defined `x`, and a third showed `x`, the output of the third cell would
+be ambiguous, depending on which of the defining cells ran first (should it be
+`0` or `1`?). That's a problem because it creates [hidden state and hidden
+bugs](../coming_from/jupyter.md), and it's part of the reason why [over 96% of
+Jupyter notebooks on GitHub aren't reproducible](https://leomurta.github.io/papers/pimentel2019a.pdf).
+
+**What do I get in return?**
+By accepting this constraint on variables, marimo makes your notebooks:
+
+- **reproducible**, with a well-defined execution order, no hidden state, and no hidden bugs;
+- **executable** as a script;
+- **interactive** with UI elements that work without callbacks;
+- **shareable as a web app**, with far better performance that streamlit.
+
+As a bonus, you'll find that you end up with cleaner, reusable code.
+
+## How do I fix this error?
+
+You have a few options.
+
+### Use local variables
+
+In marimo, variables prefixed with an underscore (`_x` or `_i`) are made local
+to a cell, and can be redefined across multiple cells.
+
+```python
+for _i in range(10):
+ ...
+```
+
+Use this in a pinch, but prefer encapsulating code in functions.
+
+### Encapsulate code in a function
+
+Python provides local scope through functions: if the variable that was
+redefined is meant to be a temporary variable, then you can make it local to
+the cell by encapsulating the code in a function. If any of the cell's
+variables are not meant to be local, or are outputs meant to be displayed, just
+return them from the function.
+
+In general, we recommend writing modular code with meaningful functions. But,
+in a pinch, just declare an anonymous function like this one to get a "local scope":
+
+
+```python
+def _():
+ import matplotlib.pyplot as plt
+ fig, ax = plt.subplots()
+ ax.plot([1, 2])
+ return ax
+
+_()
+```
+
+That's what clicking on the "Fix: Wrap in a function" button does. Note the function
+`_()` is local to the cell.
+
+
+### Merge cells
+
+Often you can simply merge the cells that define the same variable into a single cell.
+To incrementally show outputs in the cell, use [`mo.output.append`][marimo.output.append]
+or `print()`.
+
+### Chain dataframe methods
+
+When working with dataframes, instead of splitting up operations across
+multiple cells, chain operations in a single cell. This is especially ergonomic
+when using [Polars](https://docs.pola.rs/), Daft, or other modern dataframe
+libraries that support lazy execution.
diff --git a/mkdocs.yml b/mkdocs.yml
index 78c869933b..85caeda362 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -87,6 +87,11 @@ nav:
- Visualize outputs: guides/outputs.md
- Migrate from Jupyter: guides/coming_from/jupyter.md
- Expensive notebooks: guides/expensive_notebooks.md
+ - Understanding errors:
+ - Understanding errors: guides/understanding_errors/index.md
+ - Multiple definitions: guides/understanding_errors/multiple_definitions.md
+ - Import star: guides/understanding_errors/import_star.md
+ - Cycles: guides/understanding_errors/cycles.md
- SQL, dataframes, and plots:
- SQL, dataframes, and plots: guides/working_with_data/index.md
- DataFrames: guides/working_with_data/dataframes.md