Course / Tutorials / Week 12 / Q-34
Quick tutorial Q-34 · Week 12 · about 15 min · Foundation
A calculation in a web page
Write the foot-plant force formula in Python and in JavaScript, run both on three hand-checked cases, and confirm they give the same numbers.
- Ask
- Collect
- Check
- Interpret
- Coach (this week)
- Reassess
Download notebook ·Python file ·Read as Markdown ·Data folder ·Chapter 12 (sections 3.7.2, 12.2, 12.4.1)
2 of 3 cells run in this page. The rest need a model that does not run in the browser: run them in the notebook on your computer or in Colab (File → Upload notebook); their output from the course build is shown, and the files they write are published so the later cells still run here.
The decision
Before a calculation goes into a page an athlete will open, check that the page's JavaScript gives the same numbers as the Python you trusted, on cases whose answers you know.
The raw data
raw/test_cases.csv: three typed test cases (contact and flight time in seconds), not a recording: two from the book's running examples and one short sprint contact. Typed for this tutorial from Chapter 3 (contact 0.25 s, step 0.349 s) and Chapter 9 (contact 0.200 s, stride 0.700 s).
Try it
A calculation reaches an athlete when it runs in a page on their phone. The Python cells on this page already run in your browser: they use Pyodide, a version of Python compiled to WebAssembly, so nothing is sent to a server. A page an athlete opens, though, is usually written in JavaScript, the browser's own language, and loads in a fraction of a second. So the same calculation is written twice, and the two must agree.
The calculation is the force of a foot plant from its timing (Section 3.7.2): over a step the ground's push must average one body weight, and with a half-sine shape the peak is
F_peak / body weight = (π/2) × (1 + flight time / contact time).
The Python version, on three test cases
The three cases were typed before any code was written, from the book's examples, so each has a hand answer to check against.
Type a prediction to enable Run.
Show the expected result without running
case contact_s flight_s python_bw
book runner 0.25 0.099 2.1928
course clip 0.20 0.150 2.7489
sprint 0.10 0.120 3.4558The same function in JavaScript
This is what goes in the page. Read it line by line against the Python: Math.PI is math.pi, and export lets the page and a test file import it.
// force.js: peak force of a foot plant, in body weights (Section 3.7.2)
export function peakForceBW(contactS, flightS) {
return (Math.PI / 2) * (1 + flightS / contactS);
}
Run the JavaScript on the same cases
This cell runs the JavaScript with Node.js, the JavaScript engine for a computer, so it is marked to run on your computer, not in the page. It writes the answers to a file that the next cell, back in the browser, compares with Python.
Run force.js with Node.js
Runs in the notebook (needs a model that does not run in the browser). Output from the course build:
import subprocess, os
JS = """export function peakForceBW(contactS, flightS) {
return (Math.PI / 2) * (1 + flightS / contactS);
}"""
CHECK = """import { readFileSync, writeFileSync } from 'node:fs';
import { peakForceBW } from './force.mjs';
const rows = readFileSync('raw/test_cases.csv', 'utf8').trim().split('\\n').slice(1);
const out = rows.map((r) => { const [name, c, f] = r.split(',');
return `${name},${peakForceBW(Number(c), Number(f))}`; });
writeFileSync('clean/force_js.csv', 'case,js_bw\\n' + out.join('\\n') + '\\n');"""
os.makedirs("clean", exist_ok=True)
open("force.mjs", "w").write(JS + "\n")
open("check_force.mjs", "w").write(CHECK + "\n")
run = subprocess.run(["node", "check_force.mjs"], capture_output=True, text=True)
print("node exit code", run.returncode, run.stderr.strip())
print(open("clean/force_js.csv").read().strip())node exit code 0 case,js_bw book runner,2.1928316722056755 course clip,2.748893571891069 sprint,3.455751918948773
Do they agree?
Type a prediction to enable Run.
Show the expected result without running
case python_bw js_bw difference
book runner 2.192832 2.192832 0.0
course clip 2.748894 2.748894 0.0
sprint 3.455752 3.455752 0.0
largest difference: 0.0
Both languages use the same 64-bit floating-point numbers, so for a formula this simple the answers agree to every printed digit. For longer calculations they can differ in the last digit or two, so tests compare with a tolerance, such as 0.0001 body weights, not with ==.
For the athlete: "The page on your phone uses the same formula we checked here: with a 0.25 s contact and a 0.1 s flight, each foot plant pushes about 2.2 times your body weight."
Check yourself
Where do the Python cells on this page run?
Why compare the two versions on cases typed before the code was written?
Contact 0.20 s, flight 0.15 s. What peak force does the formula give, in body weights?
References
- Pyodide documentation: Pyodide, a Python distribution for the browser and Node.js based on WebAssembly.
- Morin, J.-B., Dalleau, G., Kyröläinen, H., Jeannin, T., and Belli, A. (2005). A simple method for measuring stiffness during running. Journal of Applied Biomechanics, 21(2), 167–180. (The sine-wave model of the force–time curve from contact and flight time.)
Built 2026-11-30 with Python packages numpy 2.5.5, pandas 3.1.1, matplotlib 3.11.2. In the browser the same code runs on Pyodide, so the last digit can differ.