-
-
Save AliMirlou/11143323b2bbfe3f5207c63bdc31db00 to your computer and use it in GitHub Desktop.
Run and validate an IPython Notebook non-interactively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Simple example script for running notebooks and reporting exceptions. | |
Usage: `run-ipynb.py foo.ipynb [bar.ipynb [...]]` | |
Each cell is submitted to the kernel, and checked for errors. | |
""" | |
import os | |
import sys | |
from jupyter_client.manager import KernelManager | |
from nbformat import read, current_nbformat | |
def run_notebook(nb): | |
km = KernelManager() | |
km.start_kernel(stderr=open(os.devnull, 'w')) | |
kc = km.client() | |
kc.start_channels() | |
cells = failures = 0 | |
for cell in nb.cells: | |
if cell.cell_type != 'code': | |
continue | |
kc.execute(cell.source) | |
# Wait to finish, maximum for 1 hour | |
try: | |
reply = kc.get_shell_msg(timeout=3600)['content'] | |
except Empty: | |
reply = {'status': 'error', 'traceback': ["Cell execution timed out!"]} | |
if reply['status'] == 'error': | |
failures += 1 | |
print("\nFAILURE:") | |
print(cell.source) | |
print('-----') | |
print("Raised:") | |
print('\n'.join(reply['traceback'])) | |
cells += 1 | |
sys.stdout.write('.') | |
kc.stop_channels() | |
km.shutdown_kernel() | |
return cells, failures | |
if __name__ == '__main__': | |
for ipynb in sys.argv[1:]: | |
print("Running %s..." % ipynb) | |
nb = read(ipynb, current_nbformat) | |
cells, failures = run_notebook(nb) | |
print('\nRan notebook "%s":' % ipynb) | |
print(" Cells ran: %i" % cells) | |
print(" Cells raised exception: %i" % failures) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment