# 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": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "importing notebook from /Users/minrk/dev/ip/mine/examples/IPython Kernel/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": 11, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'foo'" ] }, "execution_count": 11, "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": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "['Animations Using clear_output.ipynb',\n", " 'Background Jobs.ipynb',\n", " 'Beyond Plain Python.ipynb',\n", " 'Capturing Output.ipynb',\n", " 'Cell Magics.ipynb',\n", " 'Custom Display Logic.ipynb',\n", " 'Importing Notebooks.ipynb',\n", " 'Index.ipynb',\n", " 'Plotting in the Notebook.ipynb',\n", " 'Raw Input in the Notebook.ipynb',\n", " 'Rich Output.ipynb',\n", " 'Script Magics.ipynb',\n", " 'SymPy.ipynb',\n", " 'Terminal Usage.ipynb',\n", " 'Third Party Rich Output.ipynb',\n", " 'Trapezoid Rule.ipynb',\n", " 'Working With External Code.ipynb',\n", " '__pycache__',\n", " 'data',\n", " 'example-demo.py',\n", " 'gui',\n", " 'ipython-completion.bash',\n", " 'ipython-get-history.py',\n", " 'ipython.desktop',\n", " 'nbpackage']" ] }, "execution_count": 12, "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": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "__init__.py \u001b[34m__pycache__\u001b[m\u001b[m/ 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": 14, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
This notebook just defines `bar`\n", "
def bar(x):\n",
" return "bar" * x\n",
"