The Python cell
Some calculations do not fit into a formula. The Python cell is for those: it is real Python, executed in your browser, which sees the variables of the document.
Opening a Python cell
The “Python cell” button in the document toolbar inserts a code cell, with syntax highlighting and indentation. Variables already defined in the notebook are available there under their own names; what the code defines becomes visible again to the cells that follow.
NumPy, SciPy and SymPy are loaded by default. print() writes under the cell; a matplotlib figure appears in the Visualisation tab.
Installing a library
The “Packages” button installs, on demand, a library from a closed list: matplotlib, pandas, networkx, astropy, uncertainties, biopython, pint, scikit-learn, statsmodels, pillow, sympy_plot_backends. They come from the Pyodide repository at the moment you ask for them, then stay cached. No data from the document goes with the request.
Your own functions, without Python
For a reusable function, you do not need to go through code: an ordinary cell accepts f(x) := x² + 3x - 4. The function f becomes usable in the following cells, differentiable, plottable, and it appears in the Variables tab.
What remains true
The code runs in the browser's sandbox: it has access neither to your files nor to the network, and nothing goes to a server. Code that runs too long is interrupted by the maximum delay (⚙ Settings), and “⏹ Stop” works on a Python cell too.
Try it
Define n = 200 in an ordinary cell, then in a Python cell:
x = np.linspace(0, 10, n)
print(np.trapezoid(np.sin(x), x))
You get 1.838685. Change n to 2000 in the ordinary cell and run again: 1.839068, against an exact value of 1 − cos 10 = 1.839072.
Where to go next
A notebook containing Python exports to a Jupyter notebook, code cells included.