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