diff --git a/examples/IPython Kernel/Importing Notebooks.ipynb b/examples/IPython Kernel/Importing Notebooks.ipynb index 44ae0eb..8a0aac7 100644 --- a/examples/IPython Kernel/Importing Notebooks.ipynb +++ b/examples/IPython Kernel/Importing Notebooks.ipynb @@ -22,7 +22,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "collapsed": false }, @@ -33,14 +33,15 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ + "import nbformat\n", + "\n", "from IPython import get_ipython\n", - "from IPython.nbformat import current\n", "from IPython.core.interactiveshell import InteractiveShell" ] }, @@ -56,7 +57,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -110,7 +111,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -126,11 +127,10 @@ " \"\"\"import a notebook as a module\"\"\"\n", " path = find_notebook(fullname, self.path)\n", " \n", - " print (\"importing IPython notebook from %s\" % path)\n", + " print (\"importing notebook from %s\" % path)\n", " \n", " # load the notebook object\n", - " with io.open(path, 'r', encoding='utf-8') as f:\n", - " nb = current.read(f, 'json')\n", + " nb = nbformat.read(path, as_version=4)\n", " \n", " \n", " # create the module and add it to sys.modules\n", @@ -148,10 +148,10 @@ " self.shell.user_ns = mod.__dict__\n", " \n", " try:\n", - " for cell in nb.worksheets[0].cells:\n", - " if cell.cell_type == 'code' and cell.language == 'python':\n", + " for cell in nb.cells:\n", + " if cell.cell_type == 'code':\n", " # transform the input to executable Python\n", - " code = self.shell.input_transformer_manager.transform_cell(cell.input)\n", + " code = self.shell.input_transformer_manager.transform_cell(cell.source)\n", " # run the code in themodule\n", " exec(code, mod.__dict__)\n", " finally:\n", @@ -186,7 +186,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "collapsed": false }, @@ -228,7 +228,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { "collapsed": false }, @@ -248,11 +248,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "__init__.py \u001b[34m__pycache__\u001b[m\u001b[m/ mynotebook.ipynb \u001b[34mnbs\u001b[m\u001b[m/\r\n" + ] + } + ], "source": [ "ls nbpackage" ] @@ -281,11 +289,88 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "\n", + "\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "from pygments import highlight\n", "from pygments.lexers import PythonLexer\n", @@ -307,21 +392,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "

markdown cell

\n", + "
# My Notebook
\n", + "

code cell

\n", + "
def foo():\n",
+       "    return "foo"\n",
+       "
\n", + "\n", + "

code cell

\n", + "
def has_ip_syntax():\n",
+       "    listing = !ls\n",
+       "    return listing\n",
+       "
\n", + "\n", + "

code cell

\n", + "
def whatsmyname():\n",
+       "    return __name__\n",
+       "
\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "def show_notebook(fname):\n", " \"\"\"display a short summary of the cells of a notebook\"\"\"\n", - " with io.open(fname, 'r', encoding='utf-8') as f:\n", - " nb = current.read(f, 'json')\n", + " nb = nbformat.read(fname, as_version=4)\n", " html = []\n", - " for cell in nb.worksheets[0].cells:\n", + " for cell in nb.cells:\n", " html.append(\"

%s cell

\" % cell.cell_type)\n", " if cell.cell_type == 'code':\n", - " html.append(highlight(cell.input, lexer, formatter))\n", + " html.append(highlight(cell.source, lexer, formatter))\n", " else:\n", " html.append(\"
%s
\" % cell.source)\n", " display(HTML('\\n'.join(html)))\n", @@ -341,11 +454,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": { "collapsed": false }, - "outputs": [], + "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" ] @@ -359,11 +480,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'foo'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "mynotebook.foo()" ] @@ -379,11 +511,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": { "collapsed": false }, - "outputs": [], + "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()" ] @@ -405,11 +572,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "__init__.py \u001b[34m__pycache__\u001b[m\u001b[m/ other.ipynb\r\n" + ] + } + ], "source": [ "ls nbpackage/nbs" ] @@ -424,22 +599,58 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/html": [ + "

markdown cell

\n", + "
This notebook just defines `bar`
\n", + "

code cell

\n", + "
def bar(x):\n",
+       "    return "bar" * x\n",
+       "
\n" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], "source": [ "show_notebook(os.path.join(\"nbpackage\", \"nbs\", \"other.ipynb\"))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "importing notebook from /Users/minrk/dev/ip/mine/examples/IPython Kernel/nbpackage/nbs/other.ipynb\n" + ] + }, + { + "data": { + "text/plain": [ + "'barbarbarbarbar'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from nbpackage.nbs import other\n", "other.bar(5)" @@ -456,14 +667,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'/Users/minrk/dev/ip/mine/IPython/utils/inside_ipython.ipynb'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import shutil\n", - "from IPython.utils.path import get_ipython_package_dir\n", + "from IPython.paths import get_ipython_package_dir\n", "\n", "utils = os.path.join(get_ipython_package_dir(), 'utils')\n", "shutil.copy(os.path.join(\"nbpackage\", \"mynotebook.ipynb\"),\n", @@ -480,11 +702,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 17, "metadata": { "collapsed": false }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "importing notebook from /Users/minrk/dev/ip/mine/IPython/utils/inside_ipython.ipynb\n" + ] + }, + { + "data": { + "text/plain": [ + "'IPython.utils.inside_ipython'" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from IPython.utils import inside_ipython\n", "inside_ipython.whatsmyname()"