Repository navigation
Running cells
gpython reads the code of each cell. It finds the variables that the cell defines and the variables that the cell uses. From this, gpython knows which cells depend on which.
| Term | Meaning |
|---|---|
| Stale cell | A cell that is out of date: it did not run, you changed it, or one of its inputs changed |
| Cell number | The number in the cell header, for example c6
|
| Kernel | The Python process that runs the cells of one notebook |
When you run a cell, gpython does these steps:
- It runs the stale cells that this cell uses.
- It runs the cell.
- It handles the cells that use the variables of this cell. The run mode controls this step.
| Mode | What occurs to the cells that use this cell |
|---|---|
| lazy (default) | gpython marks them stale. You run them when you want |
| reactive | gpython runs them immediately, as marimo does |
Change the mode with f3, :lazy, :set lazy or :set nolazy. gpython saves the mode.
A stale cell keeps its old output on the screen until you run it.
For a .py notebook, gpython remembers this when you quit: an output that came from older code,
or from older inputs, is marked stale again when you open the file.
A cell whose code a different program changed is also marked stale. See File formats.
These keys work in all modes.
| Key | Action |
|---|---|
shift+enter |
Run the cell, then go to the next cell. On the last cell, make a new cell |
ctrl+enter, ctrl+shift+enter
|
Run the cell and stay |
alt+enter |
Run the cell and add a new cell below |
ctrl+e |
Run the cell and stay. Cell mode only |
f5 |
Run all cells |
f4 |
Run the stale cells |
ctrl+k |
Stop the code that runs. Not in insert mode |
Press r, then a second key. The cursor does not move.
- In cell mode, use
rorspace r. - In the editor (normal mode), use
space r. There,ralone is the vim replace command.
| Keys | Action | Command |
|---|---|---|
r 6 |
Run cell c6. The stale cells that it uses run first |
:run 6 |
r c |
Run this cell | |
r r |
Run the cell that ran last | |
r C |
Run the cells from the first cell to this cell | |
r a |
Run the cells after this cell | |
r A |
Run all cells | :run |
r s |
Run the stale cells | :stale |
r k |
Restart the kernel | :restart |
r R |
Restart the kernel and run all cells | |
r i |
Stop the code that runs | :interrupt |
r d |
Debug this cell. See Variables and the debugger | :debug |
r t |
Measure the run time of this cell | :timeit |
For r 6, gpython runs the cell when the number is complete, when you press enter, or after
a short pause. escape cancels. If you type no digit in 3 seconds, the sequence stops.
More commands: :runabove runs the cells above this cell. :runbelow runs this cell and the cells below it.
| Key or command | Result |
|---|---|
r t or :timeit
|
Run the cell one time |
r t 5 or :timeit 5
|
Run the cell five times |
t in cell mode, :set time
|
Show the last run time in each cell header |
r t prints a table below the cell: mean, median, std, variance, p50, p90, p99, min and max.
It is equivalent to %%timeit.
These rules are the same as in marimo.
- Only one cell can define a name.
- Two cells can contain the same import, for example
import seaborn as sns. The first cell owns the name. - A name that starts with
_is private to its cell. - gpython tracks changes in place at the top level of a cell, for example
xs.append(1)andd["k"] = v:- The cells that read the object run after the cell that changes it.
- If you run the cell that changes the object, gpython first runs the cell that made the object again. Thus the object starts clean.
Concurrent mode runs cells at the same time when they share no state. The cells run on threads in the one kernel. The mode is off by default.
| Command | Effect |
|---|---|
:set concurrent / :set noconcurrent
|
Turn the mode on or off. Saved |
:set concurrency=N |
Run a maximum of N cells at the same time. 1 runs them one after the other |
:set concurrent_verify / :set noconcurrent_verify
|
Turn the check after each group on or off. On by default |
:groups |
Show the groups, and what each cell waits for |
gpy --groups nb.py |
Print the same data without starting gpython |
The default for concurrency is the number of processor cores plus 4, with a maximum of 32.
Two cells can run together only if neither cell writes something that the other cell reads or writes (Bernstein's conditions). gpython finds this from the code.
gpython counts each of these as a write:
- An assignment.
- A change in place:
xs.append(...),d[k] = v, a function that changes a global. - A call to a function that gpython does not know.
- An effect: writing a file, seeding
random, drawing withplt.
These cells always run alone:
- Cells with magics (
%...). - Cells with a top-level
await. - Cells with
input(). - Cells with
breakpoint().
After a group runs, gpython examines the names that the cells of the group shared. If one of these names changed, gpython marks its readers stale. From then on, the cells of that run go one after the other.
| Type of work | Result |
|---|---|
| Network calls, sleeps, NumPy | Run in parallel |
| Pure Python loops | Take turns, because of the GIL |
| Cells that write files | Run one at a time |
Output, errors and r i work for each cell independently.
While a group runs, gpython limits the math libraries of NumPy to their share of the cores.
It uses threadpoolctl for this. gpython installs threadpoolctl with itself.
If the kernel runs in a project .venv or a conda environment, install threadpoolctl there too.
If you do not, parallel NumPy cells can use too many threads.
- A top-level
awaitworks in a cell. -
asyncio.run(...),loop.run_until_complete(...),asyncio.Runnerandanyio.runalso work in a cell. A library that calls them for you works too. -
ctrl+korr istops a cell that waits inawait.
When a cell calls input(), gpython asks for the text at the bottom of the screen.
escape stops the cell.
- A run sends its cells to the kernel as one request. The screen updates some times each second while results arrive.
- When a notebook has more than 100 cells, gpython builds only the cells near the visible area.
Keys, searches and
:ranges continue to work on all cells. - The kernel starts before the first frame, so the first cell can run sooner.
Use the notebook
- Getting started
- Command line
- Running cells
- Keys
- Commands
- Vim
- Markdown cells
- Variables and the debugger
- Images
Set up the workspace
Help and project