|
| 1 | +import glob |
| 2 | +import os |
| 3 | +import time |
| 4 | + |
| 5 | +import nbformat |
| 6 | +from nbconvert.preprocessors import ExecutePreprocessor |
| 7 | + |
| 8 | +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 9 | +NOTEBOOK_DIR = os.path.join(BASE_DIR, 'docs', 'source', 'notebooks') |
| 10 | + |
| 11 | +def list_notebooks(): |
| 12 | + """Get an iterator of filepaths to notebooks in NOTEBOOK_DIR""" |
| 13 | + return glob.glob(os.path.join(NOTEBOOK_DIR, '*.ipynb')) |
| 14 | + |
| 15 | +def execute_notebook(notebook_path): |
| 16 | + """Run and overwrite a notebook file.""" |
| 17 | + ep = ExecutePreprocessor(timeout=-1) |
| 18 | + with open(notebook_path, 'r') as buff: |
| 19 | + nb = nbformat.read(buff, as_version=nbformat.NO_CONVERT) |
| 20 | + try: |
| 21 | + t0 = time.time() |
| 22 | + _ = ep.preprocess(nb, {'metadata': {'path': NOTEBOOK_DIR}}) |
| 23 | + t1 = time.time() |
| 24 | + |
| 25 | + except BaseException as e: |
| 26 | + t1 = time.time() |
| 27 | + return False, 'Failed after {:.1f}s:\n{}'.format(t1 - t0, str(e)) |
| 28 | + |
| 29 | + with open(notebook_path, 'w') as buff: |
| 30 | + nbformat.write(nb, buff) |
| 31 | + |
| 32 | + return True, 'Succeeded after {:.1f}s'.format(t1 - t0) |
| 33 | + |
| 34 | +def run_all_notebooks(): |
| 35 | + """Tries to re-run all notebooks. Prints failures at end.""" |
| 36 | + failed = [] |
| 37 | + for notebook_path in list_notebooks(): |
| 38 | + print('Executing {}'.format(os.path.basename(notebook_path))) |
| 39 | + success, message = execute_notebook(notebook_path) |
| 40 | + if not success: |
| 41 | + failed.append(os.path.basename(notebook_path)) |
| 42 | + print(message) |
| 43 | + |
| 44 | + if failed: |
| 45 | + print('The following notebooks had errors!') |
| 46 | + print('\n'.join(failed)) |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == '__main__': |
| 51 | + run_all_notebooks() |
0 commit comments