Skip to content

Allow configuring number of lines in pyodide editors. #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions docs/usage/pyodide.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,23 @@ for theme in themes:
)
)
```

## Editor Height Configuration

You can customize the height of the Pyodide editor using several options:

### Lines (Recommended)

The simplest way to control the editor height is with the `lines` parameter, which sets the number of visible lines in the editor. The editor will automatically adjust its height to fit the specified number of lines:

```pyodide lines=3 assets="no"
# This editor shows 10 lines by default
def fibonacci(n):
"""Generate Fibonacci sequence up to n"""
a, b = 0, 1
while a < n:
yield a
a, b = b, a + b

print(list(fibonacci(100)))
```
4 changes: 4 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ plugins:
inputs:
- index.md
- reference/**.md
sections:
- overview
- usage
- api
- git-revision-date-localized:
enabled: !ENV [DEPLOY, false]
enable_creation_date: true
Expand Down
20 changes: 17 additions & 3 deletions src/markdown_exec/_internal/formatters/pyodide.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@

<script>
document.addEventListener('DOMContentLoaded', (event) => {
setupPyodide('%(id_prefix)s', install=%(install)s, themeLight='%(theme_light)s', themeDark='%(theme_dark)s', session='%(session)s');
setupPyodide(
'%(id_prefix)s',
install=%(install)s,
themeLight='%(theme_light)s',
themeDark='%(theme_dark)s',
session='%(session)s',
minLines=%(min_lines)s,
maxLines=%(max_lines)s
);
});
</script>
"""
Expand All @@ -56,7 +64,11 @@ def _format_pyodide(code: str, md: Markdown, session: str, extra: dict, **option
if "," not in theme:
theme = f"{theme},{theme}"
theme_light, theme_dark = theme.split(",")


# Get line-based configuration
min_lines = int(extra.pop("min_lines", "3"))
max_lines = int(extra.pop("max_lines", "20"))

data = {
"id_prefix": f"exec-{_counter}--",
"initial_code": code,
Expand All @@ -66,8 +78,10 @@ def _format_pyodide(code: str, md: Markdown, session: str, extra: dict, **option
"session": session or "default",
"play_emoji": _play_emoji,
"clear_emoji": _clear_emoji,
"min_lines": min_lines,
"max_lines": max_lines,
}
rendered = _template % data
if exclude_assets:
return rendered
return _assets.format(version=version) + rendered
return _assets.format(version=version) + rendered
Loading