##// END OF EJS Templates
add Importing Notebooks example
MinRK -
Show More
This diff has been collapsed as it changes many lines, (796 lines changed) Show them Hide them
@@ -0,0 +1,796 b''
1 {
2 "metadata": {
3 "gist_id": "6011986",
4 "name": ""
5 },
6 "nbformat": 3,
7 "nbformat_minor": 0,
8 "worksheets": [
9 {
10 "cells": [
11 {
12 "cell_type": "heading",
13 "level": 1,
14 "metadata": {},
15 "source": [
16 "Importing IPython Notebooks as Modules"
17 ]
18 },
19 {
20 "cell_type": "markdown",
21 "metadata": {},
22 "source": [
23 "It is a common problem that people want to import code from IPython Notebooks.\n",
24 "This is made difficult by the fact that Notebooks are not plain Python files,\n",
25 "and thus cannot be imported by the regular Python machinery.\n",
26 "\n",
27 "There is a flag in the notebook server that provides a certain workaround for a small set of cases,\n",
28 "but I think it's gross so I won't even discuss it.\n",
29 "\n",
30 "Fortunately, Python provides some fairly sophisticated [hooks](http://www.python.org/dev/peps/pep-0302/) into the import machinery,\n",
31 "so we can actually make IPython notebooks importable without much difficulty,\n",
32 "and only using public APIs.\n",
33 "\n",
34 "Forgive me if some of this is gross or wrong,\n",
35 "I haven't really written import hooks before."
36 ]
37 },
38 {
39 "cell_type": "code",
40 "collapsed": false,
41 "input": [
42 "import io, os, sys, types"
43 ],
44 "language": "python",
45 "metadata": {},
46 "outputs": [],
47 "prompt_number": 1
48 },
49 {
50 "cell_type": "code",
51 "collapsed": false,
52 "input": [
53 "from IPython.nbformat import current\n",
54 "from IPython.core.interactiveshell import InteractiveShell"
55 ],
56 "language": "python",
57 "metadata": {},
58 "outputs": [],
59 "prompt_number": 2
60 },
61 {
62 "cell_type": "markdown",
63 "metadata": {},
64 "source": [
65 "Import hooks typically take the form of two objects:\n",
66 "\n",
67 "1. a Module **Loader**, which takes a module name (e.g. `'IPython.display'`), and returns a Module\n",
68 "2. a Module **Finder**, which figures out whether a module might exist, and tells Python what **Loader** to use"
69 ]
70 },
71 {
72 "cell_type": "code",
73 "collapsed": false,
74 "input": [
75 "def find_notebook(fullname, path=None):\n",
76 " \"\"\"find a notebook, given its fully qualified name and an optional path\n",
77 " \n",
78 " This turns \"foo.bar\" into \"foo/bar.ipynb\"\n",
79 " and tries turning \"Foo_Bar\" into \"Foo Bar\" if Foo_Bar\n",
80 " does not exist.\n",
81 " \"\"\"\n",
82 " name = fullname.rsplit('.', 1)[-1]\n",
83 " if not path:\n",
84 " path = ['']\n",
85 " for d in path:\n",
86 " nb_path = os.path.join(d, name + \".ipynb\")\n",
87 " if os.path.isfile(nb_path):\n",
88 " return nb_path\n",
89 " # let import Notebook_Name find \"Notebook Name.ipynb\"\n",
90 " nb_path = nb_path.replace(\"_\", \" \")\n",
91 " if os.path.isfile(nb_path):\n",
92 " return nb_path\n",
93 " "
94 ],
95 "language": "python",
96 "metadata": {},
97 "outputs": [],
98 "prompt_number": 3
99 },
100 {
101 "cell_type": "heading",
102 "level": 2,
103 "metadata": {},
104 "source": [
105 "Notebook Loader"
106 ]
107 },
108 {
109 "cell_type": "markdown",
110 "metadata": {},
111 "source": [
112 "Here we have our Notebook Loader.\n",
113 "It's actually quite simple - once we figure out the filename of the module,\n",
114 "all it does is:\n",
115 "\n",
116 "1. load the notebook document into memory\n",
117 "2. create an empty Module\n",
118 "3. execute every cell in the Module namespace\n",
119 "\n",
120 "Since IPython cells can have extended syntax,\n",
121 "the IPython transform is applied to turn each of these cells into their pure-Python counterparts before executing them.\n",
122 "If all of your notebook cells are pure-Python,\n",
123 "this step is unnecessary."
124 ]
125 },
126 {
127 "cell_type": "code",
128 "collapsed": false,
129 "input": [
130 "class NotebookLoader(object):\n",
131 " \"\"\"Module Loader for IPython Notebooks\"\"\"\n",
132 " def __init__(self, path=None):\n",
133 " self.shell = InteractiveShell.instance()\n",
134 " self.path = path\n",
135 " \n",
136 " def load_module(self, fullname):\n",
137 " \"\"\"import a notebook as a module\"\"\"\n",
138 " path = find_notebook(fullname, self.path)\n",
139 " \n",
140 " print (\"importing IPython notebook from %s\" % path)\n",
141 " \n",
142 " # load the notebook object\n",
143 " with io.open(path, 'r', encoding='utf-8') as f:\n",
144 " nb = current.read(f, 'json')\n",
145 " \n",
146 " \n",
147 " # create the module and add it to sys.modules\n",
148 " # if name in sys.modules:\n",
149 " # return sys.modules[name]\n",
150 " mod = types.ModuleType(fullname)\n",
151 " mod.__file__ = path\n",
152 " mod.__loader__ = self\n",
153 " sys.modules[fullname] = mod\n",
154 " \n",
155 " # extra work to ensure that magics that would affect the user_ns\n",
156 " # actually affect the notebook module's ns\n",
157 " save_user_ns = self.shell.user_ns\n",
158 " self.shell.user_ns = mod.__dict__\n",
159 " \n",
160 " try:\n",
161 " for cell in nb.worksheets[0].cells:\n",
162 " if cell.cell_type == 'code' and cell.language == 'python':\n",
163 " # transform the input to executable Python\n",
164 " code = self.shell.input_transformer_manager.transform_cell(cell.input)\n",
165 " # run the code in themodule\n",
166 " exec code in mod.__dict__\n",
167 " finally:\n",
168 " self.shell.user_ns = save_user_ns\n",
169 " return mod\n"
170 ],
171 "language": "python",
172 "metadata": {},
173 "outputs": [],
174 "prompt_number": 4
175 },
176 {
177 "cell_type": "heading",
178 "level": 2,
179 "metadata": {},
180 "source": [
181 "The Module Finder"
182 ]
183 },
184 {
185 "cell_type": "markdown",
186 "metadata": {},
187 "source": [
188 "The finder is a simple object that tells you whether a name can be imported,\n",
189 "and returns the appropriate loader.\n",
190 "All this one does is check, when you do:\n",
191 "\n",
192 "```python\n",
193 "import mynotebook\n",
194 "```\n",
195 "\n",
196 "it checks whether `mynotebook.ipynb` exists.\n",
197 "If a notebook is found, then it returns a NotebookLoader.\n",
198 "\n",
199 "Any extra logic is just for resolving paths within packages."
200 ]
201 },
202 {
203 "cell_type": "code",
204 "collapsed": false,
205 "input": [
206 "class NotebookFinder(object):\n",
207 " \"\"\"Module finder that locates IPython Notebooks\"\"\"\n",
208 " def __init__(self):\n",
209 " self.loaders = {}\n",
210 " \n",
211 " def find_module(self, fullname, path=None):\n",
212 " nb_path = find_notebook(fullname, path)\n",
213 " if not nb_path:\n",
214 " return\n",
215 " \n",
216 " key = path\n",
217 " if path:\n",
218 " # lists aren't hashable\n",
219 " key = os.path.sep.join(path)\n",
220 " \n",
221 " if key not in self.loaders:\n",
222 " self.loaders[key] = NotebookLoader(path)\n",
223 " return self.loaders[key]\n"
224 ],
225 "language": "python",
226 "metadata": {},
227 "outputs": [],
228 "prompt_number": 5
229 },
230 {
231 "cell_type": "heading",
232 "level": 2,
233 "metadata": {},
234 "source": [
235 "Register the hook"
236 ]
237 },
238 {
239 "cell_type": "markdown",
240 "metadata": {},
241 "source": [
242 "Now we register the `NotebookFinder` with `sys.meta_path`"
243 ]
244 },
245 {
246 "cell_type": "code",
247 "collapsed": false,
248 "input": [
249 "sys.meta_path.append(NotebookFinder())"
250 ],
251 "language": "python",
252 "metadata": {},
253 "outputs": [],
254 "prompt_number": 6
255 },
256 {
257 "cell_type": "markdown",
258 "metadata": {},
259 "source": [
260 "After this point, my notebooks should be importable.\n",
261 "\n",
262 "Let's look at what we have in the CWD:"
263 ]
264 },
265 {
266 "cell_type": "code",
267 "collapsed": false,
268 "input": [
269 "ls nbimp"
270 ],
271 "language": "python",
272 "metadata": {},
273 "outputs": [
274 {
275 "output_type": "stream",
276 "stream": "stdout",
277 "text": [
278 "__init__.py __init__.pyc bs.ipynb mynotebook.ipynb \u001b[34mnbs\u001b[m\u001b[m/\r\n"
279 ]
280 }
281 ],
282 "prompt_number": 7
283 },
284 {
285 "cell_type": "markdown",
286 "metadata": {},
287 "source": [
288 "So I should be able to `import nbimp.mynotebook`.\n"
289 ]
290 },
291 {
292 "cell_type": "heading",
293 "level": 3,
294 "metadata": {},
295 "source": [
296 "Aside: displaying notebooks"
297 ]
298 },
299 {
300 "cell_type": "markdown",
301 "metadata": {},
302 "source": [
303 "Here is some simple code to display the contents of a notebook\n",
304 "with syntax highlighting, etc."
305 ]
306 },
307 {
308 "cell_type": "code",
309 "collapsed": false,
310 "input": [
311 "from pygments import highlight\n",
312 "from pygments.lexers import PythonLexer\n",
313 "from pygments.formatters import HtmlFormatter\n",
314 "\n",
315 "from IPython.display import display, HTML\n",
316 "\n",
317 "formatter = HtmlFormatter()\n",
318 "lexer = PythonLexer()\n",
319 "\n",
320 "# publish the CSS for pygments highlighting\n",
321 "display(HTML(\"\"\"\n",
322 "<style type='text/css'>\n",
323 "%s\n",
324 "</style>\n",
325 "\"\"\" % formatter.get_style_defs()\n",
326 "))"
327 ],
328 "language": "python",
329 "metadata": {},
330 "outputs": [
331 {
332 "html": [
333 "\n",
334 "<style type='text/css'>\n",
335 ".hll { background-color: #ffffcc }\n",
336 ".c { color: #408080; font-style: italic } /* Comment */\n",
337 ".err { border: 1px solid #FF0000 } /* Error */\n",
338 ".k { color: #008000; font-weight: bold } /* Keyword */\n",
339 ".o { color: #666666 } /* Operator */\n",
340 ".cm { color: #408080; font-style: italic } /* Comment.Multiline */\n",
341 ".cp { color: #BC7A00 } /* Comment.Preproc */\n",
342 ".c1 { color: #408080; font-style: italic } /* Comment.Single */\n",
343 ".cs { color: #408080; font-style: italic } /* Comment.Special */\n",
344 ".gd { color: #A00000 } /* Generic.Deleted */\n",
345 ".ge { font-style: italic } /* Generic.Emph */\n",
346 ".gr { color: #FF0000 } /* Generic.Error */\n",
347 ".gh { color: #000080; font-weight: bold } /* Generic.Heading */\n",
348 ".gi { color: #00A000 } /* Generic.Inserted */\n",
349 ".go { color: #888888 } /* Generic.Output */\n",
350 ".gp { color: #000080; font-weight: bold } /* Generic.Prompt */\n",
351 ".gs { font-weight: bold } /* Generic.Strong */\n",
352 ".gu { color: #800080; font-weight: bold } /* Generic.Subheading */\n",
353 ".gt { color: #0044DD } /* Generic.Traceback */\n",
354 ".kc { color: #008000; font-weight: bold } /* Keyword.Constant */\n",
355 ".kd { color: #008000; font-weight: bold } /* Keyword.Declaration */\n",
356 ".kn { color: #008000; font-weight: bold } /* Keyword.Namespace */\n",
357 ".kp { color: #008000 } /* Keyword.Pseudo */\n",
358 ".kr { color: #008000; font-weight: bold } /* Keyword.Reserved */\n",
359 ".kt { color: #B00040 } /* Keyword.Type */\n",
360 ".m { color: #666666 } /* Literal.Number */\n",
361 ".s { color: #BA2121 } /* Literal.String */\n",
362 ".na { color: #7D9029 } /* Name.Attribute */\n",
363 ".nb { color: #008000 } /* Name.Builtin */\n",
364 ".nc { color: #0000FF; font-weight: bold } /* Name.Class */\n",
365 ".no { color: #880000 } /* Name.Constant */\n",
366 ".nd { color: #AA22FF } /* Name.Decorator */\n",
367 ".ni { color: #999999; font-weight: bold } /* Name.Entity */\n",
368 ".ne { color: #D2413A; font-weight: bold } /* Name.Exception */\n",
369 ".nf { color: #0000FF } /* Name.Function */\n",
370 ".nl { color: #A0A000 } /* Name.Label */\n",
371 ".nn { color: #0000FF; font-weight: bold } /* Name.Namespace */\n",
372 ".nt { color: #008000; font-weight: bold } /* Name.Tag */\n",
373 ".nv { color: #19177C } /* Name.Variable */\n",
374 ".ow { color: #AA22FF; font-weight: bold } /* Operator.Word */\n",
375 ".w { color: #bbbbbb } /* Text.Whitespace */\n",
376 ".mf { color: #666666 } /* Literal.Number.Float */\n",
377 ".mh { color: #666666 } /* Literal.Number.Hex */\n",
378 ".mi { color: #666666 } /* Literal.Number.Integer */\n",
379 ".mo { color: #666666 } /* Literal.Number.Oct */\n",
380 ".sb { color: #BA2121 } /* Literal.String.Backtick */\n",
381 ".sc { color: #BA2121 } /* Literal.String.Char */\n",
382 ".sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */\n",
383 ".s2 { color: #BA2121 } /* Literal.String.Double */\n",
384 ".se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */\n",
385 ".sh { color: #BA2121 } /* Literal.String.Heredoc */\n",
386 ".si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */\n",
387 ".sx { color: #008000 } /* Literal.String.Other */\n",
388 ".sr { color: #BB6688 } /* Literal.String.Regex */\n",
389 ".s1 { color: #BA2121 } /* Literal.String.Single */\n",
390 ".ss { color: #19177C } /* Literal.String.Symbol */\n",
391 ".bp { color: #008000 } /* Name.Builtin.Pseudo */\n",
392 ".vc { color: #19177C } /* Name.Variable.Class */\n",
393 ".vg { color: #19177C } /* Name.Variable.Global */\n",
394 ".vi { color: #19177C } /* Name.Variable.Instance */\n",
395 ".il { color: #666666 } /* Literal.Number.Integer.Long */\n",
396 "</style>\n"
397 ],
398 "metadata": {},
399 "output_type": "display_data",
400 "text": [
401 "<IPython.core.display.HTML at 0x10a006890>"
402 ]
403 }
404 ],
405 "prompt_number": 8
406 },
407 {
408 "cell_type": "code",
409 "collapsed": false,
410 "input": [
411 "def show_notebook(fname):\n",
412 " \"\"\"display a short summary of the cells of a notebook\"\"\"\n",
413 " with io.open(fname, 'r', encoding='utf-8') as f:\n",
414 " nb = current.read(f, 'json')\n",
415 " html = []\n",
416 " for cell in nb.worksheets[0].cells:\n",
417 " html.append(\"<h4>%s cell</h4>\" % cell.cell_type)\n",
418 " if cell.cell_type == 'code':\n",
419 " html.append(highlight(cell.input, lexer, formatter))\n",
420 " else:\n",
421 " html.append(\"<pre>%s</pre>\" % cell.source)\n",
422 " display(HTML('\\n'.join(html)))\n",
423 "\n",
424 "show_notebook(os.path.join(\"nbimp\", \"mynotebook.ipynb\"))"
425 ],
426 "language": "python",
427 "metadata": {},
428 "outputs": [
429 {
430 "html": [
431 "<h4>heading cell</h4>\n",
432 "<pre>My Notebook</pre>\n",
433 "<h4>code cell</h4>\n",
434 "<div class=\"highlight\"><pre><span class=\"k\">def</span> <span class=\"nf\">foo</span><span class=\"p\">():</span>\n",
435 " <span class=\"k\">return</span> <span class=\"s\">&quot;foo&quot;</span>\n",
436 "</pre></div>\n",
437 "\n",
438 "<h4>code cell</h4>\n",
439 "<div class=\"highlight\"><pre><span class=\"k\">def</span> <span class=\"nf\">has_ip_syntax</span><span class=\"p\">():</span>\n",
440 " <span class=\"n\">listing</span> <span class=\"o\">=</span> <span class=\"err\">!</span><span class=\"n\">ls</span>\n",
441 " <span class=\"k\">return</span> <span class=\"n\">listing</span>\n",
442 "</pre></div>\n",
443 "\n",
444 "<h4>code cell</h4>\n",
445 "<div class=\"highlight\"><pre><span class=\"k\">def</span> <span class=\"nf\">whatsmyname</span><span class=\"p\">():</span>\n",
446 " <span class=\"k\">return</span> <span class=\"n\">__name__</span>\n",
447 "</pre></div>\n"
448 ],
449 "metadata": {},
450 "output_type": "display_data",
451 "text": [
452 "<IPython.core.display.HTML at 0x10ad7af10>"
453 ]
454 }
455 ],
456 "prompt_number": 9
457 },
458 {
459 "cell_type": "markdown",
460 "metadata": {},
461 "source": [
462 "So my notebook has a heading cell and some code cells,\n",
463 "one of which contains some IPython syntax.\n",
464 "\n",
465 "Let's see what happens when we import it"
466 ]
467 },
468 {
469 "cell_type": "code",
470 "collapsed": false,
471 "input": [
472 "from nbimp import mynotebook"
473 ],
474 "language": "python",
475 "metadata": {},
476 "outputs": [
477 {
478 "output_type": "stream",
479 "stream": "stdout",
480 "text": [
481 "importing IPython notebook from nbimp/mynotebook.ipynb\n"
482 ]
483 }
484 ],
485 "prompt_number": 10
486 },
487 {
488 "cell_type": "markdown",
489 "metadata": {},
490 "source": [
491 "Hooray, it imported! Does it work?"
492 ]
493 },
494 {
495 "cell_type": "code",
496 "collapsed": false,
497 "input": [
498 "mynotebook.foo()"
499 ],
500 "language": "python",
501 "metadata": {},
502 "outputs": [
503 {
504 "metadata": {},
505 "output_type": "pyout",
506 "prompt_number": 11,
507 "text": [
508 "'foo'"
509 ]
510 }
511 ],
512 "prompt_number": 11
513 },
514 {
515 "cell_type": "markdown",
516 "metadata": {},
517 "source": [
518 "Hooray again!\n",
519 "\n",
520 "Even the function that contains IPython syntax works:"
521 ]
522 },
523 {
524 "cell_type": "code",
525 "collapsed": false,
526 "input": [
527 "mynotebook.has_ip_syntax()"
528 ],
529 "language": "python",
530 "metadata": {},
531 "outputs": [
532 {
533 "metadata": {},
534 "output_type": "pyout",
535 "prompt_number": 12,
536 "text": [
537 "['Animations Using clear_output.ipynb',\n",
538 " 'Cell Magics.ipynb',\n",
539 " 'Custom Display Logic.ipynb',\n",
540 " 'Cython Magics.ipynb',\n",
541 " 'Data Publication API.ipynb',\n",
542 " 'Frontend-Kernel Model.ipynb',\n",
543 " 'Importing Notebooks.ipynb',\n",
544 " 'Octave Magic.ipynb',\n",
545 " 'Part 1 - Running Code.ipynb',\n",
546 " 'Part 2 - Basic Output.ipynb',\n",
547 " 'Part 3 - Plotting with Matplotlib.ipynb',\n",
548 " 'Part 4 - Markdown Cells.ipynb',\n",
549 " 'Part 5 - Rich Display System.ipynb',\n",
550 " 'Progress Bars.ipynb',\n",
551 " 'R Magics.ipynb',\n",
552 " 'README.md',\n",
553 " 'Script Magics.ipynb',\n",
554 " 'SymPy Examples.ipynb',\n",
555 " 'Trapezoid Rule.ipynb',\n",
556 " 'Typesetting Math Using MathJax.ipynb',\n",
557 " 'animation.m4v',\n",
558 " 'foo.pyx',\n",
559 " 'lnum.py',\n",
560 " 'logo',\n",
561 " 'nbimp',\n",
562 " 'python-logo.svg',\n",
563 " 'test.html']"
564 ]
565 }
566 ],
567 "prompt_number": 12
568 },
569 {
570 "cell_type": "heading",
571 "level": 2,
572 "metadata": {},
573 "source": [
574 "Notebooks in packages"
575 ]
576 },
577 {
578 "cell_type": "markdown",
579 "metadata": {},
580 "source": [
581 "We also have a notebook inside the `nb` package,\n",
582 "so let's make sure that works as well."
583 ]
584 },
585 {
586 "cell_type": "code",
587 "collapsed": false,
588 "input": [
589 "ls nbimp/nbs"
590 ],
591 "language": "python",
592 "metadata": {},
593 "outputs": [
594 {
595 "output_type": "stream",
596 "stream": "stdout",
597 "text": [
598 "__init__.py __init__.pyc other.ipynb\r\n"
599 ]
600 }
601 ],
602 "prompt_number": 13
603 },
604 {
605 "cell_type": "markdown",
606 "metadata": {},
607 "source": [
608 "Note that the `__init__.py` is necessary for `nb` to be considered a package,\n",
609 "just like usual."
610 ]
611 },
612 {
613 "cell_type": "code",
614 "collapsed": false,
615 "input": [
616 "show_notebook(os.path.join(\"nbimp\", \"nbs\", \"other.ipynb\"))"
617 ],
618 "language": "python",
619 "metadata": {},
620 "outputs": [
621 {
622 "html": [
623 "<h4>markdown cell</h4>\n",
624 "<pre>This notebook just defines `bar`</pre>\n",
625 "<h4>code cell</h4>\n",
626 "<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",
627 " <span class=\"k\">return</span> <span class=\"s\">&quot;bar&quot;</span> <span class=\"o\">*</span> <span class=\"n\">x</span>\n",
628 "</pre></div>\n"
629 ],
630 "metadata": {},
631 "output_type": "display_data",
632 "text": [
633 "<IPython.core.display.HTML at 0x10ad56090>"
634 ]
635 }
636 ],
637 "prompt_number": 14
638 },
639 {
640 "cell_type": "code",
641 "collapsed": false,
642 "input": [
643 "from nbimp.nbs import other\n",
644 "other.bar(5)"
645 ],
646 "language": "python",
647 "metadata": {},
648 "outputs": [
649 {
650 "output_type": "stream",
651 "stream": "stdout",
652 "text": [
653 "importing IPython notebook from nbimp/nbs/other.ipynb\n"
654 ]
655 },
656 {
657 "metadata": {},
658 "output_type": "pyout",
659 "prompt_number": 15,
660 "text": [
661 "'barbarbarbarbar'"
662 ]
663 }
664 ],
665 "prompt_number": 15
666 },
667 {
668 "cell_type": "markdown",
669 "metadata": {},
670 "source": [
671 "So now we have importable notebooks, from both the local directory and inside packages.\n",
672 "\n",
673 "I can even put a notebook inside IPython, to further demonstrate that this is working properly:"
674 ]
675 },
676 {
677 "cell_type": "code",
678 "collapsed": false,
679 "input": [
680 "import shutil\n",
681 "from IPython.utils.path import get_ipython_package_dir\n",
682 "\n",
683 "utils = os.path.join(get_ipython_package_dir(), 'utils')\n",
684 "shutil.copy(os.path.join(\"nbimp\", \"mynotebook.ipynb\"),\n",
685 " os.path.join(utils, \"inside_ipython.ipynb\")\n",
686 ")"
687 ],
688 "language": "python",
689 "metadata": {},
690 "outputs": [],
691 "prompt_number": 16
692 },
693 {
694 "cell_type": "markdown",
695 "metadata": {},
696 "source": [
697 "and import the notebook from `IPython.utils`"
698 ]
699 },
700 {
701 "cell_type": "code",
702 "collapsed": false,
703 "input": [
704 "from IPython.utils import inside_ipython\n",
705 "inside_ipython.whatsmyname()"
706 ],
707 "language": "python",
708 "metadata": {},
709 "outputs": [
710 {
711 "output_type": "stream",
712 "stream": "stdout",
713 "text": [
714 "importing IPython notebook from /Users/minrk/dev/ip/mine/IPython/utils/inside_ipython.ipynb\n"
715 ]
716 },
717 {
718 "metadata": {},
719 "output_type": "pyout",
720 "prompt_number": 17,
721 "text": [
722 "'IPython.utils.inside_ipython'"
723 ]
724 }
725 ],
726 "prompt_number": 17
727 },
728 {
729 "cell_type": "heading",
730 "level": 2,
731 "metadata": {},
732 "source": [
733 "Even Cython magics"
734 ]
735 },
736 {
737 "cell_type": "markdown",
738 "metadata": {},
739 "source": [
740 "With a bit of extra magic for handling the IPython interactive namespace during load,\n",
741 "even magics like `%%cython` can be used:"
742 ]
743 },
744 {
745 "cell_type": "code",
746 "collapsed": false,
747 "input": [
748 "import Cython_Magics"
749 ],
750 "language": "python",
751 "metadata": {},
752 "outputs": [
753 {
754 "output_type": "stream",
755 "stream": "stdout",
756 "text": [
757 "importing IPython notebook from Cython Magics.ipynb\n",
758 "1000000 loops, best of 3: 439 ns per loop"
759 ]
760 },
761 {
762 "output_type": "stream",
763 "stream": "stdout",
764 "text": [
765 "\n",
766 "sin(1)= 0.841470984808\n"
767 ]
768 }
769 ],
770 "prompt_number": 18
771 },
772 {
773 "cell_type": "code",
774 "collapsed": false,
775 "input": [
776 "Cython_Magics.black_scholes(100.0, 100.0, 1.0, 0.3, 0.03, 0.0, -1)"
777 ],
778 "language": "python",
779 "metadata": {},
780 "outputs": [
781 {
782 "metadata": {},
783 "output_type": "pyout",
784 "prompt_number": 19,
785 "text": [
786 "10.327861752731728"
787 ]
788 }
789 ],
790 "prompt_number": 19
791 }
792 ],
793 "metadata": {}
794 }
795 ]
796 } No newline at end of file
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
@@ -0,0 +1,59 b''
1 {
2 "metadata": {
3 "name": ""
4 },
5 "nbformat": 3,
6 "nbformat_minor": 0,
7 "worksheets": [
8 {
9 "cells": [
10 {
11 "cell_type": "heading",
12 "level": 1,
13 "metadata": {},
14 "source": [
15 "My Notebook"
16 ]
17 },
18 {
19 "cell_type": "code",
20 "collapsed": false,
21 "input": [
22 "def foo():\n",
23 " return \"foo\""
24 ],
25 "language": "python",
26 "metadata": {},
27 "outputs": [],
28 "prompt_number": 1
29 },
30 {
31 "cell_type": "code",
32 "collapsed": false,
33 "input": [
34 "def has_ip_syntax():\n",
35 " listing = !ls\n",
36 " return listing"
37 ],
38 "language": "python",
39 "metadata": {},
40 "outputs": [],
41 "prompt_number": 2
42 },
43 {
44 "cell_type": "code",
45 "collapsed": false,
46 "input": [
47 "def whatsmyname():\n",
48 " return __name__"
49 ],
50 "language": "python",
51 "metadata": {},
52 "outputs": [],
53 "prompt_number": 4
54 }
55 ],
56 "metadata": {}
57 }
58 ]
59 } No newline at end of file
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
@@ -0,0 +1,33 b''
1 {
2 "metadata": {
3 "name": ""
4 },
5 "nbformat": 3,
6 "nbformat_minor": 0,
7 "worksheets": [
8 {
9 "cells": [
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "This notebook just defines `bar`"
15 ]
16 },
17 {
18 "cell_type": "code",
19 "collapsed": false,
20 "input": [
21 "def bar(x):\n",
22 " return \"bar\" * x"
23 ],
24 "language": "python",
25 "metadata": {},
26 "outputs": [],
27 "prompt_number": 2
28 }
29 ],
30 "metadata": {}
31 }
32 ]
33 } No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now