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: add additional docs coming from jupytext, papermill, streamlit (#2356)
Myles Scolnick897ab8ab2a83
diff --git a/docs/guides/coming_from/index.md b/docs/guides/coming_from/index.mdnew file mode 100644index 0000000000..87855e348e--- /dev/null+++ b/docs/guides/coming_from/index.md@@ -0,0 +1,22 @@+# Coming from other tools++```{eval-rst}+.. toctree::+ :maxdepth: 2+ :hidden:++ jupyter+ jupytext+ papermill+ streamlit+```++marimo is a **single tool** that replaces `jupyter`, `streamlit`, `jupytext`,+`ipywidgets`, `papermill`, and more.++| Guide | Description |+| :--------------- | :----------------------------------- |+| {doc}`jupyter` | Transitioning from Jupyter notebooks |+| {doc}`jupytext` | Transitioning from Jupytext |+| {doc}`papermill` | Transitioning from Papermill |+| {doc}`streamlit` | Transitioning from Streamlit |diff --git a/docs/guides/coming_from_jupyter.md b/docs/guides/coming_from/jupyter.mdsimilarity index 100%rename from docs/guides/coming_from_jupyter.mdrename to docs/guides/coming_from/jupyter.mddiff --git a/docs/guides/coming_from/jupytext.md b/docs/guides/coming_from/jupytext.mdnew file mode 100644index 0000000000..052cdb093e--- /dev/null+++ b/docs/guides/coming_from/jupytext.md@@ -0,0 +1,121 @@+# Coming from Jupytext++If you're familiar with Jupytext, you'll find that marimo offers similar+functionality for working with notebooks as Python files, but without the need+for additional setup or synchronization issues because marimo notebooks+are stored as `.py` files by default. However, Jupytext works with IPython+notebooks, whereas marimo works with marimo notebooks, which are not based+on IPython/Jupyter. Here's a comparison to help you transition smoothly.++## Notebook Format++:::{list-table}+:widths: 50 50++* * **Jupytext**+ * **marimo**+* * Jupytext uses comments or special markers to define cell types in notebooks.+ * Notebooks are pure Python (`.py`) files by default, using+ standard Python syntax, such as decorators and functions, to define cells.+:::++## Converting Jupyter notebooks++### From `.ipynb`++:::{list-table}+:widths: 50 50++* * **Jupytext**+ * **marimo**+* * `jupytext --to py notebook.ipynb`+ * `marimo convert notebook.ipynb > notebook.py`+:::++:::{admonition} From py:percent notebooks to marimo notebooks+:class: tip++If you have a Python file encoded in the [py:percent](https://jupytext.readthedocs.io/en/latest/#text-notebooks)+format, you can convert it to a marimo notebook in two steps:++```+jupytext --to notebook.ipynb percent_notebook.py+marimo convert notebook.ipynb > marimo_notebook.py+```++:::++### To `.ipynb`++:::{list-table}+:widths: 50 50++* * **Jupytext**+ * **marimo**+* * `jupytext --to notebook.ipynb notebook.py`+ * `marimo export ipynb notebook.py > notebook.ipynb`+:::++## Editing Notebooks++:::{list-table}+:widths: 50 50++* * **Jupytext**+ * **marimo**+* * Requires synchronization between `.ipynb` and `.py` files.+ * Edit marimo notebooks files directly in the marimo editor (`marimo edit+ notebook.py`), and changes are read from and written to the same file.+:::++## Executing Notebooks++:::{list-table}+:widths: 50 50++* * **Jupytext**+ * **marimo**+* * Use Jupyter to edit notebooks interactively, or Papermill to execute notebooks+ from the command line.+ * In addition to running notebooks interactively (`marimo+ notebook.py`), you can run notebooks as scripts (`python notebook.py`) or as apps+ (`marimo run notebook.py`), passing values to them with marimo's built-in support+ for [CLI args](/api/cli_args.md).+:::++## Version Control++:::{list-table}+:widths: 50 50++* * **Jupytext**+ * **marimo**+* * Jupyter notebooks are stored as JSON by default, making them difficult to+ meaningfully version with git. Use Jupytext to pair and synchronize+ jupyter notebooks with text representations for smaller git diffs.+ * Notebooks are already in `.py` format, making them git-friendly+ by default. Small changes to the notebook are guaranteed to yield small+ diffs.+:::++## Markdown and Code Cells++:::{list-table}+:widths: 50 50++* * **Jupytext**+ * **marimo**+* * Uses special markers or formats to distinguish cell types. Magical syntax is required.+ * Uses `mo.md("...")` for Markdown content, and interpolate Python values with `mo.md(f"...")`; no magical syntax.+:::++## Deployment++:::{list-table}+:widths: 50 50++* * **Jupytext**+ * **marimo**+* * Requires migrating to other libraries like Voila or Streamlit for deployment.+ * Can be deployed as interactive web apps with `marimo run`.+:::diff --git a/docs/guides/coming_from/papermill.md b/docs/guides/coming_from/papermill.mdnew file mode 100644index 0000000000..77ebdbed86--- /dev/null+++ b/docs/guides/coming_from/papermill.md@@ -0,0 +1,143 @@+# Coming from Papermill++marimo provides built-in support for parametrizing and executing marimo+notebooks. If you're familiar with Papermill, this guide will help you+understand how to achieve similar functionality using marimo's features.++## Parameterizing Notebooks++**Papermill**++Papermill allows you to parameterize Jupyter notebooks by defining a "parameters" cell+and injecting values at runtime.++**marimo**++marimo offers two main ways to parameterize notebooks:++1. **Command Line Arguments**:+ Use [`mo.cli_args`](/api/cli_args.md) to access command-line arguments passed to your notebook.++ ```python+ import marimo as mo++ # Access CLI args+ args = mo.cli_args()+ param1 = args.get("param1", "default_value")+ ```++ Run your notebook as a script with:++ ```bash+ python notebook.py -- --param1 value1+ ```++ Run your notebook as an app with:++ ```bash+ marimo run notebook.py -- --param1 value1+ ```++2. **Query Parameters**:+ For web apps, use `mo.query_params` to access URL query parameters.++ ```python+ import marimo as mo++ # Access query params+ params = mo.query_params()+ param1 = params.get("param1", "default_value")+ ```++ Access your app with:++ ```bash+ marimo run notebook.py+ ```++ Then visit:++ ```bash+ http://your-app-url/?param1=value1+ ```++## Executing Notebooks++**Papermill**++Papermill allows you to execute notebooks programmatically and pass parameters.++**marimo**++marimo notebooks are pure Python files, making them easy to execute+programmatically.++1. **Running a named cell**:++ After naming a cell in your file, you can run it using the+ [cell execution API](/api/cell.md#marimo.Cell.run).++ ```python+ from my_notebook import my_cell++ # last_expression is the visual output of the cell+ # definitions is a dictionary of the variables defined by the cell+ last_expression, definitions = my_cell.run()+ ```++ This API also allows for parametrizing the inputs to the cell; to learn more,+ make sure to checkout [the example](#marimo.Cell.run) in our API reference.++2. **Using subprocess**:++ ```python+ import subprocess++ subprocess.run(["python", "notebook.py", "--", "--param1", "value1"])+ ```++## Storing or Sharing Artifacts++**Papermill**++Papermill can store executed notebooks with output.++**marimo**++marimo offers several options for storing and sharing outputs:++1. **Export to HTML**:++ ```bash+ marimo export html notebook.py -o notebook.html -- -arg1 foo --arg2 bar+ ```++2. **Deploy as Web App**:++ ```bash+ marimo run notebook.py+ ```++3. **Auto-export HTML**:+ You can configure marimo to automatically export to HTML during the editing process.+ This is configured in the marimo application settings directly in the editor.+ This way, after changes are made to your notebook, an HTML snapshot is generated,+ and placed in a `.marimo/` directory in the same location as your notebook.++## Workflow Integration++**Papermill**++Papermill is often used in data pipelines and workflow systems.++**marimo**++marimo notebooks can be easily integrated into workflows:++1. **As Python Scripts**:+ marimo notebooks are Python files, so they can be executed directly in most workflow systems.+ See [our examples](https://github.com/marimo-team/marimo/tree/main/examples) for integrating with+ popular tools.++2. **Programmatic Execution**:+ Importing notebook as Python modules or executing via subprocess allows for chaining together multiple notebooks in a workflow.diff --git a/docs/guides/coming_from/streamlit.md b/docs/guides/coming_from/streamlit.mdnew file mode 100644index 0000000000..9c6f3c0ceb--- /dev/null+++ b/docs/guides/coming_from/streamlit.md@@ -0,0 +1,246 @@+# Coming from Streamlit++If you're familiar with Streamlit and looking to transition to marimo, read on.+The biggest difference between Streamlit and marimo is that+Streamlit can only be used for data apps, whereas marimo is a notebook-first+programming environment that makes it effortless to run notebooks as apps.++## Key Differences++1. **Notebook vs. App Framework**:+ - marimo is primarily a reactive notebook+ environment, while Streamlit is an app framework.+ - marimo notebooks can be run as apps -- often with better performance+ than streamlit apps -- but they're designed with a notebook-first approach.+ - When creating streamlit apps, it is common to first prototype them as Jupyter+ notebooks, then migrate and refactor them into streamlit apps. With marimo,+ every notebook is automatically an app; there's no migration step needed.++2. **Execution Model**:+ - marimo uses a reactive execution model that, on interaction or code+ change, runs the minimal subset of notebook code needed to keep your+ notebook up-to-date.+ - Streamlit reruns the entire script on each interaction, which frequently+ causes performance issues.++3. **File Format**:+ - marimo notebooks and Streamlit apps are pure Python files (.py).+ - marimo's structure allows for more fine-grained reactivity.+ - Unlike streamlit files, marimo files can be executed as Python scripts from the+ command-line, and can be imported and used as a module by other Python+ programs. For example, other programs can [reuse cells](#marimo.Cell.run) from+ a marimo notebook.++4. **UI Elements**:++- Both offer UI elements like sliders, text fields, and tables.+- In streamlit,+creating a UI element automatically outputs it to the display.+ -In marimo, the+creation of a UI element is separated from its display, meaning that you can+easily create custom layouts and higher-order elements, and even emit the same UI element twice.+- marimo support the [anywidget](https://anywidget.dev/) spec for custom UI components, letting+you reuse widgets that were originally developed for the Jupyter ecosystem,+- streamlit has its own system for creating custom components.++5. **Built-in Editor**:++- marimo includes a [built-in editor](/guides/editor_features/index.md) for notebooks, designed specifically+for working with data.+- Streamlit relies on external editors.+- Both approaches have their pros and cons.++6. **Working with data.**:++- marimo's notebook environment allows for iterative and interactive+ development and exploration, letting it serve as your daily driver for+ working with data. marimo even has native support for [SQL](/guides/sql.md).+- Streamlit is exclusively used for building standalone data apps.++## Common Streamlit Features in marimo++### 1. Displaying text++Streamlit:++```python+import streamlit as st+st.markdown(+ """+ # Greetings+ Hello world+ """+)+```++marimo:++```python+import marimo as mo+mo.md(+ """+ # Greetings+ Hello world+ """+)+```++### 2. Displaying Data++Streamlit:++```python+st.dataframe(df)+```++marimo:++```python+df # Last expression in a cell is automatically displayed+```++### 3. Input Widgets++Streamlit:++```python+age = st.slider("How old are you?", 0, 130, 25)+```++marimo:++```python+age = mo.ui.slider(label="How old are you?", start=0, stop=130, value=25)+mo.md(f"One more question: {age}") # marimo can achieve more advanced composition+```++### 4. Buttons++Streamlit:++```python+if st.button("Click me"):+ st.write("Button clicked!")+```++marimo:++```python+button = mo.ui.run_button("Click me")+```++```python+# In another cell+if button.value:+ mo.output.replace(mo.md("Button clicked!"))+```++```+# Or+mo.md("Button clicked!") if button.value else None+```++### 5. Layouts++Streamlit:++```python+col1, col2 = st.columns(2)+with col1:+ st.write("Column 1")+with col2:+ st.write("Column 2")+```++marimo:++```python+mo.hstack([+ mo.md("Column 1"),+ mo.md("Column 2")+])+```++### 6. Advanced Layouts (tabs, accordions)++Streamlit:++```python+with st.expander("Expand me"):+ st.write("Hello from the expander!")+```++marimo:++```python+mo.accordion({"Expand me": "Hello from the expander!"})+```++marimo's unique approach to composition allows for more flexible layouts with+unlimited nesting.++### 6. Plotting++Streamlit:++```python+import matplotlib.pyplot as plt++fig, ax = plt.subplots()+ax.plot([1, 2, 3, 4])+st.pyplot(fig)+```++marimo:++```python+import matplotlib.pyplot as plt++plt.plot([1, 2, 3, 4])+plt.gca() # Last expression is displayed+```++### 7. Caching++Streamlit:++```python[email protected]_data+def expensive_computation(args):+ # ...+```++marimo:++```python+def expensive_computation(args):+ # ...+```++### 8. Session State++Streamlit uses `st.session_state` for persisting data. In marimo, you can use+regular Python variables, as the notebook maintains consistent state for cells+that are not re-executed.++### 9. Running as an App++Streamlit:++```bash+streamlit run your_app.py+```++marimo:++```bash+marimo run your_notebook.py+```++## Key Concepts to Remember++1. In marimo, cells are automatically re-executed when their dependencies change. But only the affected cells are re-executed, making it far more efficient than a naively written streamlit program.+2. UI elements in marimo are typically assigned to variables and their values accessed via the `value` attribute.+3. marimo's `mo.md()` function is versatile and can include both text and UI elements with f-strings.+4. marimo's notebook-first approach allows it to be used for all kinds of data work, including exploratory data analysis, data engineering, machine learning experimentation and model training, library documentation and examples, and more.diff --git a/docs/guides/index.md b/docs/guides/index.mdindex 54fbee17b7..ba564391a7 100644--- a/docs/guides/index.md+++ b/docs/guides/index.md@@ -17,7 +17,7 @@editor_features/indexthemingbest_practices/index- coming_from_jupyter+ coming_from/indexintegrating_with_marimo/indexstatewasm@@ -42,7 +42,8 @@ These guides cover marimo's core concepts.| {doc}`editor_features/index` | View variables, cell dependencies, errors, and more || {doc}`theming` | Theming notebooks || {doc}`best_practices/index` | Notebook best practices |-| {doc}`coming_from_jupyter` | marimo for Jupyter users |+| {doc}`coming_from/jupyter` | marimo for Jupyter users |+| {doc}`coming_from/index` | Transitioning from other tools || {doc}`integrating_with_marimo/index` | Rich displays of objects, custom UI plugins || {doc}`state` | Mutable, reactive state || {doc}`wasm` | Create notebooks in our online playground! |