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
marimo for Jupyter users tutorial (#1104)
Akshay Agrawalf5ba30a04b3f
diff --git a/docs/guides/coming_from_jupyter.md b/docs/guides/coming_from_jupyter.mdnew file mode 100644index 0000000000..e9a26c1851--- /dev/null+++ b/docs/guides/coming_from_jupyter.md@@ -0,0 +1,8 @@+## Coming from Jupyter++If you're coming from Jupyter, this guide will help you get started+with marimo. Fun fact: the guide is itself a marimo notebook!+++<iframe src="https://marimo.app/l/z0aerp?embed=true" width=800 height=1200>+</iframe>diff --git a/docs/guides/index.md b/docs/guides/index.mdindex 2968eae7a5..63cc59cc3f 100644--- a/docs/guides/index.md+++ b/docs/guides/index.md@@ -8,6 +8,7 @@overviewreactivityinteractivity+ coming_from_jupyteroutputseditor_featuresdataframes@@ -24,23 +25,24 @@These guides cover marimo's core concepts.-| | |-| :--------------------- | :------------------------------------------------------ |-| {doc}`overview` | An overview of basic concepts |-| {doc}`reactivity` | How reactive execution works |-| {doc}`interactivity` | Using interactive UI elements |-| {doc}`outputs` | Markdown, plots, and other visual outputs |-| {doc}`editor_features` | View variables, cell dependencies, errors, and more |-| {doc}`dataframes` | Working with dataframes |-| {doc}`plotting` | Interactive and reactive plots |-| {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 |-| {doc}`performance` | Writing performant notebooks |-| {doc}`wasm` | Running notebooks in the browser (no backend required!) |-| {doc}`ai_completion` | Using AI to speed up your coding |-| {doc}`deploying/index` | Deploying marimo notebooks and apps |+| | |+| :------------------------- | :------------------------------------------------------ |+| {doc}`overview` | An overview of basic concepts |+| {doc}`reactivity` | How reactive execution works |+| {doc}`interactivity` | Using interactive UI elements |+| {doc}`coming_from_jupyter` | marimo for Jupyter users |+| {doc}`outputs` | Markdown, plots, and other visual outputs |+| {doc}`editor_features` | View variables, cell dependencies, errors, and more |+| {doc}`dataframes` | Working with dataframes |+| {doc}`plotting` | Interactive and reactive plots |+| {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 |+| {doc}`performance` | Writing performant notebooks |+| {doc}`wasm` | Running notebooks in the browser (no backend required!) |+| {doc}`ai_completion` | Using AI to speed up your coding |+| {doc}`deploying/index` | Deploying marimo notebooks and apps |```{admonition} Learn by doing!:class: tipdiff --git a/frontend/src/components/editor/output/Outputs.css b/frontend/src/components/editor/output/Outputs.cssindex 2aca7010dc..d1a11f7af0 100644--- a/frontend/src/components/editor/output/Outputs.css+++ b/frontend/src/components/editor/output/Outputs.css@@ -7,8 +7,9 @@width: 100%;}-/* Just applied to vertical layout */-.output-area .output {+/* No margin in edit mode, where we already have separation+ * between cells. */+.Cell.published .output-area .output {margin-top: 1rem;margin-bottom: 1rem;}diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.pyindex 952f7de5e1..54bf9aa8cc 100644--- a/marimo/_cli/cli.py+++ b/marimo/_cli/cli.py@@ -355,6 +355,7 @@ Recommended sequence:- plots- layout- fileformat+ - for-jupyter-users""")@click.option(@@ -392,6 +393,7 @@ Recommended sequence:"plots","layout","fileformat",+ "for-jupyter-users",]),)@@ -400,7 +402,14 @@ def tutorial(host: str,headless: bool,name: Literal[- "intro", "dataflow", "ui", "markdown", "plots", "layout", "fileformat"+ "intro",+ "dataflow",+ "ui",+ "markdown",+ "plots",+ "layout",+ "fileformat",+ "for-jupyter-users",],) -> None:from marimo._tutorials import (@@ -408,6 +417,7 @@ def tutorial(fileformat,intro,layout,+ marimo_for_jupyter_users,markdown,plots,ui,@@ -421,6 +431,7 @@ def tutorial("plots": plots,"layout": layout,"fileformat": fileformat,+ "for-jupyter-users": marimo_for_jupyter_users,}source = inspect.getsource(tutorials[name])d = tempfile.TemporaryDirectory()diff --git a/marimo/_tutorials/marimo_for_jupyter_users.py b/marimo/_tutorials/marimo_for_jupyter_users.pynew file mode 100644index 0000000000..580a4c16a7--- /dev/null+++ b/marimo/_tutorials/marimo_for_jupyter_users.py@@ -0,0 +1,315 @@+# Copyright 2024 Marimo. All rights reserved.+import marimo++__generated_with = "0.3.12"+app = marimo.App()++[email protected](hide_code=True)+def __(mo):+ mo.md(+ """# marimo for Jupyter users++ This notebook explains important differences between Jupyter and marimo. If you're+ familiar with Jupyter and are trying out marimo for the first time, read on!+ """+ )+ return++[email protected](hide_code=True)+def __(mo):+ mo.md(+ """+ ## Reactive execution++ The biggest difference between marimo and Jupyter is *reactive execution*.++ Try updating the value of x in the next cell, then run it.+ """+ )+ return+++def __():+ x = 0; x+ return x,+++def __(x):+ y = x + 1; y+ return y,++[email protected](hide_code=True)+def __(mo):+ mo.md(+ """+ marimo 'reacts' to the change in `x` and automatically recalculates `y`!++ **Explanation.** marimo reads the code in your cells and understands the+ dependences between them, based on the variables that each cell declares and+ references. When you execute one cell, marimo automatically executes all other+ cells that depend on it, not unlike a spreadsheet.++ In contrast, Jupyter requires you to manually run each cell.+ """+ )+ return++[email protected](hide_code=True)+def __(mo):+ mo.md(+ rf"""+ ### Why?++ Reactive execution frees you from the tedious task of manually re-running cells.++ It also ensures that your code and outputs remain in sync:++ - You don't have to worry about whether you forgot to re-run a cell.+ - When you delete a cell, its variables are automatically removed from+ program memory. Affected cells are automatically invalidated.++ This makes marimo notebooks as reproducible as regular Python scripts.+ """+ )+ return++[email protected](hide_code=True)+def __(mo):+ mo.md(+ """+ ## Interactive elements built-in++ marimo comes with a [large library of UI elements](https://docs.marimo.io/guides/interactivity.html) that are automatically+ synchronized with Python.+ """+ )+ return+++def __():+ import marimo as mo+ return mo,+++def __(mo):+ slider = mo.ui.slider(start=1, stop=10, label="$x$")+ slider+ return slider,+++def __(slider):+ slider.value+ return++[email protected](hide_code=True)+def __(mo):+ mo.md(+ rf"""+ **Explanation.** marimo is both a notebook and a library. Import `marimo as+ mo` and use `mo.ui` to get access to powerful UI elements.++ UI elements assigned to variables are automatically plugged into marimo's+ reactive execution model: interactions automatically trigger execution of+ cells that refer to them.++ In contrast, Jupyter's lack of reactivity makes IPyWidgets difficult to use.+ """+ )+ return++[email protected](hide_code=True)+def __(mo):+ mo.md(+ rf"""+ ## Shareable as apps++ marimo notebooks can be shared as read-only web apps: just serve it with++ ```marimo run your_notebook.py```++ at the command-line.++ Not every marimo notebook needs to be shared as an app, but marimo makes it+ seamless to do so if you want to. In this way, marimo works as a replacement+ for both Jupyter and Streamlit.+ """+ )+ return++[email protected](hide_code=True)+def __(mo):+ mo.md(+ rf"""+ ## Cell order++ In marimo, cells can be arranged in any order — marimo figures out the one true way to execute them based on variable declarations and references (in a ["topologically sorted"](https://en.wikipedia.org/wiki/Topological_sorting#:~:text=In%20computer%20science%2C%20a%20topological,before%20v%20in%20the%20ordering.) order)+ """+ )+ return+++def __(z):+ z.value+ return+++def __(mo):+ z = mo.ui.slider(1, 10, label="$z$"); z+ return z,++[email protected](hide_code=True)+def __(mo):+ mo.md(+ rf"""+ This lets you arrange your cells in the way that makes the most sense to you. For example, put helper functions and imports at the bottom of a notebook, like an appendix.++ In contrast, Jupyter notebooks implicitly assume a top-to-bottom execution order.+ """+ )+ return+++def __(mo):+ mo.md(+ rf"""+ ## Re-assigning variables++ marimo disallows variable re-assignment. Here is something commonly done in Jupyter notebooks that cannot be done in marimo:+ """+ )+ return+++def __():+ df = 0+ return df,+++def __():+ df = 1+ return df,+++def __(df):+ results = df.groupby(["my_column"]).sum()+ return results,++[email protected](hide_code=True)+def __(mo):+ mo.md(+ rf"""+ **Explanation.** `results` depends on `df`, but which value of `df` should it use? Reactivity makes it impossible to answer this question in a sensible way, so marimo disallows variable reassignment.++ If you run into this error, here are your options:++ 1. combine definitions into one cell+ 2. prefix variables with an underscore (`_df`) to make them local to the cell+ 3. wrap your code in functions, or give your variables more descriptive names+ """+ )+ return++[email protected](hide_code=True)+def __(mo):+ mo.md(+ rf"""+ ## Markdown++ marimo only has Python cells, but you can still write Markdown: `import marimo as mo` and use `mo.md` to write Markdown.+ """+ )+ return+++def __(mo, slider):+ mo.md(+ f"""+ The value of {slider} is {slider.value}.+ """+ )+ return++[email protected](hide_code=True)+def __(mo):+ mo.md(+ rf"""+ **Explanation.** By lifting Markdown into Python, marimo lets you construct+ dynamic Markdown parametrized by arbitrary Python elements. marimo knows+ how to render its own elements, and you can use `mo.as_html` to render other+ objects, like plots.++ _Tip: toggle a markdown view via `Cmd/Ctrl-Shift-M` in an empty cell._+ """+ )+ return++[email protected](hide_code=True)+def __(mo):+ mo.md(+ rf"""+ ## Notebook files++ Jupyter saves notebooks as JSON files, with outputs serialized in them. This is helpful as a record of your plots and other results, but makes notebooks difficult to version and reuse.++ ### marimo notebooks are Python scripts+ marimo notebooks are stored as pure Python scripts. This lets you version them with git, execute them with the command line, and re-use logic from one notebook in another.++ ### marimo notebooks do not store outputs+ marimo does _not_ save your outputs in the file; if you want them saved, make sure to save them to disk with Python, or export to HTML via the notebook menu.++ ### marimo notebooks are versionable with git++ marimo is designed so that small changes in your code yield small git diffs!+ """+ )+ return++[email protected](hide_code=True)+def __(mo):+ mo.md(+ rf"""+ ## Parting thoughts++ marimo is a **reinvention** of the Python notebook as a reproducible, interactive, and shareable Python program, instead of an error-prone scratchpad.++ We believe that the tools we use shape the way we think — better tools, for better minds. With marimo, we hope to provide the Python community with a better programming environment to do research and communicate it; to experiment with code and share it; to learn computational science and teach it.++ The marimo editor and library have many features not discussed here.+ Check out [our docs](https://docs.marimo.io/) to learn more!++ _This guide was adapted from [Pluto for Jupyter+ users](https://featured.plutojl.org/basic/pluto%20for%20jupyter%20users).+ We ❤️ Pluto.jl!_+ """+ )+ return+++if __name__ == "__main__":+ app.run()