Skip to main content

colab-nb-runner

Run a Jupyter notebook cell by cell in a Google Colab session, from your terminal. A small wrapper around the Colab CLI for long GPU jobs that must not die with a dropped connection.

Türkçe özet aşağıda.

test PyPI MIT

The problem

The Colab CLI can execute Python in a session (colab exec), but it has no "run this .ipynb" command. Looping over the cells yourself runs into three things. The first two we measured on real runs; the third follows from retrying:

What happens What it costs you
colab exec sometimes exits 0 after a Python exception a failed notebook looks successful
The websocket drops mid-request ("Connection was lost"), and the CLI then hangs instead of exiting an hours-long cell cannot be awaited in one call
Retrying a dropped request can start the same cell twice a long cell runs twice, or a side effect is duplicated

How it works

  • Each cell is started in a background thread in the kernel. Its output goes to a log file in the VM.
  • The runner reads state and new output with short polls. A poll only reads, so it is retried freely after a drop.
  • Starting a cell is idempotent: after a lost reply the runner asks the kernel whether the cell already started before trying again.
  • Every colab call has a wall-clock limit; a hung CLI process is killed and the call retried.
  • A cell fails on a non-zero exit code or a traceback in its output. The run stops at the first failing cell and exits 1.

Kernel state carries over between cells, as with "Run all".

Install

uv tool install google-colab-cli      # the Colab CLI itself, then: colab new -s mysession --gpu T4
pip install colab-nb-runner

Python 3.9+, macOS or Linux, no dependencies. Needs the colab command on your PATH.

Use

colab-nb-run notebook.ipynb -s mysession
=== CELL 2/3 (thread, limit 1800s) ===
  .. poll failed (1 in a row, rc 0)
adım ığüşöç 0
adım ığüşöç 1
adım ığüşöç 2
adım ığüşöç 3
[cell 2: 42.6s, ok]

=== CELL 3/3 (thread, limit 1800s) ===
answer 42
[cell 3: 13.2s, ok]

=== NOTEBOOK DONE (1.6 min) ===

That is a real run: one poll was lost and the output still arrived complete.

Option Meaning
--dry-run Send nothing; list the cells, their limits and how each would run
--timeouts t.json {"3": 5400}: limit in seconds per code cell index (0-based, markdown cells not counted). Others get --default-timeout (1800)
--patch p.json {"old text": "new text"} applied to this run only. Each entry must match exactly once, otherwise nothing is sent
--start N, --end N Run only a range of code cells, e.g. to resume after a failure
--skip REGEX Replace matching top-level lines with pass (repeatable)
--config FILE Colab CLI session state file, passed through as colab --config
--poll SECONDS Poll interval (20)

On a Mac, run long jobs under caffeinate -dimsu colab-nb-run .... When the machine sleeps, the session can be lost.

Things to know

  • Google Drive: mount it once before the run with colab drivemount -s mysession. Top-level drive.mount(...) lines in the notebook are skipped by default, because a second mount waits for a browser confirmation nobody will give. Use --keep-drive-mount to keep them.
  • input() and getpass() raise instead of waiting forever. Pass the value with --patch.
  • Magic cells: a cell with a line starting with ! or % cannot run in a thread. It is sent as one request and retried if the connection drops, so it must be safe to run twice (a pip install is).
  • Only text output is shown. Plots and other rich display() output are not captured.
  • A cell over its limit keeps running in the kernel. A thread cannot be stopped from outside; the runner stops and tells you. Inspect or restart the session.
  • Cells run in a thread, so code that must be on the main thread (signal handlers, some GUI or async setups) will not work.
  • Error hook: if the notebook defines _nbrun_on_error(exception), it is called when a threaded cell fails. IPython's own post_run_cell hooks do not see these failures.
  • Colab Secrets (userdata.get) were not readable from colab exec when we measured (CLI 0.6.0). Set them from a browser cell in the same session, or with colab exec --env.

Tested

  • 19 automated tests run the real runner against a fake kernel and a fake colab executable that drops replies, loses requests and hangs on demand. No Colab account needed: python -m unittest discover -s tests.
  • Checked by hand on a real CPU session with Colab CLI 0.7.4 (11 Nov 2026): state across cells, a magic cell, a failing cell and an input() call. The design comes from long GPU pipeline runs with CLI 0.6.0.
  • Not tested on Windows; it uses POSIX process groups.

This is not an official Google project.

License

MIT


Türkçe

Bir Jupyter notebook'unu terminalden, Google Colab oturumunda hücre hücre çalıştırır. Colab CLI'ın üstüne ince bir katman; bağlantı kopunca ölmemesi gereken uzun GPU işleri için.

Colab CLI Python kodu çalıştırıyor ama .ipynb çalıştırmıyor. Hücreleri elle döndürünce üç şeye takıldık: colab exec Python hatasında bazen 0 dönüyor, bağlantı kopunca CLI çıkmayıp asılı kalıyor, kopan isteği yeniden denemek aynı hücreyi iki kez başlatabiliyor.

Çözüm: hücre kernel'de arka plan iş parçacığında başlar ve çıktısını VM'deki bir dosyaya yazar. Çalıştırıcı kısa yoklamalarla durumu ve yeni satırları okur; yoklama yalnız okuduğu için kopunca rahatça tekrarlanır. Bir hücreyi yeniden başlatmadan önce kernel'e "zaten başladı mı" diye sorulur. İlk hatalı hücrede durur ve 1 ile çıkar.

pip install colab-nb-runner
colab-nb-run notebook.ipynb -s oturum

Seçenekler ve bilinmesi gerekenler yukarıdaki İngilizce bölümde. Mac'te uzun koşuları caffeinate -dimsu altında başlatın.

Metadata

Release files for colab-nb-runner 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for colab-nb-runner 0.1.0
File Size Uploaded Provenance Actions
colab_nb_runner-0.1.0.tar.gz 16.5 kB With provenance Details for colab_nb_runner-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for colab-nb-runner 0.1.0
File Interpreter ABI Platform Actions
colab_nb_runner-0.1.0-py3-none-any.whl Python 3 none any Details for colab_nb_runner-0.1.0-py3-none-any.whl

Total release size: 28.4 kB

Release files / colab_nb_runner-0.1.0.tar.gz

Download URL colab_nb_runner-0.1.0.tar.gz
Size 16.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a0b4e8d623797f1e0a58039c68b564dfc81cc823739d0e9ca1f4ff11569ef60c
BLAKE2b-256 checksum
How to use checksums
55ab070ad99cc0491c9505384d7c879fb7c4cfe15d01a79361e465b3d2bbfe36
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

Release files / colab_nb_runner-0.1.0-py3-none-any.whl

Download URL colab_nb_runner-0.1.0-py3-none-any.whl
Size 11.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0189a58f93d81ff3a141f4cbb0b5d1b61715eb468b5b2dc119f11486644d5cbb
BLAKE2b-256 checksum
How to use checksums
8dd09a16d74f649abbd113e296629eae711f7b25cb261d6b3dc56187873ac5e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

Security for colab-nb-runner 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for colab_nb_runner-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 11, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page