Skip to content

Commit bcf9452

Browse files
committed
Add nbformat-based validation script for Jupyter notebooks
1 parent 8cbc503 commit bcf9452

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

scripts/format_check.sh

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,22 +91,14 @@ else
9191
exit 1
9292
fi
9393

94-
# Check Jupyter Notebook format
95-
echo "Checking Jupyter Notebook format..."
96-
if ! command -v nbstripout &> /dev/null
97-
then
98-
echo "nbstripout could not be found, please install it with 'pip install nbstripout'"
94+
# Check Jupyter Notebook format using nbformat
95+
echo "Checking Jupyter Notebook format with nbformat..."
96+
python3 scripts/validate_notebooks.py
97+
if [ $? -ne 0 ]; then
98+
echo "One or more notebooks failed nbformat validation."
9999
exit 1
100100
fi
101101

102-
for notebook in $(find . -name "*.ipynb"); do
103-
nbstripout --check "$notebook"
104-
if [ $? -ne 0 ]; then
105-
echo "Notebook $notebook is not in the correct format. Please strip output and metadata."
106-
exit 1
107-
fi
108-
done
109-
110102
echo "Checking C++ formatting...";
111103
formatting_outputs=$(find tensorflow_quantum/ -iname *.h -o -iname *.cc | xargs clang-format -style=google -output-replacements-xml);
112104
CFORMATCHECK=0

scripts/validate_notebooks.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
import nbformat
3+
from pathlib import Path
4+
5+
def main():
6+
"""Check all notebooks for valid nbformat structure."""
7+
failed = False
8+
notebooks = list(Path('.').rglob('*.ipynb'))
9+
print(f"Found notebooks: {notebooks}")
10+
for notebook_path in notebooks:
11+
try:
12+
with open(notebook_path, 'r', encoding='utf-8') as f:
13+
nb = nbformat.read(f, as_version=4)
14+
nbformat.validate(nb)
15+
print(f"✓ {notebook_path}")
16+
except Exception as e:
17+
print(f"✗ {notebook_path} failed validation: {e}")
18+
failed = True
19+
20+
if failed:
21+
sys.exit(1)
22+
else:
23+
print("All notebooks passed nbformat validation.")
24+
25+
if __name__ == "__main__":
26+
main()

0 commit comments

Comments
 (0)