Skip to content

Commit 12b800c

Browse files
authored
1 parent 3cb90a9 commit 12b800c

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

docs/usage/pyodide.md

+14
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,20 @@ print("hello")
135135
```
136136
````
137137

138+
## Editor height
139+
140+
By default, the editor has a fixed minimum height. You can specify the initial number of lines to display using the `lines` option:
141+
142+
````md
143+
```pyodide lines="3"
144+
print("hello")
145+
print("world")
146+
print("!")
147+
```
148+
````
149+
150+
This is useful when you want to save vertical space for small code examples or need more space for larger ones.
151+
138152
See all previews below.
139153

140154
```python exec="1"

src/markdown_exec/formatters/pyodide.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<div class="pyodide-editor-bar">
2727
<span class="pyodide-bar-item">Editor (session: %(session)s)</span><span id="%(id_prefix)srun" title="Run: press Ctrl-Enter" class="pyodide-bar-item pyodide-clickable"><span class="twemoji">%(play_emoji)s</span> Run</span>
2828
</div>
29-
<div><pre id="%(id_prefix)seditor" class="pyodide-editor">%(initial_code)s</pre></div>
29+
<div><pre id="%(id_prefix)seditor" class="pyodide-editor" %(lines_attr)s style="%(lines_style)s">%(initial_code)s</pre></div>
3030
<div class="pyodide-editor-bar">
3131
<span class="pyodide-bar-item">Output</span><span id="%(id_prefix)sclear" class="pyodide-bar-item pyodide-clickable"><span class="twemoji">%(clear_emoji)s</span> Clear</span>
3232
</div>
@@ -54,6 +54,18 @@ def _format_pyodide(code: str, md: Markdown, session: str, extra: dict, **option
5454
if "," not in theme:
5555
theme = f"{theme},{theme}"
5656
theme_light, theme_dark = theme.split(",")
57+
58+
# Handle lines option
59+
lines = extra.pop("lines", "")
60+
lines_attr = ""
61+
lines_style = ""
62+
if lines and lines.isdigit():
63+
line_count = int(lines)
64+
# Calculate approximate height based on line count (assuming ~20px per line)
65+
height = max(line_count * 20, 200) # Minimum 200px as in CSS
66+
lines_attr = f'data-lines="{line_count}"'
67+
lines_style = f'--pyodide-editor-height: {height}px;'
68+
5769
data = {
5870
"id_prefix": f"exec-{_counter}--",
5971
"initial_code": code,
@@ -63,6 +75,8 @@ def _format_pyodide(code: str, md: Markdown, session: str, extra: dict, **option
6375
"session": session or "default",
6476
"play_emoji": play_emoji,
6577
"clear_emoji": clear_emoji,
78+
"lines_attr": lines_attr,
79+
"lines_style": lines_style,
6680
}
6781
rendered = template % data
6882
if exclude_assets:

src/markdown_exec/pyodide.css

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ html[data-theme="dark"] {
1818
font-size: .85em;
1919
}
2020

21+
.pyodide-editor[data-lines] {
22+
min-height: var(--pyodide-editor-height);
23+
}
24+
2125
.pyodide-editor-bar {
2226
color: var(--md-primary-bg-color);
2327
background-color: var(--md-primary-fg-color);

0 commit comments

Comments
 (0)