Skip to content

Commit 7128c64

Browse files
authored
Merge branch 'master' into mh-add-support-md
2 parents 91d9e35 + d0fdc1e commit 7128c64

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

scripts/format_check.sh

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

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."
99+
exit 1
100+
fi
101+
94102
echo "Checking C++ formatting...";
95103
formatting_outputs=$(find tensorflow_quantum/ -iname *.h -o -iname *.cc | xargs clang-format -style=google -output-replacements-xml);
96104
CFORMATCHECK=0

scripts/validate_notebooks.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2020 The TensorFlow Quantum Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
# ==============================================================================
15+
"""Validate all Jupyter notebooks in the repository using nbformat."""
16+
import sys
17+
from pathlib import Path
18+
19+
import nbformat
20+
from nbformat.validator import NotebookValidationError
21+
22+
23+
def main():
24+
"""Check all notebooks for valid nbformat structure."""
25+
failed = False
26+
notebooks = list(Path('.').rglob('*.ipynb'))
27+
print(f"Found notebooks: {notebooks}")
28+
for notebook_path in notebooks:
29+
try:
30+
with open(notebook_path, 'r', encoding='utf-8') as f:
31+
nb = nbformat.read(f, as_version=4)
32+
nbformat.validate(nb)
33+
print(f"✓ {notebook_path}")
34+
except (NotebookValidationError, OSError) as e:
35+
print(f"✗ {notebook_path} failed validation: {e}")
36+
failed = True
37+
38+
if failed:
39+
sys.exit(1)
40+
else:
41+
print("All notebooks passed nbformat validation.")
42+
43+
44+
if __name__ == "__main__":
45+
main()

0 commit comments

Comments
 (0)