Skip to content

Executable walkthrough

This page computes a small sequence, checks its closed form, and displays the result in several MIME formats. Everything below uses Python's standard library and IPython, so the native and browser environments need no scientific packages.

Shared state across cells

The first cell defines the number of terms. Its output is generated during the site build. Enable interactivity using the wand beside a cell’s copy button, then select Run all to repeat the calculation in your browser. Change n to see the later outputs change together.

n = 8
values = list(range(1, n + 1))
print(f"Terms: {values}")
Terms: [1, 2, 3, 4, 5, 6, 7, 8]

Every cell on this page shares one kernel. This hidden setup cell imports the display helpers used later; it also runs when you choose Run all.

The sum of the first nn positive integers is

Sn=∑k=1nk=n(n+1)2.(1)S_n = \sum_{k=1}^{n} k = \frac{n(n+1)}{2}.\tag{1}

We check (1) against the explicit sum. An assertion failure would stop the documentation build.

total = sum(values)
assert total == n * (n + 1) // 2
print(f"Sum: {total}")
Sum: 36

Prose can quote the kernel directly: the eval role reports that the first 8 terms sum to 36, computed during the build.

Rich output from MIME bundles

Jupyter records structured MIME bundles. This cell supplies both HTML and plain text; the renderer selects HTML according to its MIME priority.

display({
"text/html": f"<p><strong>Computed total:</strong> {total}</p>",
"text/plain": f"Computed total: {total}",
}, raw=True)

Computed total: 36

The next cell emits Plotly's JSON MIME format directly. No Python Plotly package is needed for this example. Hover over the points or drag to zoom.

display({
"application/vnd.plotly.v1+json": {
"data": [{
"x": values,
"y": [k * (k + 1) // 2 for k in values],
"type": "scatter",
"mode": "lines+markers",
}],
"layout": {
"title": {"text": "Partial sums"},
"xaxis": {"title": {"text": "Number of terms"}},
"yaxis": {"title": {"text": "Sum"}},
"height": 320,
"margin": {"t": 50, "r": 20, "b": 50, "l": 50},
},
},
"text/plain": "A plot of partial sums",
}, raw=True)

Included prose and source maps

This paragraph comes from an included file. Select the words included file and inspect window.mystSourceMap.resolve(window.getSelection().getRangeAt(0)) in the browser console. The returned origin names the included file, carries its content digest, and marks the mapping as exact or as a containing range.

Generated output has no authored prose location. Browser edits do not rewrite the source origins attached to the original document.

Resetting the interactive session starts a fresh kernel and preserves edited cells. Choose Run all again to recreate the shared state. Reloading the page restores the authored code and build-time output.

The authoring guide shows the source syntax, and browser execution explains how to add your own packages.