From 8cbc503e9420f753f92fb88d8db0fe5b49979b0a Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Sun, 14 Sep 2025 09:44:20 +0530 Subject: [PATCH 1/4] Add Jupyter notebook format check using nbstripout --- scripts/format_check.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/scripts/format_check.sh b/scripts/format_check.sh index 1d91427e0..d9e87183d 100755 --- a/scripts/format_check.sh +++ b/scripts/format_check.sh @@ -91,6 +91,22 @@ else exit 1 fi +# Check Jupyter Notebook format +echo "Checking Jupyter Notebook format..." +if ! command -v nbstripout &> /dev/null +then + echo "nbstripout could not be found, please install it with 'pip install nbstripout'" + exit 1 +fi + +for notebook in $(find . -name "*.ipynb"); do + nbstripout --check "$notebook" + if [ $? -ne 0 ]; then + echo "Notebook $notebook is not in the correct format. Please strip output and metadata." + exit 1 + fi +done + echo "Checking C++ formatting..."; formatting_outputs=$(find tensorflow_quantum/ -iname *.h -o -iname *.cc | xargs clang-format -style=google -output-replacements-xml); CFORMATCHECK=0 From bcf945212de84d40c7089850ac2f5e3ebf0f0406 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Fri, 12 Dec 2025 18:57:43 +0530 Subject: [PATCH 2/4] Add nbformat-based validation script for Jupyter notebooks --- scripts/format_check.sh | 18 +++++------------- scripts/validate_notebooks.py | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 scripts/validate_notebooks.py diff --git a/scripts/format_check.sh b/scripts/format_check.sh index d9e87183d..f0de918dc 100755 --- a/scripts/format_check.sh +++ b/scripts/format_check.sh @@ -91,22 +91,14 @@ else exit 1 fi -# Check Jupyter Notebook format -echo "Checking Jupyter Notebook format..." -if ! command -v nbstripout &> /dev/null -then - echo "nbstripout could not be found, please install it with 'pip install nbstripout'" +# Check Jupyter Notebook format using nbformat +echo "Checking Jupyter Notebook format with nbformat..." +python3 scripts/validate_notebooks.py +if [ $? -ne 0 ]; then + echo "One or more notebooks failed nbformat validation." exit 1 fi -for notebook in $(find . -name "*.ipynb"); do - nbstripout --check "$notebook" - if [ $? -ne 0 ]; then - echo "Notebook $notebook is not in the correct format. Please strip output and metadata." - exit 1 - fi -done - echo "Checking C++ formatting..."; formatting_outputs=$(find tensorflow_quantum/ -iname *.h -o -iname *.cc | xargs clang-format -style=google -output-replacements-xml); CFORMATCHECK=0 diff --git a/scripts/validate_notebooks.py b/scripts/validate_notebooks.py new file mode 100644 index 000000000..70ba86c5c --- /dev/null +++ b/scripts/validate_notebooks.py @@ -0,0 +1,26 @@ +import sys +import nbformat +from pathlib import Path + +def main(): + """Check all notebooks for valid nbformat structure.""" + failed = False + notebooks = list(Path('.').rglob('*.ipynb')) + print(f"Found notebooks: {notebooks}") + for notebook_path in notebooks: + try: + with open(notebook_path, 'r', encoding='utf-8') as f: + nb = nbformat.read(f, as_version=4) + nbformat.validate(nb) + print(f"✓ {notebook_path}") + except Exception as e: + print(f"✗ {notebook_path} failed validation: {e}") + failed = True + + if failed: + sys.exit(1) + else: + print("All notebooks passed nbformat validation.") + +if __name__ == "__main__": + main() From a79d15d23abaf622cb45d09fb9c58fa87c9f00aa Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Fri, 12 Dec 2025 19:14:36 +0530 Subject: [PATCH 3/4] Add nbformat-based validation script for Jupyter notebooks --- scripts/validate_notebooks.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/scripts/validate_notebooks.py b/scripts/validate_notebooks.py index 70ba86c5c..2d68ef23d 100644 --- a/scripts/validate_notebooks.py +++ b/scripts/validate_notebooks.py @@ -1,7 +1,25 @@ +# Copyright 2020 The TensorFlow Quantum Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Validate all Jupyter notebooks in the repository using nbformat.""" import sys -import nbformat from pathlib import Path +import nbformat +from nbformat.validator import NotebookValidationError + + def main(): """Check all notebooks for valid nbformat structure.""" failed = False @@ -13,7 +31,7 @@ def main(): nb = nbformat.read(f, as_version=4) nbformat.validate(nb) print(f"✓ {notebook_path}") - except Exception as e: + except (NotebookValidationError, OSError) as e: print(f"✗ {notebook_path} failed validation: {e}") failed = True From 71c4bac52a7dfbebd19f76f2f1d322cd28435493 Mon Sep 17 00:00:00 2001 From: Aniket Singh Yadav Date: Fri, 12 Dec 2025 19:16:48 +0530 Subject: [PATCH 4/4] Add nbformat-based validation script for Jupyter notebooks --- scripts/validate_notebooks.py | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/validate_notebooks.py b/scripts/validate_notebooks.py index 2d68ef23d..dd924f449 100644 --- a/scripts/validate_notebooks.py +++ b/scripts/validate_notebooks.py @@ -40,5 +40,6 @@ def main(): else: print("All notebooks passed nbformat validation.") + if __name__ == "__main__": main()