{ "metadata": { "name": "", "signature": "sha256:63d76ae904e96b79a11c540f2dfdd80a7384cecbd0e64042e465346afca52dfe" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "# Source code for the system (must be in one cell)\n", "# See below for documentation\n", "\n", "#NBINCLUDE_STOP\n", "try:\n", " nbinclude\n", "except NameError: # nbinclude is not defined\n", " def nbinclude(nbfile, root=nbinclude_f.name):\n", " global __name__\n", " \n", " import io\n", " import IPython.nbformat.current\n", " import os\n", "\n", " # Find the notebook file\n", " if not nbfile.endswith(\".ipynb\"):\n", " nbfile = nbfile + \".ipynb\"\n", " \n", " if not os.path.isfile(nbfile):\n", " candidate = os.path.join(os.getcwd(), nbfile)\n", " if os.path.isfile(candidate):\n", " nbfile = candidate\n", " else:\n", " candidate = os.path.join(get_ipython().config.NotebookManager.notebook_dir, nbfile)\n", " if os.path.isfile(candidate):\n", " nbfile=candidate\n", " else:\n", " candidate = os.path.join(os.path.dirname(root), nbfile)\n", " if os.path.isfile(candidate):\n", " nbfile = candidate\n", " \n", " if not os.path.isfile(nbfile):\n", " raise IOError, 'Notebook \"{}\" not found'.format(nbfile)\n", "\n", " # Read it\n", " with io.open(nbfile) as f:\n", " nb = IPython.nbformat.current.read(f, 'json')\n", "\n", " # Execute the cells in it\n", " ip = get_ipython()\n", " old_name = __name__\n", " __name__ = os.path.basename(nbfile).split(\".ipynb\")[0].replace(\" \", \"\")\n", " for cell in nb.worksheets[0].cells:\n", " if cell.cell_type != 'code':\n", " continue\n", " inp = cell.input\n", " stop = False\n", " if \"#NBINCLUDE_STOP\" in inp:\n", " inp = inp.split(\"#NBINCLUDE_STOP\")[0]\n", " stop = True\n", " ip.run_cell(inp)\n", " if stop:\n", " break\n", " __name__ = old_name" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Cross-notebook importing system" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This system is designed to let you include an IPython notebook file from another IPython notebook file.\n", "\n", "To set up, first download this `nbinclude.ipynb` file to your system.\n", "\n", "Then add the following shim to the beginning of your notebook files, substituting the path as appropriate." ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Cross-notebook include shim\n", "with open(\"/home/nikita/dev/ipython-notebooks/nbtools/nbinclude.ipynb\") as nbinclude_f: # don't rename nbinclude_f\n", " import IPython.nbformat.current\n", " get_ipython().run_cell(IPython.nbformat.current.read(nbinclude_f, 'json').worksheets[0].cells[0].input)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then you can start including notebooks. The following all work:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# Searches for notebooks relative to the following locations:\n", "# 1. As absolute path\n", "# 2. Relative to the current working directory\n", "# 3. Relative to the default notebook folder\n", "# 4. Relative to the folder that contains nbinclude.ipynb\n", "nbinclude(\"nbinclude.ipynb\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Omitting the \".ipynb\" works, too\n", "nbinclude(\"nbinclude\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# So do absolute paths\n", "nbinclude(\"/home/nikita/dev/ipython-notebooks/nbtools/nbinclude.ipynb\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# And absolute paths that omit the \".ipynb\"\n", "nbinclude(\"/home/nikita/dev/ipython-notebooks/nbtools/nbinclude\")" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To have code run only in the standalone notebook, the classic guard works:" ] }, { "cell_type": "code", "collapsed": false, "input": [ "if __name__ == \"__main__\":\n", " print \"This code will not run if the notebook is imported via nbinclude\"" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is also a magic macro, `#NBINCLUDE_STOP` (exact spelling). The remainder of the cell containing the macro, and any cells after it, will not be read by the nbinclude system.\n", "\n", "This is useful for separating cells at the beginning of the notebook (which contain function definitions you want to include), from cells at the end of the notebook (that you don't want to include)" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }