The standard Skulpt labs use a plain <textarea> for code entry — intentionally
minimal so that chapters 1–18 stay simple to deploy and share.
For longer programs (20+ lines), a proper code editor with line numbers and
syntax highlighting makes a meaningful difference: when Skulpt reports
NameError on line 27, students can jump directly to the problem instead
of counting lines manually.
This page is a self-contained proof-of-concept.
It loads CodeMirror 5 from a CDN and wires it to Skulpt — all inside this
single Markdown file, with no changes to shared CSS, JavaScript, or mkdocs.yml.
The program below is 37 lines.
Without line numbers, NameError on line 27 sends students scanning.
With line numbers, they find the problem immediately.
Click Run to grow the tree, then try editing depth (currently 6)
or the trunk length (currently 80).
Paste these five blocks into any Markdown page to get a CodeMirror + Skulpt lab.
Replace the Python code in _cmLines with your own program.
This page is a standalone proof-of-concept: runCmLab and resetCmLab are
defined inline and read from cmEditor.getValue(). The production labs
throughout the textbook use the shared js/codemirror-lab.js instead, which
handles multiple labs per page via a suffix registry (initCmLab) and adds
a Run ⇄ Pause button: while a program runs, clicking Pause freezes the
turtle drawing (handy for discussion or screenshots) and Resume continues
it. If the code is edited while paused, the button changes to Run New
Code and starts a fresh run with the new code instead of resuming the old
one. Reset stops a running program. See
The Run ⇄ Pause Button for the full behavior table
and the design rationale. The minimal inline version below does not include
the pause feature.
Students will read error messages with line numbers
The standard Skulpt labs in chapters 1–18 stay with <textarea> — intentionally.
CodeMirror is worth the extra setup for longer demonstrations where navigating to
a specific line number is part of the lesson.