Skip to content

chore(scripts): add reusable notebook_setup sparse-checkout helper - #14768

Open
iennae wants to merge 1 commit into
mainfrom
feat/notebook-setup-helper
Open

iennae wants to merge 1 commit into
mainfrom
feat/notebook-setup-helper

Conversation

@iennae

@iennae iennae commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds a reusable standard-library bootstrap helper (scripts/notebook-setup/notebook_setup.py) so tutorial notebooks across python-docs-samples do not have to inline ~65 lines of git sparse-checkout, pip install, and importlib.reload plumbing at the top of each .ipynb file.

Key Behaviors

  • Colab / Colab Enterprise standalone .ipynb runs: Creates or updates a managed sparse checkout (samples-<name>), self-heals partial non-git directories, fetches and checks out the latest FETCH_HEAD for the requested branch, installs requirements.txt, and reloads any previously imported sibling modules.
  • Local developer & CI checkouts: Detects an enclosing local repository checkout and leaves local working-tree edits untouched (no git fetch or git checkout -f), switching into the sample directory and ensuring sys.path is configured.
  • Unit tests: Tested in scripts/notebook-setup/tests/notebook_setup_test.py with target-safe mock.patch.object(..., autospec=True).

@iennae
iennae requested review from a team as code owners November 6, 2026 21:52
@product-auto-label product-auto-label Bot added the samples Issues that are directly related to samples. label Nov 6, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a reusable notebook setup helper (notebook_setup.py) along with tests and documentation to bootstrap sample environments in Google Colab or local checkouts. The review feedback highlights several improvement opportunities: handling potential None values in sys.modules to prevent runtime crashes, safely handling cases where the repository root path is a file rather than a directory, removing the --upgrade flag from pip install to avoid breaking pre-installed Colab packages, and using a more robust parent-directory search in the README usage example.

Comment thread scripts/notebook-setup/README.md Resolved
Add a reusable, standard-library bootstrap utility in scripts/notebook-setup/
for tutorials and sample notebooks that depend on sibling Python modules when
opened standalone in Google Colab or Colab Enterprise.

- Sparse-clones only the target sample subdirectory into a managed cache folder,
  self-heals interrupted non-git directories, fetches and checks out FETCH_HEAD
  on persistent runtimes, installs requirements.txt, and reloads imported sibling
  sample modules.
- Preserves existing local developer and CI repository checkouts without running
  git fetch or git checkout -f.
- Includes unit tests in scripts/notebook-setup/tests/notebook_setup_test.py.
@iennae
iennae force-pushed the feat/notebook-setup-helper branch from b896ebf to 848bc82 Compare November 7, 2026 03:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant