{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "This notebook is part of the `nbsphinx` documentation: http://nbsphinx.readthedocs.io/." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Pre-Executing Notebooks\n", "\n", "Automatically executing notebooks during the Sphinx build process is an important feature of `nbsphinx`.\n", "However, there are a few use cases where pre-executing a notebook and storing the outputs might be preferable.\n", "Storing any output will, by default, stop ``nbsphinx`` from executing the notebook." ] }, { "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 to 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": {}, "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": [ "If you *do* want to execute your notebooks, but some cells run for a long time, you can change the timeout, see [Cell Execution Timeout](timeout.ipynb)." ] }, { "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": {}, "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": {}, "outputs": [ { "ename": "ZeroDivisionError", "evalue": "division by zero", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m1\u001b[0m \u001b[0;34m/\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;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.6.3" }, "widgets": { "state": {}, "version": "0.2.0" } }, "nbformat": 4, "nbformat_minor": 1 }