Skip to content

Commit dea41af

Browse files
committed
Add project root detection utils to notebook_tools
- Create utils module with project_root.py - Add find_project_root() and set_project_root() functions - Use project-specific markers for reliable detection - Enable consistent root detection across all notebooks
1 parent 5c20c72 commit dea41af

3 files changed

Lines changed: 100 additions & 3 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# lib/notebook_tools/utils/__init__.py
2+
"""Utility modules for notebook tools."""
3+
4+
from .project_root import find_project_root, set_project_root
5+
6+
__all__ = ["find_project_root", "set_project_root"]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# lib/notebook_tools/utils/project_root.py
2+
"""Project root detection utilities."""
3+
4+
import os
5+
from pathlib import Path
6+
7+
8+
def find_project_root():
9+
"""Auto-detect project root by finding marker files."""
10+
current = Path.cwd()
11+
# Use specific files from your project
12+
markers = ['environment.yml', 'PROVENANCE.md', 'scripts/setup_experiments_structure.sh']
13+
14+
for path in [current] + list(current.parents):
15+
if any((path / marker).exists() for marker in markers):
16+
return path
17+
# Stop at filesystem root
18+
if path == path.parent:
19+
break
20+
return current
21+
22+
23+
def set_project_root():
24+
"""
25+
Set working directory to project root and return path.
26+
27+
Returns:
28+
Path: Project root directory after changing to it
29+
"""
30+
project_root = find_project_root()
31+
os.chdir(project_root)
32+
return project_root

notebooks/templates/01_preprocessing.working.ipynb

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,85 @@
3636
"<!-- TOC -->"
3737
]
3838
},
39+
{
40+
"cell_type": "markdown",
41+
"id": "17947ae6-b8e5-429f-a7ca-defdd4908b5f",
42+
"metadata": {},
43+
"source": [
44+
"## Confirm Directory Structure\n",
45+
"\n",
46+
"Validates project directories exist at root level."
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 1,
52+
"id": "1c55d534-4c27-4620-a2f2-496bf78e50c3",
53+
"metadata": {},
54+
"outputs": [
55+
{
56+
"name": "stdout",
57+
"output_type": "stream",
58+
"text": [
59+
"Current dir: /home/trauco/experiments-framework/notebooks/templates\n",
60+
"Project root: /home/trauco/experiments-framework\n",
61+
"✓ data/raw\n",
62+
"✓ data/clips\n",
63+
"✓ data/frames\n",
64+
"✓ configs\n",
65+
"✓ models\n",
66+
"✓ lib\n",
67+
"✓ notebooks\n"
68+
]
69+
}
70+
],
71+
"source": [
72+
"import sys\n",
73+
"import os\n",
74+
"from pathlib import Path\n",
75+
"\n",
76+
"# show current location\n",
77+
"print(f\"Current dir: {Path.cwd()}\")\n",
78+
"\n",
79+
"# manual path to project\n",
80+
"project_root = Path('/home/trauco/experiments-framework')\n",
81+
"os.chdir(project_root)\n",
82+
"print(f\"Project root: {project_root}\")\n",
83+
"\n",
84+
"# add lib to path\n",
85+
"lib_path = project_root / 'lib'\n",
86+
"if str(lib_path) not in sys.path:\n",
87+
" sys.path.insert(0, str(lib_path))\n",
88+
"\n",
89+
"# verify structure\n",
90+
"dirs = ['data/raw', 'data/clips', 'data/frames', 'configs', 'models', 'lib', 'notebooks']\n",
91+
"for dir_path in dirs:\n",
92+
" if Path(dir_path).exists():\n",
93+
" print(f\"✓ {dir_path}\")\n",
94+
" else:\n",
95+
" print(f\"✗ {dir_path}\")"
96+
]
97+
},
3998
{
4099
"cell_type": "code",
41100
"execution_count": null,
42-
"id": "fc72b50c-ad28-481e-b9e2-3b08c7a93c69",
101+
"id": "edbb48e5-ba73-4735-8b98-24b706b05f08",
43102
"metadata": {},
44103
"outputs": [],
45104
"source": []
46105
},
47106
{
48107
"cell_type": "code",
49108
"execution_count": null,
50-
"id": "4a9712a5-7ebe-4a24-aabc-9b7a59a4df4b",
109+
"id": "644b0c3b-a587-4609-b246-bdbeec2f91b8",
51110
"metadata": {},
52111
"outputs": [],
53112
"source": []
54113
},
55114
{
56115
"cell_type": "code",
57116
"execution_count": null,
58-
"id": "40a0baf8-b339-4639-a190-3e86632739f0",
117+
"id": "8757f9a5-43d5-4266-b15f-9cd407e24801",
59118
"metadata": {},
60119
"outputs": [],
61120
"source": []

0 commit comments

Comments
 (0)