import os import subprocess import tempfile import nbformat def _notebook_run(path): """Execute a notebook via nbconvert and collect output. :returns (parsed nb object, execution errors) """ dirname, filename = os.path.split(path) os.chdir(dirname) with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout: args = ["jupyter", "nbconvert", "--to", "notebook", "--execute", "--ExecutePreprocessor.timeout=60", "--output", fout.name, filename] subprocess.check_call(args) fout.seek(0) nb = nbformat.read(fout, 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 def test_ipynb(path): nb, errors = _notebook_run(path) assert errors == []