{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Execute notebooks\n", "\n", "This notebook executes all phconvert notebooks to automate testing.\n", "\n", "## Requirement\n", "\n", "On top of the phconvert requirements, you need:\n", "\n", "- nbconvert 4.2+.\n", "- pathlib (only on python2)\n", "- download [data files](http://dx.doi.org/10.6084/m9.figshare.1455963) and put then in the phconvert/notebooks folder\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import nbconvert\n", "nbconvert.__version__" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import phconvert as phc\n", "phc.__version__" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import sys\n", "import os\n", "from pathlib import Path # on python2 install pathlib from PyPI\n", "\n", "import nbformat\n", "from nbconvert.preprocessors import ExecutePreprocessor\n", "\n", "\n", "def run_notebook(in_filepath, run_path=None, out_dir=None, out_suffix=''):\n", " \"\"\"Runs the notebook `notebook_name` (file name with no extension).\n", "\n", " This function executes notebook with name `notebook_name` (no extension)\n", " and saves the fully executed notebook in a new file appending \"-out\"\n", " to the original file name.\n", " \"\"\"\n", " if not in_filepath.is_file():\n", " raise IOError('File \"%s\" not found.' % in_filepath)\n", " in_filepath = in_filepath.resolve()\n", " \n", " if run_path is None:\n", " run_path = str(in_filepath.parent)\n", " \n", " if out_dir is None:\n", " out_dir = in_filepath.parent\n", " else:\n", " out_dir = Path(out_dir).resolve()\n", " \n", " out_filepath = Path(out_dir, \"%s%s.ipynb\" % (in_filepath.stem, out_suffix))\n", " \n", " nb = nbformat.read(in_filepath.open(), as_version=4)\n", " ep = ExecutePreprocessor(timeout = 3600, \n", " kernel_name = 'python%d' % sys.version_info[0])\n", "\n", " try:\n", " out = ep.preprocess(nb, {'metadata': {'path': run_path}})\n", " except Exception:\n", " msg = 'Error executing the notebook \"%s\".\\n\\n' % in_filepath\n", " msg += 'See notebook \"%s\" for the traceback.' % out_filepath\n", " print(msg)\n", " raise\n", " finally:\n", " nbformat.write(nb, out_filepath.open(mode='wt'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%pwd" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pathlist = list(f for f in Path('../notebooks').glob('*.ipynb') if not f.name.startswith('_'))\n", "pathlist" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "for nbpath in pathlist:\n", " print(nbpath.stem)\n", " run_notebook(nbpath, out_dir=Path(nbpath.parent, 'out'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "print('OK')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If previous cell prints \"OK\", all the notebooks have been sucessfully executed." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }