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.

improve best practices (#381)

Akshay Agrawala14befdbbe35
diff --git a/docs/_static/css/custom.css b/docs/_static/css/custom.css
index 91c8a0a878..8769f649f7 100644
--- a/docs/_static/css/custom.css
+++ b/docs/_static/css/custom.css
@@ -82,3 +82,22 @@ figure img {
blockquote {
border-left-color: #1c7362;
}
+
+details.sd-dropdown summary {
+ padding-top: 0.25em;
+ padding-bottom: 0.25em;
+}
+
+details.sd-dropdown:not([open]) > .sd-card-header {
+ /* make border more visible */
+ border: 1px solid #d7d7d7;
+}
+
+details.sd-dropdown .sd-summary-title {
+ font-weight: normal;
+}
+
+details.sd-dropdown .sd-summary-down,
+details.sd-dropdown .sd-summary-up {
+ top: 0.3em;
+}
diff --git a/docs/conf.py b/docs/conf.py
index 20fac27021..6da58084fa 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -48,6 +48,10 @@ extensions = [
"sphinx_design",
]
+myst_enable_extensions = [
+ "colon_fence",
+]
+
autoclasstoc_sections = [
PublicMethods.key,
@@ -112,7 +116,7 @@ html_theme_options = {
"url": "https://discord.gg/JE7nhX6mD8",
"html": "",
"class": "fa-brands fa-solid fa-discord fa-2x",
- }
+ },
],
}
@@ -125,4 +129,4 @@ pygments_dark_style = "monokai"
def setup(app):
# https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#docstring-preprocessing
app.connect("autodoc-process-docstring", md2rst)
- app.add_directive('readmepart', ReadmePartDirective)
+ app.add_directive("readmepart", ReadmePartDirective)
diff --git a/docs/guides/best_practices.md b/docs/guides/best_practices.md
index 2f4cf1e774..be081e8d72 100644
--- a/docs/guides/best_practices.md
+++ b/docs/guides/best_practices.md
@@ -1,21 +1,18 @@
# Best practices
-The few constraints marimo puts on your notebooks are natural consequences of
-the fact that marimo programs are directed acyclic graphs. As long as you keep
-this fact in mind, you'll quickly adapt to the marimo way of writing notebooks
-and apps. You'll also find yourself writing better, more reproducible code.
-
-Follow these tips to stay on the marimo way:
+Here are best practices for writing marimo notebooks.
**Use global variables sparingly.** Keep the number of global variables in your
-program small to avoid name collisions.
+program small to avoid name collisions. If you have intermediate variables,
+encapsulate them in functions or prefix them with an underscore (`_tmp = ...`) to
+make them local to a cell.
**Use descriptive names.** Use descriptive variable names, especially for
global variables. This will help you minimize name clashes, and will also
result in better code.
-**Use functions.**
-Encapsulate logic into functions to avoid polluting the global namespace with
+**Use functions.** Encapsulate logic into functions to avoid polluting the
+global namespace with
temporary or intermediate variables, and to avoid code duplication.
**Use Python modules.** If your notebook gets too long, split complex logic
@@ -25,12 +22,48 @@ into helper Python modules and import them into your notebook.
only mutate an object in the cell that creates it, or create new objects
instead of mutating existing ones.
-**Write idempotent cells.** Write cells whose outputs and behavior are the same
+:::{dropdown} Example
+
+_Don't_ split up declarations and mutations over multiple cells. For example, _don't
+do this:_
+
+```python
+l = [1, 2, 3]
+```
+
+```python
+l.append(new_item())
+```
+
+Instead, _do_ **declare and mutate in the same cell**:
+
+```python
+l = [1, 2, 3]
+...
+l.append(new_item())
+```
+
+or, if working in multiple cells, **declare a new variable based on the old
+one**:
+
+```python
+l = [1, 2, 3]
+```
+
+```python
+extended_list = l + [new_item()]
+```
+:::
+
+
+
+**Write idempotent cells.**
+Write cells whose outputs and behavior are the same
when given the same inputs (references); such cells are called idempotent. This
will help you avoid bugs and cache expensive intermediate computations.
-**Cache computations with `@functools.cache`.** Use Python's builtin
-`functools` library to cache expensive computations.
+**Cache computations with `@functools.cache`.**
+Use Python's builtin `functools` library to cache expensive computations.
For example,
diff --git a/docs/guides/state.md b/docs/guides/state.md
index c7097b5d8f..afb145f877 100644
--- a/docs/guides/state.md
+++ b/docs/guides/state.md
@@ -1,4 +1,4 @@
-# Reactive State
+# Reactive state
```{admonition} Advanced topic!
:class: warning
diff --git a/docs/index.md b/docs/index.md
index 1aa591fa0d..3b78058033 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -68,8 +68,8 @@ notebooks</a>.
getting_started/index
guides/index
- api/index
recipes
+ api/index
faq
examples
integrations/index