My Notebook\n", "
def foo():\n",
" return "foo"\n",
"
def has_ip_syntax():\n",
" listing = !ls\n",
" return listing\n",
"
def whatsmyname():\n",
" return __name__\n",
"
%s\" % cell.source)\n", " display(HTML('\\n'.join(html)))\n", "\n", "show_notebook(os.path.join(\"nbpackage\", \"mynotebook.ipynb\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So my notebook has a heading cell and some code cells,\n", "one of which contains some IPython syntax.\n", "\n", "Let's see what happens when we import it" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "importing IPython notebook from nbpackage/mynotebook.ipynb\n" ] } ], "source": [ "from nbpackage import mynotebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hooray, it imported! Does it work?" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'foo'" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mynotebook.foo()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Hooray again!\n", "\n", "Even the function that contains IPython syntax works:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['Animations Using clear_output.ipynb',\n", " 'Connecting with the Qt Console.ipynb',\n", " 'Importing Notebooks.ipynb',\n", " 'Progress Bars.ipynb',\n", " 'Raw Input.ipynb',\n", " 'SymPy.ipynb',\n", " 'Trapezoid Rule.ipynb',\n", " 'nbpackage']" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mynotebook.has_ip_syntax()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Notebooks in packages" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We also have a notebook inside the `nb` package,\n", "so let's make sure that works as well." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "__init__.py other.ipynb\r\n" ] } ], "source": [ "ls nbpackage/nbs" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the `__init__.py` is necessary for `nb` to be considered a package,\n", "just like usual." ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
This notebook just defines `bar`\n", "
def bar(x):\n",
" return "bar" * x\n",
"