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