update Importing Notebooks example to v4
Min RK -
r21638:38c44a41
Not Reviewed
Show More
Add another comment
TODOs: 0 unresolved 0 Resolved
COMMENTS: 0 General 0 Inline
@@ -22,7 +22,7
22 },
22 },
23 {
23 {
24 "cell_type": "code",
24 "cell_type": "code",
25 "execution_count": null,
25 "execution_count": 1,
26 "metadata": {
26 "metadata": {
27 "collapsed": false
27 "collapsed": false
28 },
28 },
@@ -33,14 +33,15
33 },
33 },
34 {
34 {
35 "cell_type": "code",
35 "cell_type": "code",
36 "execution_count": null,
36 "execution_count": 2,
37 "metadata": {
37 "metadata": {
38 "collapsed": false
38 "collapsed": false
39 },
39 },
40 "outputs": [],
40 "outputs": [],
41 "source": [
41 "source": [
42 "import nbformat\n",
43 "\n",
42 "from IPython import get_ipython\n",
44 "from IPython import get_ipython\n",
43 "from IPython.nbformat import current\n",
44 "from IPython.core.interactiveshell import InteractiveShell"
45 "from IPython.core.interactiveshell import InteractiveShell"
45 ]
46 ]
46 },
47 },
@@ -56,7 +57,7
56 },
57 },
57 {
58 {
58 "cell_type": "code",
59 "cell_type": "code",
59 "execution_count": null,
60 "execution_count": 3,
60 "metadata": {
61 "metadata": {
61 "collapsed": false
62 "collapsed": false
62 },
63 },
@@ -110,7 +111,7
110 },
111 },
111 {
112 {
112 "cell_type": "code",
113 "cell_type": "code",
113 "execution_count": null,
114 "execution_count": 4,
114 "metadata": {
115 "metadata": {
115 "collapsed": false
116 "collapsed": false
116 },
117 },
@@ -126,11 +127,10
126 " \"\"\"import a notebook as a module\"\"\"\n",
127 " \"\"\"import a notebook as a module\"\"\"\n",
127 " path = find_notebook(fullname, self.path)\n",
128 " path = find_notebook(fullname, self.path)\n",
128 " \n",
129 " \n",
129 " print (\"importing IPython notebook from %s\" % path)\n",
130 " print (\"importing notebook from %s\" % path)\n",
130 " \n",
131 " \n",
131 " # load the notebook object\n",
132 " # load the notebook object\n",
132 " with io.open(path, 'r', encoding='utf-8') as f:\n",
133 " nb = nbformat.read(path, as_version=4)\n",
133 " nb = current.read(f, 'json')\n",
134 " \n",
134 " \n",
135 " \n",
135 " \n",
136 " # create the module and add it to sys.modules\n",
136 " # create the module and add it to sys.modules\n",
@@ -148,10 +148,10
148 " self.shell.user_ns = mod.__dict__\n",
148 " self.shell.user_ns = mod.__dict__\n",
149 " \n",
149 " \n",
150 " try:\n",
150 " try:\n",
151 " for cell in nb.worksheets[0].cells:\n",
151 " for cell in nb.cells:\n",
152 " if cell.cell_type == 'code' and cell.language == 'python':\n",
152 " if cell.cell_type == 'code':\n",
153 " # transform the input to executable Python\n",
153 " # transform the input to executable Python\n",
154 " code = self.shell.input_transformer_manager.transform_cell(cell.input)\n",
154 " code = self.shell.input_transformer_manager.transform_cell(cell.source)\n",
155 " # run the code in themodule\n",
155 " # run the code in themodule\n",
156 " exec(code, mod.__dict__)\n",
156 " exec(code, mod.__dict__)\n",
157 " finally:\n",
157 " finally:\n",
@@ -186,7 +186,7
186 },
186 },
187 {
187 {
188 "cell_type": "code",
188 "cell_type": "code",
189 "execution_count": null,
189 "execution_count": 5,
190 "metadata": {
190 "metadata": {
191 "collapsed": false
191 "collapsed": false
192 },
192 },
@@ -228,7 +228,7
228 },
228 },
229 {
229 {
230 "cell_type": "code",
230 "cell_type": "code",
231 "execution_count": null,
231 "execution_count": 6,
232 "metadata": {
232 "metadata": {
233 "collapsed": false
233 "collapsed": false
234 },
234 },
@@ -248,11 +248,19
248 },
248 },
249 {
249 {
250 "cell_type": "code",
250 "cell_type": "code",
251 "execution_count": null,
251 "execution_count": 7,
252 "metadata": {
252 "metadata": {
253 "collapsed": false
253 "collapsed": false
254 },
254 },
255 "outputs": [],
255 "outputs": [
256 {
257 "name": "stdout",
258 "output_type": "stream",
259 "text": [
260 "__init__.py \u001b[34m__pycache__\u001b[m\u001b[m/ mynotebook.ipynb \u001b[34mnbs\u001b[m\u001b[m/\r\n"
261 ]
262 }
263 ],
256 "source": [
264 "source": [
257 "ls nbpackage"
265 "ls nbpackage"
258 ]
266 ]
@@ -281,11 +289,88
281 },
289 },
282 {
290 {
283 "cell_type": "code",
291 "cell_type": "code",
284 "execution_count": null,
292 "execution_count": 8,
285 "metadata": {
293 "metadata": {
286 "collapsed": false
294 "collapsed": false
287 },
295 },
288 "outputs": [],
296 "outputs": [
297 {
298 "data": {
299 "text/html": [
300 "\n",
301 "<style type='text/css'>\n",
302 ".hll { background-color: #ffffcc }\n",
303 ".c { color: #408080; font-style: italic } /* Comment */\n",
304 ".err { border: 1px solid #FF0000 } /* Error */\n",
305 ".k { color: #008000; font-weight: bold } /* Keyword */\n",
306 ".o { color: #666666 } /* Operator */\n",
307 ".cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
308 ".cp { color: #BC7A00 } /* Comment.Preproc */\n",
309 ".c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
310 ".cs { color: #408080; font-style: italic } /* Comment.Special */\n",
311 ".gd { color: #A00000 } /* Generic.Deleted */\n",
312 ".ge { font-style: italic } /* Generic.Emph */\n",
313 ".gr { color: #FF0000 } /* Generic.Error */\n",
314 ".gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
315 ".gi { color: #00A000 } /* Generic.Inserted */\n",
316 ".go { color: #888888 } /* Generic.Output */\n",
317 ".gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
318 ".gs { font-weight: bold } /* Generic.Strong */\n",
319 ".gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
320 ".gt { color: #0044DD } /* Generic.Traceback */\n",
321 ".kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
322 ".kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
323 ".kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
324 ".kp { color: #008000 } /* Keyword.Pseudo */\n",
325 ".kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
326 ".kt { color: #B00040 } /* Keyword.Type */\n",
327 ".m { color: #666666 } /* Literal.Number */\n",
328 ".s { color: #BA2121 } /* Literal.String */\n",
329 ".na { color: #7D9029 } /* Name.Attribute */\n",
330 ".nb { color: #008000 } /* Name.Builtin */\n",
331 ".nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
332 ".no { color: #880000 } /* Name.Constant */\n",
333 ".nd { color: #AA22FF } /* Name.Decorator */\n",
334 ".ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
335 ".ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
336 ".nf { color: #0000FF } /* Name.Function */\n",
337 ".nl { color: #A0A000 } /* Name.Label */\n",
338 ".nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
339 ".nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
340 ".nv { color: #19177C } /* Name.Variable */\n",
341 ".ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
342 ".w { color: #bbbbbb } /* Text.Whitespace */\n",
343 ".mb { color: #666666 } /* Literal.Number.Bin */\n",
344 ".mf { color: #666666 } /* Literal.Number.Float */\n",
345 ".mh { color: #666666 } /* Literal.Number.Hex */\n",
346 ".mi { color: #666666 } /* Literal.Number.Integer */\n",
347 ".mo { color: #666666 } /* Literal.Number.Oct */\n",
348 ".sb { color: #BA2121 } /* Literal.String.Backtick */\n",
349 ".sc { color: #BA2121 } /* Literal.String.Char */\n",
350 ".sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
351 ".s2 { color: #BA2121 } /* Literal.String.Double */\n",
352 ".se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
353 ".sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
354 ".si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
355 ".sx { color: #008000 } /* Literal.String.Other */\n",
356 ".sr { color: #BB6688 } /* Literal.String.Regex */\n",
357 ".s1 { color: #BA2121 } /* Literal.String.Single */\n",
358 ".ss { color: #19177C } /* Literal.String.Symbol */\n",
359 ".bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
360 ".vc { color: #19177C } /* Name.Variable.Class */\n",
361 ".vg { color: #19177C } /* Name.Variable.Global */\n",
362 ".vi { color: #19177C } /* Name.Variable.Instance */\n",
363 ".il { color: #666666 } /* Literal.Number.Integer.Long */\n",
364 "</style>\n"
365 ],
366 "text/plain": [
367 "<IPython.core.display.HTML object>"
368 ]
369 },
370 "metadata": {},
371 "output_type": "display_data"
372 }
373 ],
289 "source": [
374 "source": [
290 "from pygments import highlight\n",
375 "from pygments import highlight\n",
291 "from pygments.lexers import PythonLexer\n",
376 "from pygments.lexers import PythonLexer\n",
@@ -307,21 +392,49
307 },
392 },
308 {
393 {
309 "cell_type": "code",
394 "cell_type": "code",
310 "execution_count": null,
395 "execution_count": 9,
311 "metadata": {
396 "metadata": {
312 "collapsed": false
397 "collapsed": false
313 },
398 },
314 "outputs": [],
399 "outputs": [
400 {
401 "data": {
402 "text/html": [
403 "<h4>markdown cell</h4>\n",
404 "<pre># My Notebook</pre>\n",
405 "<h4>code cell</h4>\n",
406 "<div class=\"highlight\"><pre><span class=\"k\">def</span> <span class=\"nf\">foo</span><span class=\"p\">():</span>\n",
407 " <span class=\"k\">return</span> <span class=\"s\">&quot;foo&quot;</span>\n",
408 "</pre></div>\n",
409 "\n",
410 "<h4>code cell</h4>\n",
411 "<div class=\"highlight\"><pre><span class=\"k\">def</span> <span class=\"nf\">has_ip_syntax</span><span class=\"p\">():</span>\n",
412 " <span class=\"n\">listing</span> <span class=\"o\">=</span> <span class=\"err\">!</span><span class=\"n\">ls</span>\n",
413 " <span class=\"k\">return</span> <span class=\"n\">listing</span>\n",
414 "</pre></div>\n",
415 "\n",
416 "<h4>code cell</h4>\n",
417 "<div class=\"highlight\"><pre><span class=\"k\">def</span> <span class=\"nf\">whatsmyname</span><span class=\"p\">():</span>\n",
418 " <span class=\"k\">return</span> <span class=\"n\">__name__</span>\n",
419 "</pre></div>\n"
420 ],
421 "text/plain": [
422 "<IPython.core.display.HTML object>"
423 ]
424 },
425 "metadata": {},
426 "output_type": "display_data"
427 }
428 ],
315 "source": [
429 "source": [
316 "def show_notebook(fname):\n",
430 "def show_notebook(fname):\n",
317 " \"\"\"display a short summary of the cells of a notebook\"\"\"\n",
431 " \"\"\"display a short summary of the cells of a notebook\"\"\"\n",
318 " with io.open(fname, 'r', encoding='utf-8') as f:\n",
432 " nb = nbformat.read(fname, as_version=4)\n",
319 " nb = current.read(f, 'json')\n",
320 " html = []\n",
433 " html = []\n",
321 " for cell in nb.worksheets[0].cells:\n",
434 " for cell in nb.cells:\n",
322 " html.append(\"<h4>%s cell</h4>\" % cell.cell_type)\n",
435 " html.append(\"<h4>%s cell</h4>\" % cell.cell_type)\n",
323 " if cell.cell_type == 'code':\n",
436 " if cell.cell_type == 'code':\n",
324 " html.append(highlight(cell.input, lexer, formatter))\n",
437 " html.append(highlight(cell.source, lexer, formatter))\n",
325 " else:\n",
438 " else:\n",
326 " html.append(\"<pre>%s</pre>\" % cell.source)\n",
439 " html.append(\"<pre>%s</pre>\" % cell.source)\n",
327 " display(HTML('\\n'.join(html)))\n",
440 " display(HTML('\\n'.join(html)))\n",
@@ -341,11 +454,19
341 },
454 },
342 {
455 {
343 "cell_type": "code",
456 "cell_type": "code",
344 "execution_count": null,
457 "execution_count": 10,
345 "metadata": {
458 "metadata": {
346 "collapsed": false
459 "collapsed": false
347 },
460 },
348 "outputs": [],
461 "outputs": [
462 {
463 "name": "stdout",
464 "output_type": "stream",
465 "text": [
466 "importing notebook from /Users/minrk/dev/ip/mine/examples/IPython Kernel/nbpackage/mynotebook.ipynb\n"
467 ]
468 }
469 ],
349 "source": [
470 "source": [
350 "from nbpackage import mynotebook"
471 "from nbpackage import mynotebook"
351 ]
472 ]
@@ -359,11 +480,22
359 },
480 },
360 {
481 {
361 "cell_type": "code",
482 "cell_type": "code",
362 "execution_count": null,
483 "execution_count": 11,
363 "metadata": {
484 "metadata": {
364 "collapsed": false
485 "collapsed": false
365 },
486 },
366 "outputs": [],
487 "outputs": [
488 {
489 "data": {
490 "text/plain": [
491 "'foo'"
492 ]
493 },
494 "execution_count": 11,
495 "metadata": {},
496 "output_type": "execute_result"
497 }
498 ],
367 "source": [
499 "source": [
368 "mynotebook.foo()"
500 "mynotebook.foo()"
369 ]
501 ]
@@ -379,11 +511,46
379 },
511 },
380 {
512 {
381 "cell_type": "code",
513 "cell_type": "code",
382 "execution_count": null,
514 "execution_count": 12,
383 "metadata": {
515 "metadata": {
384 "collapsed": false
516 "collapsed": false
385 },
517 },
386 "outputs": [],
518 "outputs": [
519 {
520 "data": {
521 "text/plain": [
522 "['Animations Using clear_output.ipynb',\n",
523 " 'Background Jobs.ipynb',\n",
524 " 'Beyond Plain Python.ipynb',\n",
525 " 'Capturing Output.ipynb',\n",
526 " 'Cell Magics.ipynb',\n",
527 " 'Custom Display Logic.ipynb',\n",
528 " 'Importing Notebooks.ipynb',\n",
529 " 'Index.ipynb',\n",
530 " 'Plotting in the Notebook.ipynb',\n",
531 " 'Raw Input in the Notebook.ipynb',\n",
532 " 'Rich Output.ipynb',\n",
533 " 'Script Magics.ipynb',\n",
534 " 'SymPy.ipynb',\n",
535 " 'Terminal Usage.ipynb',\n",
536 " 'Third Party Rich Output.ipynb',\n",
537 " 'Trapezoid Rule.ipynb',\n",
538 " 'Working With External Code.ipynb',\n",
539 " '__pycache__',\n",
540 " 'data',\n",
541 " 'example-demo.py',\n",
542 " 'gui',\n",
543 " 'ipython-completion.bash',\n",
544 " 'ipython-get-history.py',\n",
545 " 'ipython.desktop',\n",
546 " 'nbpackage']"
547 ]
548 },
549 "execution_count": 12,
550 "metadata": {},
551 "output_type": "execute_result"
552 }
553 ],
387 "source": [
554 "source": [
388 "mynotebook.has_ip_syntax()"
555 "mynotebook.has_ip_syntax()"
389 ]
556 ]
@@ -405,11 +572,19
405 },
572 },
406 {
573 {
407 "cell_type": "code",
574 "cell_type": "code",
408 "execution_count": null,
575 "execution_count": 13,
409 "metadata": {
576 "metadata": {
410 "collapsed": false
577 "collapsed": false
411 },
578 },
412 "outputs": [],
579 "outputs": [
580 {
581 "name": "stdout",
582 "output_type": "stream",
583 "text": [
584 "__init__.py \u001b[34m__pycache__\u001b[m\u001b[m/ other.ipynb\r\n"
585 ]
586 }
587 ],
413 "source": [
588 "source": [
414 "ls nbpackage/nbs"
589 "ls nbpackage/nbs"
415 ]
590 ]
@@ -424,22 +599,58
424 },
599 },
425 {
600 {
426 "cell_type": "code",
601 "cell_type": "code",
427 "execution_count": null,
602 "execution_count": 14,
428 "metadata": {
603 "metadata": {
429 "collapsed": false
604 "collapsed": false
430 },
605 },
431 "outputs": [],
606 "outputs": [
607 {
608 "data": {
609 "text/html": [
610 "<h4>markdown cell</h4>\n",
611 "<pre>This notebook just defines `bar`</pre>\n",
612 "<h4>code cell</h4>\n",
613 "<div class=\"highlight\"><pre><span class=\"k\">def</span> <span class=\"nf\">bar</span><span class=\"p\">(</span><span class=\"n\">x</span><span class=\"p\">):</span>\n",
614 " <span class=\"k\">return</span> <span class=\"s\">&quot;bar&quot;</span> <span class=\"o\">*</span> <span class=\"n\">x</span>\n",
615 "</pre></div>\n"
616 ],
617 "text/plain": [
618 "<IPython.core.display.HTML object>"
619 ]
620 },
621 "metadata": {},
622 "output_type": "display_data"
623 }
624 ],
432 "source": [
625 "source": [
433 "show_notebook(os.path.join(\"nbpackage\", \"nbs\", \"other.ipynb\"))"
626 "show_notebook(os.path.join(\"nbpackage\", \"nbs\", \"other.ipynb\"))"
434 ]
627 ]
435 },
628 },
436 {
629 {
437 "cell_type": "code",
630 "cell_type": "code",
438 "execution_count": null,
631 "execution_count": 15,
439 "metadata": {
632 "metadata": {
440 "collapsed": false
633 "collapsed": false
441 },
634 },
442 "outputs": [],
635 "outputs": [
636 {
637 "name": "stdout",
638 "output_type": "stream",
639 "text": [
640 "importing notebook from /Users/minrk/dev/ip/mine/examples/IPython Kernel/nbpackage/nbs/other.ipynb\n"
641 ]
642 },
643 {
644 "data": {
645 "text/plain": [
646 "'barbarbarbarbar'"
647 ]
648 },
649 "execution_count": 15,
650 "metadata": {},
651 "output_type": "execute_result"
652 }
653 ],
443 "source": [
654 "source": [
444 "from nbpackage.nbs import other\n",
655 "from nbpackage.nbs import other\n",
445 "other.bar(5)"
656 "other.bar(5)"
@@ -456,14 +667,25
456 },
667 },
457 {
668 {
458 "cell_type": "code",
669 "cell_type": "code",
459 "execution_count": null,
670 "execution_count": 16,
460 "metadata": {
671 "metadata": {
461 "collapsed": false
672 "collapsed": false
462 },
673 },
463 "outputs": [],
674 "outputs": [
675 {
676 "data": {
677 "text/plain": [
678 "'/Users/minrk/dev/ip/mine/IPython/utils/inside_ipython.ipynb'"
679 ]
680 },
681 "execution_count": 16,
682 "metadata": {},
683 "output_type": "execute_result"
684 }
685 ],
464 "source": [
686 "source": [
465 "import shutil\n",
687 "import shutil\n",
466 "from IPython.utils.path import get_ipython_package_dir\n",
688 "from IPython.paths import get_ipython_package_dir\n",
467 "\n",
689 "\n",
468 "utils = os.path.join(get_ipython_package_dir(), 'utils')\n",
690 "utils = os.path.join(get_ipython_package_dir(), 'utils')\n",
469 "shutil.copy(os.path.join(\"nbpackage\", \"mynotebook.ipynb\"),\n",
691 "shutil.copy(os.path.join(\"nbpackage\", \"mynotebook.ipynb\"),\n",
@@ -480,11 +702,29
480 },
702 },
481 {
703 {
482 "cell_type": "code",
704 "cell_type": "code",
483 "execution_count": null,
705 "execution_count": 17,
484 "metadata": {
706 "metadata": {
485 "collapsed": false
707 "collapsed": false
486 },
708 },
487 "outputs": [],
709 "outputs": [
710 {
711 "name": "stdout",
712 "output_type": "stream",
713 "text": [
714 "importing notebook from /Users/minrk/dev/ip/mine/IPython/utils/inside_ipython.ipynb\n"
715 ]
716 },
717 {
718 "data": {
719 "text/plain": [
720 "'IPython.utils.inside_ipython'"
721 ]
722 },
723 "execution_count": 17,
724 "metadata": {},
725 "output_type": "execute_result"
726 }
727 ],
488 "source": [
728 "source": [
489 "from IPython.utils import inside_ipython\n",
729 "from IPython.utils import inside_ipython\n",
490 "inside_ipython.whatsmyname()"
730 "inside_ipython.whatsmyname()"
Comments 0
You need to be logged in to leave comments. Login now