Skip to content

Commit 7f319c8

Browse files
MarcoGorelliMarco GorelliAlexAndorra
authored
CI add hook to check notebooks have watermark (#4170)
* add hook to check for watermark * Update scripts/check_watermark.py Co-authored-by: Alexandre ANDORRA <[email protected]> * Update scripts/check_watermark.py Co-authored-by: Alexandre ANDORRA <[email protected]> Co-authored-by: Marco Gorelli <[email protected]> Co-authored-by: Alexandre ANDORRA <[email protected]>
1 parent d8bfe93 commit 7f319c8

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,10 @@ repos:
2626
rev: 20.8b1
2727
hooks:
2828
- id: black
29+
- repo: local
30+
hooks:
31+
- id: watermark
32+
name: Check notebooks have watermark
33+
types: [jupyter]
34+
entry: python scripts/check_watermark.py
35+
language: python

scripts/check_watermark.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Check that given Jupyter notebooks all contain a final watermark cell to facilite reproducibility.
3+
4+
This is intended to be used as a pre-commit hook, see `.pre-commit-config.yaml`.
5+
You can run it manually with `pre-commit run watermark --all`.
6+
"""
7+
8+
import argparse
9+
from pathlib import Path
10+
import re
11+
12+
if __name__ == "__main__":
13+
parser = argparse.ArgumentParser()
14+
parser.add_argument("filenames", nargs="*")
15+
args = parser.parse_args()
16+
for file_ in args.filenames:
17+
assert (
18+
re.search(
19+
r"%load_ext watermark.*%watermark -n -u -v -iv -w",
20+
Path(file_).read_text(),
21+
flags=re.DOTALL,
22+
)
23+
is not None
24+
), (
25+
f"Watermark not found in {file_} - please see the PyMC3 Jupyter Notebook Style guide:\n"
26+
"https://github.com/pymc-devs/pymc3/wiki/PyMC's-Jupyter-Notebook-Style"
27+
)

0 commit comments

Comments
 (0)