{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "This notebook is part of the `nbsphinx` documentation: http://nbsphinx.readthedocs.org/." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# A Pre-Executed Notebook\n", "\n", "Notebooks with no outputs are automatically executed during the Sphinx build process.\n", "If, however, there is at least one output cell present, the notebook is not evaluated and included as is.\n", "\n", "This can be useful for the following use cases." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Long-Running Cells\n", "\n", "If you are doing some very time-consuming computations, it might not be feasible to re-execute the notebook every time you build your Sphinx documentation.\n", "\n", "So just do it once - when you happen have the time - and then just keep the output." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import time" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 160 ms, sys: 56 ms, total: 216 ms\n", "Wall time: 1h 1s\n" ] }, { "data": { "text/plain": [ "42" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%time time.sleep(60 * 60)\n", "6 * 7" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Rare Libraries\n", "\n", "You might have created results with a library that's hard to install and therefore you have only managed to install it on one very old computer in the basement, so you probably cannot run this whenever you build your Sphinx docs." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from a_very_rare_library import calculate_the_answer" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "calculate_the_answer()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exceptions\n", "\n", "If an exception is raised during the Sphinx build process, it is stopped (the build process, not the exception!).\n", "If you want to show to your audience how an exception looks like, you have two choices:\n", "\n", "1. Allow errors -- either generally or on a per-notebook basis -- see [Ignoring Errors](allow-errors.ipynb).\n", "\n", "1. Execute the notebook beforehand and save the results, like it's done in this example notebook:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "division by zero", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;36m1\u001b[0m \u001b[1;33m/\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mZeroDivisionError\u001b[0m: division by zero" ] } ], "source": [ "1 / 0" ] } ], "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 }