Skip to content

Commit 3c1124a

Browse files
authored
chore(scripts): add reusable notebook_setup sparse-checkout helper (#14768)
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.
1 parent 6d6d315 commit 3c1124a

8 files changed

Lines changed: 779 additions & 0 deletions

File tree

‎scripts/notebook-setup/README.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Notebook Setup Helper (`notebook_setup.py`)
2+
3+
`notebook_setup.py` provides a standard-library bootstrap utility for notebooks in `python-docs-samples` that depend on sibling Python modules and `requirements.txt` files when opened standalone in Google Colab or Colab Enterprise.
4+
5+
## Usage in a Notebook
6+
7+
```python
8+
# @title Download sample files and install packages (~30 seconds)
9+
from pathlib import Path
10+
import sys
11+
import tempfile
12+
import urllib.request
13+
14+
BRANCH = "main"
15+
SETUP_PATH = Path(tempfile.gettempdir()) / "notebook_setup.py"
16+
for parent in (Path.cwd(), *Path.cwd().parents):
17+
candidate = parent / "scripts/notebook-setup/notebook_setup.py"
18+
if candidate.is_file():
19+
SETUP_PATH = candidate
20+
break
21+
else:
22+
if not SETUP_PATH.is_file():
23+
url = (
24+
"https://raw.githubusercontent.com/GoogleCloudPlatform/"
25+
f"python-docs-samples/{BRANCH}/scripts/notebook-setup/notebook_setup.py"
26+
)
27+
with urllib.request.urlopen(url, timeout=30) as response:
28+
SETUP_PATH.write_bytes(response.read())
29+
30+
setup_dir = str(SETUP_PATH.parent.resolve())
31+
if setup_dir not in sys.path:
32+
sys.path.insert(0, setup_dir)
33+
import notebook_setup
34+
35+
notebook_setup.bootstrap("people-and-planet-ai/weather-forecasting", branch=BRANCH)
36+
```

‎scripts/notebook-setup/__init__.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Copyright 2026 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.

0 commit comments

Comments
 (0)