Created
March 19, 2016 18:44
-
-
Save grigorisg9gr/b4f4713a42872e23fbe3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# grigoris, 20/3/2016: testing notebooks\n", | |
"# from nbformat import reads\n", | |
"from nbformat.current import reads\n", | |
"from nbformat import NotebookNode\n", | |
"from jupyter_client import KernelManager\n", | |
"from Queue import Empty\n", | |
"import os" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"def test_notebook(nb): \n", | |
" km = KernelManager()\n", | |
" # put pylab inline if we wish to execute with the \n", | |
" # respective %matplotlib inline command, otherwise ea = [].\n", | |
" ea = '--pylab=inline'\n", | |
" km.start_kernel(extra_arguments=[ea], stderr=open(os.devnull, 'w'))\n", | |
" kc = km.client()\n", | |
" kc.start_channels()\n", | |
" # execute with pass\n", | |
" kc.execute(\"pass\")\n", | |
" kc.get_shell_msg()\n", | |
" kc.get_shell_msg()\n", | |
" \n", | |
" successes = 0\n", | |
" errors = 0\n", | |
" err_msg = []\n", | |
" for ws in nb.worksheets:\n", | |
" for cell in ws.cells:\n", | |
" if cell.cell_type != 'code':\n", | |
" # consider only code cells.\n", | |
" continue\n", | |
" try:\n", | |
" try:\n", | |
" kc.execute(cell.input)\n", | |
" # get the execution json result.\n", | |
" res = kc.get_shell_msg(timeout=8)\n", | |
" except Empty:\n", | |
" err_msg.append('The input {} is Empty.'.format(cell.input))\n", | |
" continue\n", | |
"\n", | |
" # get the status of the executed cell\n", | |
" stat = res['content']['status']\n", | |
" if stat == 'ok':\n", | |
" successes += 1\n", | |
" elif stat == 'error': # or stat == 'aborted':\n", | |
" err_msg.append(res['content'])\n", | |
" errors += 1\n", | |
" else:\n", | |
" err_msg.append('Unrecognised status on {}'.format(cell.input))\n", | |
" raise ValueError()\n", | |
" \n", | |
" except Exception as e:\n", | |
" err_msg.append('failed to run cell: {}'.format(repr(e)))\n", | |
" errors += 1\n", | |
" continue\n", | |
"\n", | |
" kc.stop_channels()\n", | |
" km.shutdown_kernel()\n", | |
" del km\n", | |
"\n", | |
" return successes, errors, err_msg\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"ipynb = '../menpowidgets_testing.ipynb'\n", | |
"with open(ipynb) as f:\n", | |
" nb = reads(f.read(), 'json')\n", | |
"test_notebook(nb)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 2", | |
"language": "python", | |
"name": "python2" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 2 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.11" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Testing a notebook: The output is in the format of (successes, failures, failure_messages).
The idea comes from https://gist.github.com/minrk/2620735 , however these are quite obsolete and non-functional. So, this is a functional simple version of testing out a notebook.