|
| 1 | +# Embedding marimo Notebooks with the Playground |
| 2 | + |
| 3 | +This guide covers the second approach to using marimo in your documentation: embedding full marimo notebooks using the marimo playground, [like in our own documentation](https://docs.marimo.io/api/inputs/button/). This approach is ideal when you want to: |
| 4 | + |
| 5 | +- Show complete, multi-cell notebooks |
| 6 | +- Allow users to edit and experiment with the code |
| 7 | +- Embed existing .py notebooks |
| 8 | + |
| 9 | +If you're looking for simpler, inline code examples, check out the [Quick Start](quick-start.md) guide's section on inline code. |
| 10 | + |
| 11 | +This feature uses `pymdownx.blocks` to embed marimo notebooks in your MkDocs documentation, creating iframes that render the marimo playground. |
| 12 | + |
| 13 | +## Setup |
| 14 | + |
| 15 | +To use the marimo playground, you need to install the `pymdown-extensions` package. |
| 16 | + |
| 17 | +```bash |
| 18 | +pip install mkdocs-marimo pymdown-extensions |
| 19 | +``` |
| 20 | + |
| 21 | +## Basic Example |
| 22 | + |
| 23 | +Here's a simple example of embedding a marimo notebook using blocks: |
| 24 | + |
| 25 | +````markdown |
| 26 | +/// marimo-embed |
| 27 | + height: 400px |
| 28 | + mode: read |
| 29 | + app_width: wide |
| 30 | + |
| 31 | +```python |
| 32 | +@app.cell |
| 33 | +def __(): |
| 34 | + import marimo as mo |
| 35 | + |
| 36 | + name = mo.ui.text(placeholder="Enter your name", debounce=False) |
| 37 | + name |
| 38 | + return |
| 39 | + |
| 40 | +@app.cell |
| 41 | +def __(): |
| 42 | + mo.md(f"Hello, **{name.value or '__'}**!") |
| 43 | + return |
| 44 | +``` |
| 45 | + |
| 46 | +/// |
| 47 | +```` |
| 48 | + |
| 49 | +/// marimo-embed |
| 50 | + height: 400px |
| 51 | + mode: read |
| 52 | + app_width: wide |
| 53 | + |
| 54 | +```python |
| 55 | +@app.cell |
| 56 | +def __(): |
| 57 | + import marimo as mo |
| 58 | + |
| 59 | + name = mo.ui.text(placeholder="Enter your name", debounce=False) |
| 60 | + name |
| 61 | + return |
| 62 | + |
| 63 | +@app.cell |
| 64 | +def __(): |
| 65 | + mo.md(f"Hello, **{name.value or '__'}**!") |
| 66 | + return |
| 67 | +``` |
| 68 | + |
| 69 | +/// |
| 70 | + |
| 71 | +## Interactive Plot Example |
| 72 | + |
| 73 | +Here's an example with an interactive plot: |
| 74 | + |
| 75 | +````markdown |
| 76 | +/// marimo-embed |
| 77 | + height: 800px |
| 78 | + mode: read |
| 79 | + app_width: wide |
| 80 | + |
| 81 | +```python |
| 82 | +@app.cell |
| 83 | +def __(): |
| 84 | + import marimo as mo |
| 85 | + import numpy as np |
| 86 | + import matplotlib.pyplot as plt |
| 87 | + |
| 88 | + # Create interactive sliders |
| 89 | + freq = mo.ui.slider(1, 10, value=2, label="Frequency") |
| 90 | + amp = mo.ui.slider(0.1, 2, value=1, label="Amplitude") |
| 91 | + |
| 92 | + mo.hstack([freq, amp]) |
| 93 | + return |
| 94 | + |
| 95 | +@app.cell |
| 96 | +def __(): |
| 97 | + # Plot the sine wave |
| 98 | + x = np.linspace(0, 10, 1000) |
| 99 | + y = amp.value * np.sin(freq.value * x) |
| 100 | + |
| 101 | + plt.figure(figsize=(10, 6)) |
| 102 | + plt.plot(x, y) |
| 103 | + plt.title('Interactive Sine Wave') |
| 104 | + plt.xlabel('x') |
| 105 | + plt.ylabel('y') |
| 106 | + plt.grid(True) |
| 107 | + plt.gca() |
| 108 | + return |
| 109 | +``` |
| 110 | + |
| 111 | +/// |
| 112 | +```` |
| 113 | + |
| 114 | +/// marimo-embed |
| 115 | + height: 800px |
| 116 | + mode: read |
| 117 | + app_width: wide |
| 118 | + |
| 119 | +```python |
| 120 | +@app.cell |
| 121 | +def __(): |
| 122 | + import marimo as mo |
| 123 | + import numpy as np |
| 124 | + import matplotlib.pyplot as plt |
| 125 | + |
| 126 | + # Create interactive sliders |
| 127 | + freq = mo.ui.slider(1, 10, value=2, label="Frequency") |
| 128 | + amp = mo.ui.slider(0.1, 2, value=1, label="Amplitude") |
| 129 | + |
| 130 | + mo.hstack([freq, amp]) |
| 131 | + return |
| 132 | + |
| 133 | +@app.cell |
| 134 | +def __(): |
| 135 | + # Plot the sine wave |
| 136 | + x = np.linspace(0, 10, 1000) |
| 137 | + y = amp.value * np.sin(freq.value * x) |
| 138 | + |
| 139 | + plt.figure(figsize=(10, 6)) |
| 140 | + plt.plot(x, y) |
| 141 | + plt.title('Interactive Sine Wave') |
| 142 | + plt.xlabel('x') |
| 143 | + plt.ylabel('y') |
| 144 | + plt.grid(True) |
| 145 | + plt.gca() |
| 146 | + return |
| 147 | +``` |
| 148 | + |
| 149 | +/// |
| 150 | + |
| 151 | +## Example with Hidden Code |
| 152 | + |
| 153 | +Here's an example that hides the code: |
| 154 | + |
| 155 | +/// marimo-embed |
| 156 | + height: 400px |
| 157 | + mode: read |
| 158 | + app_width: wide |
| 159 | + include_code: false |
| 160 | + |
| 161 | +```python |
| 162 | +@app.cell |
| 163 | +def __(): |
| 164 | + import marimo as mo |
| 165 | + import numpy as np |
| 166 | + import matplotlib.pyplot as plt |
| 167 | + |
| 168 | + x = np.linspace(0, 10, 100) |
| 169 | + y = np.sin(x) |
| 170 | + plt.plot(x, y) |
| 171 | + plt.title('Simple Sine Wave') |
| 172 | + plt.gca() |
| 173 | + return |
| 174 | +``` |
| 175 | + |
| 176 | +/// |
| 177 | + |
| 178 | +## Configuration Options |
| 179 | + |
| 180 | +### marimo-embed |
| 181 | + |
| 182 | +| Option | Description | Values | |
| 183 | +| --- | --- | --- | |
| 184 | +| `height` | Controls the height of the embed | - Named sizes: `small` (300px), `medium` (400px), `large` (600px),<br> `xlarge` (800px), `xxlarge` (1000px)<br>- Custom size: Any pixel value (e.g. `500px`) | |
| 185 | +| `mode` | Controls the interaction mode | - `read`: Read-only view (default)<br>- `edit`: Allows editing the code | |
| 186 | +| `app_width` | Controls the width of the marimo app | - `wide`: Standard width (default)<br>- `full`: Full width<br>- `compact`: Narrow width | |
| 187 | +| `include_code` | Controls whether code is included in the embed | - `true`: Show code (default)<br>- `false`: Hide code | |
| 188 | + |
| 189 | +### marimo-embed-file |
| 190 | + |
| 191 | +The `marimo-embed-file` block is used to embed existing marimo files: |
| 192 | + |
| 193 | +/// marimo-embed-file |
| 194 | + filepath: getting-started/inlined.py |
| 195 | + height: 600px |
| 196 | + mode: read |
| 197 | + show_source: true |
| 198 | +/// |
| 199 | + |
| 200 | +| Option | Description | Default | |
| 201 | +| --- | --- | --- | |
| 202 | +| `filepath` | Path to the marimo file to embed | Required | |
| 203 | +| `show_source` | Whether to show the source code below the embed | `true` | |
| 204 | +| `include_code` | Controls whether code is included in the embed | `true` | |
0 commit comments