Skip to content

Instantly share code, notes, and snippets.

@ketch
Last active March 5, 2017 01:17
Show Gist options
  • Save ketch/b499ed74054bb0a9ec8ddb4b1309c7c2 to your computer and use it in GitHub Desktop.
Save ketch/b499ed74054bb0a9ec8ddb4b1309c7c2 to your computer and use it in GitHub Desktop.
This little script executes Jupyter notebooks using either Python 2 or 3 (whichever you run it with) and fails if any exceptions are raised. It's set up to run all notebooks in the current directory. It's borrowed from http://blog.thedataincubator.com/2016/06/testing-jupyter-notebooks/, with some enhancements to work with both versions of Python.
import os
import subprocess
import tempfile
import sys
import nbformat
if sys.version_info >= (3,0):
kernel = 'python3'
else:
kernel = 'python2'
def _notebook_run(path):
"""Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
"--ExecutePreprocessor.timeout=60",
"--ExecutePreprocessor.kernel_name="+kernel,
"--output", fout.name, path]
subprocess.check_call(args)
fout.seek(0)
nb = nbformat.reads(fout.read().decode('utf-8'), nbformat.current_nbformat)
errors = [output for cell in nb.cells if "outputs" in cell
for output in cell["outputs"]
if output.output_type == "error"]
return nb, errors
if __name__ == '__main__':
for filename in os.listdir('.'):
if (filename.split('.')[-1] == 'ipynb'):
nb, errors = _notebook_run(filename)
if errors != []:
raise(Exception)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment