import sys
from tempfile import TemporaryDirectory
from unittest.mock import patch as patch_cliInstalling agent instructions
install_nbskill copies the package-owned routing skill and its references into a Codex or Claude skills directory. The routing file points to the Pyskill API in nbskill.skill; executable guidance stays with the Python package and its installed version. Pre-commit hooks are an optional installation step.
from nbskill.nbskill import install_nbskill
from nbskill.foundation import demo_path, remove_demo_path
from unittest.mock import patchThe package ships a compact routing skill. The operational guidance lives in the Python skill module, so installation copies package-owned instructions rather than generating a second workflow.
Production contract
Skill installation is Python-first. It installs only into the requested Codex or Claude skills directory, optionally installs notebook hooks.
Project setup and hooks
Project configuration and Git hooks belong to installation. Foundation only detects project ownership and does not install or repair a workspace.
Managed hook markers delimit the block installed by nbskill. They let later installation update that block without replacing unrelated hook content.
The managed pre-commit block runs nbdev-clean and rejects a commit when cleaning changes files. It then runs notebook tests, using uv when the commands are not on the path.
install_nbdev_pre_commit_hooks
def install_nbdev_pre_commit_hooks(
path:str='.', run_nbdev_install_hooks:bool=True
):Install nbdev-clean and nbdev-test pre-commit hooks in a git-backed nbdev project.
Respecting hook removal
install_nbdev_pre_commit_hooks records installation in .git/info. If you remove the hook, a later call records that choice and leaves it absent. This temporary Git repository demonstrates both states:
with TemporaryDirectory() as hook_example_dir:
hook_example_root = Path(hook_example_dir)
(hook_example_root/"pyproject.toml").write_text('[project]\nname = "hook_example"\nversion = "0.0.1"\n[tool.nbdev]\n')
subprocess.run(["git", "init"], cwd=hook_example_root, check=True, capture_output=True)
installed_hook = install_nbdev_pre_commit_hooks(hook_example_root, run_nbdev_install_hooks=False)
assert installed_hook["installed"]
installed_hook["installed"]Bootstrapping project configuration
bootstrap_nbskill_project
def bootstrap_nbskill_project(
path:str='.', overwrite_agents:bool=False, pause:str='1'
):Install nbskill project guardrails once a workspace root is known.
A project can need a small idempotent bootstrap before using nbskill. Once a workspace root is known, bootstrap_nbskill_project can copy the shared agent instructions, make notebook CI serial, and preserve nbskill metadata through nbdev cleanups without clobbering local AGENTS.md edits.
Installing agent instructions
The installer copies the compact routing skill and its reference material. Native notebook methodology lives in the Python skill module; installation only makes those files available to the chosen agent.
Installation targets select a skills directory before package files are copied. Existing identical files remain unchanged; overwriting a modified file depends on the caller’s option.
install_nbskill
install_nbskill returns the paths it changed and the paths already current. Set skills_dir to choose a destination, and overwrite=False to protect a locally edited routing file. The example copies real package files into a temporary directory:
install_nbskill
def install_nbskill(
target:str='codex', # codex, claude, cursor, both, or custom when skills_dir is set
skills_dir:str | None=None, # Parent skills directory; skill is installed below jupyter-notebooks
skill_name:str='jupyter-notebooks', # Skill folder name
overwrite:bool=True, # Update stale managed files
install_hooks:bool=False, # Install nbdev-clean/nbdev-test pre-commit hooks in the current repo
):Install current Python workflow instructions.
install_root = demo_path("06_skill_usage")
try:
installation = install_nbskill(skills_dir=install_root)
assert (install_root / "jupyter-notebooks" / "SKILL.md").exists()
install_preview = dict(changed=len(installation["installed"]), hooks=installation["hooks"]["reason"])
finally: remove_demo_path(install_root)
install_previewinstall_nbskill_cli
The command-line entry point exposes the same destination and overwrite options as install_nbskill. Use install_nbskill --help to inspect them. An actual invocation can copy files into the selected agent directory, so the notebook demonstrates the Python API against a disposable destination.
install_nbskill_cli
def install_nbskill_cli():Install notebook workflow instructions from the command line.
Preserving installed files
Repeating an installation with unchanged package content leaves every file byte-identical and reports it under unchanged. This checks the real files rather than a simulated copy operation.
bootstrap_nbskill_project reports an unrelated directory without writing project files.
with TemporaryDirectory() as unrelated_project:
bootstrap_status = bootstrap_nbskill_project(unrelated_project)
assert not bootstrap_status["changed"] and bootstrap_status["reason"] == "not-an-nbdev-project"
bootstrap_statusinstall_nbskill_cli uses an explicit skills directory so an installation can be inspected independently of the user’s normal configuration.
with TemporaryDirectory() as cli_skills_dir:
with patch_cli.object(sys, "argv", ["install_nbskill", "--skills-dir", cli_skills_dir]): cli_status = install_nbskill_cli()
assert cli_status is None
assert (Path(cli_skills_dir)/"jupyter-notebooks"/"SKILL.md").is_file()