Skip to content

Instantly share code, notes, and snippets.

@scivision
Created August 2, 2023 21:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scivision/e7326fdcfadb0c61b3f0adfff84f0f13 to your computer and use it in GitHub Desktop.
Save scivision/e7326fdcfadb0c61b3f0adfff84f0f13 to your computer and use it in GitHub Desktop.
Strip Jupyter notebook outputs as Git pre-commit hook
#!/usr/bin/env python3
"""
Strip Jupyter notebook outputs as Git pre-commit hook
"""
import subprocess
from pathlib import Path
files_changed = subprocess.check_output(["git", "diff", "--staged", "--name-only"], text=True).split("\n")
for file in files_changed:
p = Path(file)
if not p.is_file():
continue
if p.suffix == ".ipynb":
subprocess.check_call(["jupyter", "nbconvert", "--clear-output", "--inplace", file])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment