Sarmate.net Sarmate.net
Home Features Pricing Documentation Contact
Log in Sign up
Scientific web 8 min

Python in the browser — <mp-code>

One of the points where mathpad sets itself apart from PDF: Python directly embedded and runnable in the browser. The student edits the code in a CodeMirror editor, clicks Run, the output (including turtle and pygame) appears in the course. No server, no install.

First example

A simple print, runnable thanks to the runnable attribute:

HTML
<mp-code runnable>
print("Hello, world !")
</mp-code>
Output — click Run print("Hello, world !")

Available attributes

AttributeValuesDescription
runnablebooleanAdds a green Run button. Without it, the block is just a syntax-highlighted editor (no execution).
widthInteger px (default 500)Editor width. Use 350 for mobile-friendly.
heightInteger px (default 200)Display hint (CSS forces an auto height that adapts to content).
themeneat (default) · dracula · monokai · material · eclipseCodeMirror theme. Lazy-loaded — only the used theme is downloaded.
Python only mp-code exclusively supports Python (CodeMirror text/x-cython mode + Skulpt). A lang="..." attribute is ignored. For JavaScript or another language, use a plain <pre><code>.

Loops and functions

Skulpt supports Python 3 — functions, loops, lists, conditions, everything works as expected.

HTML
<mp-code runnable>
def factorielle(n):
    if n <= 1:
        return 1
    return n * factorielle(n - 1)

for i in range(1, 7):
    print(i, "! =", factorielle(i))
</mp-code>
Output — click Run def factorielle(n): if n <= 1: return 1 return n * factorielle(n - 1) for i in range(1, 7): print(i, "! =", factorielle(i))

Turtle graphics

The turtle module works natively. Skulpt draws in a <canvas> that appears automatically below the editor.

HTML
<mp-code runnable>
import turtle

t = turtle.Turtle()
t.speed(0)
for couleur in ["red", "orange", "gold", "green", "blue", "purple"]:
    t.color(couleur)
    for _ in range(4):
        t.forward(80)
        t.right(90)
    t.right(60)
</mp-code>
Output — click Run import turtle t = turtle.Turtle() t.speed(0) for couleur in ["red", "orange", "gold", "green", "blue", "purple"]: t.color(couleur) for _ in range(4): t.forward(80) t.right(90) t.right(60)

Available editor themes

neatdefault, light
draculapopular dark
monokaiclassic dark
materialdark blue-ish
eclipselight, IDE
HTML
<mp-code runnable theme="dracula" width="350">
moyenne = sum([12, 15, 9, 18]) / 4
print("Average :", moyenne)
</mp-code>
Output (Dracula theme) moyenne = sum([12, 15, 9, 18]) / 4 print("Average :", moyenne)

Best practices

Editor button In the online editor: Code → Python inserts the mp-code skeleton with runnable and a sample function. A second button offers alternative themes.

Ready to try mathpad?

Create your first interactive scientific HTML document in the online editor, or import your existing LaTeX course in one click.