Skip to content

colab: make setup cell more robust#498

Merged
zivy merged 4 commits intoInsightSoftwareConsortium:mainfrom
Masood-Akram:fix/notebook-fix
Feb 3, 2026
Merged

colab: make setup cell more robust#498
zivy merged 4 commits intoInsightSoftwareConsortium:mainfrom
Masood-Akram:fix/notebook-fix

Conversation

@Masood-Akram
Copy link
Contributor

Improved Colab detection and made the clone step idempotent (won’t reclone on rerun).
Prevents false negatives and improves notebook re-runnability in Colab.

@Masood-Akram
Copy link
Contributor Author

This PR makes the Colab setup detection more robust and avoids re-cloning on reruns. Happy to adjust to preferred style.

@zivy
Copy link
Member

zivy commented Feb 2, 2026

Hello @Masood-Akram,

Thank you for your contribution. This improves the original code:

  1. More robust checking if on colab via importing google.colab instead of checking for environment variables.
  2. Shallow clone vs cloning the whole history.

What is the motivation for the dedicated _in_colab function vs. a more direct approach shown below?

try:
  import google.colab
  import pathlib
  # If the directory does not exist, perform a shallow clone (latest commit no history).
  if not pathlib.Path("SimpleITK-Notebooks").exists():
    !git clone --depth 1 https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks
  %cd SimpleITK-Notebooks/Python
  !pip install -q -r requirements.txt
except Exception:
  pass # not on colab

If the above code is acceptable to you over the usage of _in_colab, please modify your PR accordingly, otherwise would appreciate it if you could motivate the use of _in_colab.

@Masood-Akram
Copy link
Contributor Author

Thanks for the review. I added _in_colab() mainly to keep the Colab detection logic separate from the setup steps (readability and easier reuse across notebooks). That said, I agree the inline try/except approach you suggested is simpler and consistent with the style in the repo. I can update the PR to use your direct approach.

Masood-Akram and others added 3 commits February 3, 2026 08:49
The notebook repository does not use flake8_nb for linting,
so removed the noqa directive which is identified as a spelling
mistake by our spellchecker.
@zivy zivy marked this pull request as ready for review February 3, 2026 20:24
@zivy zivy merged commit bb86266 into InsightSoftwareConsortium:main Feb 3, 2026
16 of 18 checks passed
@zivy
Copy link
Member

zivy commented Feb 3, 2026

@Masood-Akram Thank you for the contribution. Merging and will propagate to the other notebooks that support colab.

@Masood-Akram
Copy link
Contributor Author

@zivy Happy to contribute to this repository.

@zivy
Copy link
Member

zivy commented Feb 3, 2026

@Masood-Akram, just an FYI, I propagated this change to all the notebooks that use colab and listed you as co-author (PR #499).

@Masood-Akram
Copy link
Contributor Author

@zivy Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants