File tree Expand file tree Collapse file tree 2 files changed +31
-13
lines changed
Expand file tree Collapse file tree 2 files changed +31
-13
lines changed Original file line number Diff line number Diff line change 9191 exit 1
9292fi
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
100100fi
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-
110102echo " Checking C++ formatting..." ;
111103formatting_outputs=$( find tensorflow_quantum/ -iname * .h -o -iname * .cc | xargs clang-format -style=google -output-replacements-xml) ;
112104CFORMATCHECK=0
Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments