Skip to content

Commit af38d82

Browse files
authored
feat: add pymdown blocks support to embed with marimo playground (#24)
* feat: add pymdown blocks support to embed with marimo playground * release docs
1 parent 9bd2143 commit af38d82

File tree

11 files changed

+697
-50
lines changed

11 files changed

+697
-50
lines changed

CONTRIBUTING.md

+6
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ Serve the documentation:
2222
```bash
2323
pixi run docs
2424
```
25+
26+
Lint and format:
27+
28+
```bash
29+
pixi run -e lint ruff format
30+
```

README.md

+57
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ pixi add mkdocs-marimo
2020

2121
Create reactive and interactive Python blocks in your markdown files using [marimo](https://github.com/marimo-team/marimo).
2222

23+
### Embedding inline HTML components
24+
25+
This uses code fences to embed marimo components as [marimo islands](https://docs.marimo.io/guides/exporting/?h=#embed-marimo-outputs-in-html-using-islands).
26+
2327
````markdown
2428
```python {marimo}
2529
import marimo as mo
@@ -33,6 +37,59 @@ mo.md(f"Hello, **{name.value or '__'}**!")
3337
```
3438
````
3539

40+
### Embedding the marimo playground
41+
42+
For an easy way to embed marimo notebooks or applications, we recommend embedding the marimo playground. This feature uses `pymdownx.blocks` to embed marimo notebooks in your MkDocs documentation, creating iframes that render the marimo playground.
43+
44+
````markdown
45+
/// marimo-embed
46+
height: 400px
47+
mode: run
48+
49+
```python
50+
@app.cell
51+
def __():
52+
import matplotlib.pyplot as plt
53+
import numpy as np
54+
55+
x = np.linspace(0, 10, 100)
56+
y = np.sin(x)
57+
58+
plt.figure(figsize=(8, 4))
59+
plt.plot(x, y)
60+
plt.title('Sine Wave')
61+
plt.xlabel('x')
62+
plt.ylabel('sin(x)')
63+
plt.grid(True)
64+
plt.gca()
65+
return
66+
///
67+
````
68+
69+
Available options for `marimo-embed`:
70+
71+
- `height`: Named sizes (`small`, `medium`, `large`, `xlarge`, `xxlarge`) or custom pixel values (e.g. `500px`) (default: medium)
72+
- `mode`: read, edit (default: read)
73+
- `app_width`: wide, full, compact (default: wide)
74+
75+
You can also embed marimo files directly:
76+
77+
````markdown
78+
/// marimo-embed-file
79+
filepath: path/to/your/file.py
80+
height: 400px
81+
mode: read
82+
show_source: true
83+
///
84+
````
85+
86+
Additional options for `marimo-embed-file`:
87+
88+
- `filepath`: path to the marimo file to embed (required)
89+
- `show_source`: true, false (default: true) - whether to show the source code below the embed
90+
91+
## Examples
92+
3693
Checkout the [documentation](https://marimo-team.github.io/mkdocs-marimo) for more examples.
3794

3895
## Contributions welcome

docs/getting-started/blocks.md

+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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` |

docs/getting-started/configuration.md

+22
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,25 @@ mo.md(f"Change the slider value: {slider}")
6060
This will render a marimo cell that displays the source code and shows the output, but is not interactive in the browser (overriding the global `is_reactive` setting).
6161

6262
Remember that options specified in individual code fences will override the global settings for that specific cell.
63+
64+
## Releasing (for maintainers)
65+
66+
To release a new version:
67+
68+
1. Bump the version:
69+
70+
```bash
71+
hatch version patch # or minor/major
72+
git add pyproject.toml
73+
git commit -m "chore: bump version to $(hatch version --no-color)"
74+
git push origin main
75+
```
76+
77+
2. Create and push a tag:
78+
79+
```bash
80+
git tag $(hatch version --no-color)
81+
git push origin $(hatch version --no-color)
82+
```
83+
84+
This will trigger the GitHub Actions workflow to build and publish the package to PyPI.

0 commit comments

Comments
 (0)