From 682c99c04f60cc247b0086825cc1eafe5b0888ea Mon Sep 17 00:00:00 2001 From: Jammy2211 Date: Thu, 9 Apr 2026 09:00:37 +0100 Subject: [PATCH] feat: add setup_notebook() to replace pyprojroot boilerplate in workspace notebooks Adds a single-call utility that finds the workspace root (via config/ directory), sets the working directory, reconfigures autoconf paths, and enables inline plotting. This replaces the 5-line pyprojroot boilerplate previously needed in every script. Co-Authored-By: Claude Opus 4.6 --- autoconf/__init__.py | 1 + autoconf/setup_colab.py | 2 -- autoconf/setup_notebook.py | 41 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 autoconf/setup_notebook.py diff --git a/autoconf/__init__.py b/autoconf/__init__.py index 313b4ce..e60f224 100644 --- a/autoconf/__init__.py +++ b/autoconf/__init__.py @@ -9,6 +9,7 @@ from .json_prior.config import JSONPriorConfig from .setup_colab import for_autolens +from .setup_notebook import setup_notebook from .test_mode import test_mode_level, is_test_mode diff --git a/autoconf/setup_colab.py b/autoconf/setup_colab.py index 023773b..4ca165a 100644 --- a/autoconf/setup_colab.py +++ b/autoconf/setup_colab.py @@ -136,7 +136,6 @@ def for_autogalaxy(raise_error_if_not_gpu: bool = True) -> None: "pyvis==0.3.2", "dill==0.4.0", "jaxnnls", - "pyprojroot==0.2.0", "nautilus-sampler==1.0.4", "timeout_decorator==0.5.0", "anesthetic==2.8.14", @@ -183,7 +182,6 @@ def for_autolens(raise_error_if_not_gpu: bool = True) -> None: "pyvis==0.3.2", "dill==0.4.0", "jaxnnls", - "pyprojroot==0.2.0", "nautilus-sampler==1.0.4", "timeout_decorator==0.5.0", "anesthetic==2.8.14", diff --git a/autoconf/setup_notebook.py b/autoconf/setup_notebook.py new file mode 100644 index 0000000..68c2088 --- /dev/null +++ b/autoconf/setup_notebook.py @@ -0,0 +1,41 @@ +import os +from pathlib import Path + + +def setup_notebook(): + """ + Set up a Jupyter notebook to run from the workspace root. + + Finds the workspace root by walking up from the current directory + looking for a ``config`` directory (the marker for a PyAuto workspace), + changes to it, reconfigures autoconf paths, and enables inline plotting. + """ + root = _find_workspace_root() + os.chdir(root) + + from autoconf import conf + + conf.instance.push( + new_path=root / "config", + output_path=root / "output", + ) + + try: + ipy = get_ipython() + ipy.run_line_magic("matplotlib", "inline") + except NameError: + pass + + print(f"Working Directory has been set to `{root}`") + + +def _find_workspace_root(start=None): + """Walk up from *start* (default: cwd) looking for a ``config/`` dir.""" + current = Path(start or os.getcwd()).resolve() + for parent in [current, *current.parents]: + if (parent / "config").is_dir(): + return parent + raise FileNotFoundError( + "Could not find workspace root (no 'config/' directory found " + f"in {current} or any parent)." + )