Show More
@@ -1,523 +1,763 b'' | |||
|
1 | 1 | { |
|
2 | 2 | "cells": [ |
|
3 | 3 | { |
|
4 | 4 | "cell_type": "markdown", |
|
5 | 5 | "metadata": {}, |
|
6 | 6 | "source": [ |
|
7 | 7 | "# Importing IPython Notebooks as Modules" |
|
8 | 8 | ] |
|
9 | 9 | }, |
|
10 | 10 | { |
|
11 | 11 | "cell_type": "markdown", |
|
12 | 12 | "metadata": {}, |
|
13 | 13 | "source": [ |
|
14 | 14 | "It is a common problem that people want to import code from IPython Notebooks.\n", |
|
15 | 15 | "This is made difficult by the fact that Notebooks are not plain Python files,\n", |
|
16 | 16 | "and thus cannot be imported by the regular Python machinery.\n", |
|
17 | 17 | "\n", |
|
18 | 18 | "Fortunately, Python provides some fairly sophisticated [hooks](http://www.python.org/dev/peps/pep-0302/) into the import machinery,\n", |
|
19 | 19 | "so we can actually make IPython notebooks importable without much difficulty,\n", |
|
20 | 20 | "and only using public APIs." |
|
21 | 21 | ] |
|
22 | 22 | }, |
|
23 | 23 | { |
|
24 | 24 | "cell_type": "code", |
|
25 |
"execution_count": |
|
|
25 | "execution_count": 1, | |
|
26 | 26 | "metadata": { |
|
27 | 27 | "collapsed": false |
|
28 | 28 | }, |
|
29 | 29 | "outputs": [], |
|
30 | 30 | "source": [ |
|
31 | 31 | "import io, os, sys, types" |
|
32 | 32 | ] |
|
33 | 33 | }, |
|
34 | 34 | { |
|
35 | 35 | "cell_type": "code", |
|
36 |
"execution_count": |
|
|
36 | "execution_count": 2, | |
|
37 | 37 | "metadata": { |
|
38 | 38 | "collapsed": false |
|
39 | 39 | }, |
|
40 | 40 | "outputs": [], |
|
41 | 41 | "source": [ |
|
42 | "import nbformat\n", | |
|
43 | "\n", | |
|
42 | 44 | "from IPython import get_ipython\n", |
|
43 | "from IPython.nbformat import current\n", | |
|
44 | 45 | "from IPython.core.interactiveshell import InteractiveShell" |
|
45 | 46 | ] |
|
46 | 47 | }, |
|
47 | 48 | { |
|
48 | 49 | "cell_type": "markdown", |
|
49 | 50 | "metadata": {}, |
|
50 | 51 | "source": [ |
|
51 | 52 | "Import hooks typically take the form of two objects:\n", |
|
52 | 53 | "\n", |
|
53 | 54 | "1. a Module **Loader**, which takes a module name (e.g. `'IPython.display'`), and returns a Module\n", |
|
54 | 55 | "2. a Module **Finder**, which figures out whether a module might exist, and tells Python what **Loader** to use" |
|
55 | 56 | ] |
|
56 | 57 | }, |
|
57 | 58 | { |
|
58 | 59 | "cell_type": "code", |
|
59 |
"execution_count": |
|
|
60 | "execution_count": 3, | |
|
60 | 61 | "metadata": { |
|
61 | 62 | "collapsed": false |
|
62 | 63 | }, |
|
63 | 64 | "outputs": [], |
|
64 | 65 | "source": [ |
|
65 | 66 | "def find_notebook(fullname, path=None):\n", |
|
66 | 67 | " \"\"\"find a notebook, given its fully qualified name and an optional path\n", |
|
67 | 68 | " \n", |
|
68 | 69 | " This turns \"foo.bar\" into \"foo/bar.ipynb\"\n", |
|
69 | 70 | " and tries turning \"Foo_Bar\" into \"Foo Bar\" if Foo_Bar\n", |
|
70 | 71 | " does not exist.\n", |
|
71 | 72 | " \"\"\"\n", |
|
72 | 73 | " name = fullname.rsplit('.', 1)[-1]\n", |
|
73 | 74 | " if not path:\n", |
|
74 | 75 | " path = ['']\n", |
|
75 | 76 | " for d in path:\n", |
|
76 | 77 | " nb_path = os.path.join(d, name + \".ipynb\")\n", |
|
77 | 78 | " if os.path.isfile(nb_path):\n", |
|
78 | 79 | " return nb_path\n", |
|
79 | 80 | " # let import Notebook_Name find \"Notebook Name.ipynb\"\n", |
|
80 | 81 | " nb_path = nb_path.replace(\"_\", \" \")\n", |
|
81 | 82 | " if os.path.isfile(nb_path):\n", |
|
82 | 83 | " return nb_path\n", |
|
83 | 84 | " " |
|
84 | 85 | ] |
|
85 | 86 | }, |
|
86 | 87 | { |
|
87 | 88 | "cell_type": "markdown", |
|
88 | 89 | "metadata": {}, |
|
89 | 90 | "source": [ |
|
90 | 91 | "## Notebook Loader" |
|
91 | 92 | ] |
|
92 | 93 | }, |
|
93 | 94 | { |
|
94 | 95 | "cell_type": "markdown", |
|
95 | 96 | "metadata": {}, |
|
96 | 97 | "source": [ |
|
97 | 98 | "Here we have our Notebook Loader.\n", |
|
98 | 99 | "It's actually quite simple - once we figure out the filename of the module,\n", |
|
99 | 100 | "all it does is:\n", |
|
100 | 101 | "\n", |
|
101 | 102 | "1. load the notebook document into memory\n", |
|
102 | 103 | "2. create an empty Module\n", |
|
103 | 104 | "3. execute every cell in the Module namespace\n", |
|
104 | 105 | "\n", |
|
105 | 106 | "Since IPython cells can have extended syntax,\n", |
|
106 | 107 | "the IPython transform is applied to turn each of these cells into their pure-Python counterparts before executing them.\n", |
|
107 | 108 | "If all of your notebook cells are pure-Python,\n", |
|
108 | 109 | "this step is unnecessary." |
|
109 | 110 | ] |
|
110 | 111 | }, |
|
111 | 112 | { |
|
112 | 113 | "cell_type": "code", |
|
113 |
"execution_count": |
|
|
114 | "execution_count": 4, | |
|
114 | 115 | "metadata": { |
|
115 | 116 | "collapsed": false |
|
116 | 117 | }, |
|
117 | 118 | "outputs": [], |
|
118 | 119 | "source": [ |
|
119 | 120 | "class NotebookLoader(object):\n", |
|
120 | 121 | " \"\"\"Module Loader for IPython Notebooks\"\"\"\n", |
|
121 | 122 | " def __init__(self, path=None):\n", |
|
122 | 123 | " self.shell = InteractiveShell.instance()\n", |
|
123 | 124 | " self.path = path\n", |
|
124 | 125 | " \n", |
|
125 | 126 | " def load_module(self, fullname):\n", |
|
126 | 127 | " \"\"\"import a notebook as a module\"\"\"\n", |
|
127 | 128 | " path = find_notebook(fullname, self.path)\n", |
|
128 | 129 | " \n", |
|
129 |
" print (\"importing |
|
|
130 | " print (\"importing notebook from %s\" % path)\n", | |
|
130 | 131 | " \n", |
|
131 | 132 | " # load the notebook object\n", |
|
132 | " with io.open(path, 'r', encoding='utf-8') as f:\n", | |
|
133 | " nb = current.read(f, 'json')\n", | |
|
133 | " nb = nbformat.read(path, as_version=4)\n", | |
|
134 | 134 | " \n", |
|
135 | 135 | " \n", |
|
136 | 136 | " # create the module and add it to sys.modules\n", |
|
137 | 137 | " # if name in sys.modules:\n", |
|
138 | 138 | " # return sys.modules[name]\n", |
|
139 | 139 | " mod = types.ModuleType(fullname)\n", |
|
140 | 140 | " mod.__file__ = path\n", |
|
141 | 141 | " mod.__loader__ = self\n", |
|
142 | 142 | " mod.__dict__['get_ipython'] = get_ipython\n", |
|
143 | 143 | " sys.modules[fullname] = mod\n", |
|
144 | 144 | " \n", |
|
145 | 145 | " # extra work to ensure that magics that would affect the user_ns\n", |
|
146 | 146 | " # actually affect the notebook module's ns\n", |
|
147 | 147 | " save_user_ns = self.shell.user_ns\n", |
|
148 | 148 | " self.shell.user_ns = mod.__dict__\n", |
|
149 | 149 | " \n", |
|
150 | 150 | " try:\n", |
|
151 |
" for cell in nb. |
|
|
152 |
" if cell.cell_type == 'code' |
|
|
151 | " for cell in nb.cells:\n", | |
|
152 | " if cell.cell_type == 'code':\n", | |
|
153 | 153 | " # transform the input to executable Python\n", |
|
154 |
" code = self.shell.input_transformer_manager.transform_cell(cell. |
|
|
154 | " code = self.shell.input_transformer_manager.transform_cell(cell.source)\n", | |
|
155 | 155 | " # run the code in themodule\n", |
|
156 | 156 | " exec(code, mod.__dict__)\n", |
|
157 | 157 | " finally:\n", |
|
158 | 158 | " self.shell.user_ns = save_user_ns\n", |
|
159 | 159 | " return mod\n" |
|
160 | 160 | ] |
|
161 | 161 | }, |
|
162 | 162 | { |
|
163 | 163 | "cell_type": "markdown", |
|
164 | 164 | "metadata": {}, |
|
165 | 165 | "source": [ |
|
166 | 166 | "## The Module Finder" |
|
167 | 167 | ] |
|
168 | 168 | }, |
|
169 | 169 | { |
|
170 | 170 | "cell_type": "markdown", |
|
171 | 171 | "metadata": {}, |
|
172 | 172 | "source": [ |
|
173 | 173 | "The finder is a simple object that tells you whether a name can be imported,\n", |
|
174 | 174 | "and returns the appropriate loader.\n", |
|
175 | 175 | "All this one does is check, when you do:\n", |
|
176 | 176 | "\n", |
|
177 | 177 | "```python\n", |
|
178 | 178 | "import mynotebook\n", |
|
179 | 179 | "```\n", |
|
180 | 180 | "\n", |
|
181 | 181 | "it checks whether `mynotebook.ipynb` exists.\n", |
|
182 | 182 | "If a notebook is found, then it returns a NotebookLoader.\n", |
|
183 | 183 | "\n", |
|
184 | 184 | "Any extra logic is just for resolving paths within packages." |
|
185 | 185 | ] |
|
186 | 186 | }, |
|
187 | 187 | { |
|
188 | 188 | "cell_type": "code", |
|
189 |
"execution_count": |
|
|
189 | "execution_count": 5, | |
|
190 | 190 | "metadata": { |
|
191 | 191 | "collapsed": false |
|
192 | 192 | }, |
|
193 | 193 | "outputs": [], |
|
194 | 194 | "source": [ |
|
195 | 195 | "class NotebookFinder(object):\n", |
|
196 | 196 | " \"\"\"Module finder that locates IPython Notebooks\"\"\"\n", |
|
197 | 197 | " def __init__(self):\n", |
|
198 | 198 | " self.loaders = {}\n", |
|
199 | 199 | " \n", |
|
200 | 200 | " def find_module(self, fullname, path=None):\n", |
|
201 | 201 | " nb_path = find_notebook(fullname, path)\n", |
|
202 | 202 | " if not nb_path:\n", |
|
203 | 203 | " return\n", |
|
204 | 204 | " \n", |
|
205 | 205 | " key = path\n", |
|
206 | 206 | " if path:\n", |
|
207 | 207 | " # lists aren't hashable\n", |
|
208 | 208 | " key = os.path.sep.join(path)\n", |
|
209 | 209 | " \n", |
|
210 | 210 | " if key not in self.loaders:\n", |
|
211 | 211 | " self.loaders[key] = NotebookLoader(path)\n", |
|
212 | 212 | " return self.loaders[key]\n" |
|
213 | 213 | ] |
|
214 | 214 | }, |
|
215 | 215 | { |
|
216 | 216 | "cell_type": "markdown", |
|
217 | 217 | "metadata": {}, |
|
218 | 218 | "source": [ |
|
219 | 219 | "## Register the hook" |
|
220 | 220 | ] |
|
221 | 221 | }, |
|
222 | 222 | { |
|
223 | 223 | "cell_type": "markdown", |
|
224 | 224 | "metadata": {}, |
|
225 | 225 | "source": [ |
|
226 | 226 | "Now we register the `NotebookFinder` with `sys.meta_path`" |
|
227 | 227 | ] |
|
228 | 228 | }, |
|
229 | 229 | { |
|
230 | 230 | "cell_type": "code", |
|
231 |
"execution_count": |
|
|
231 | "execution_count": 6, | |
|
232 | 232 | "metadata": { |
|
233 | 233 | "collapsed": false |
|
234 | 234 | }, |
|
235 | 235 | "outputs": [], |
|
236 | 236 | "source": [ |
|
237 | 237 | "sys.meta_path.append(NotebookFinder())" |
|
238 | 238 | ] |
|
239 | 239 | }, |
|
240 | 240 | { |
|
241 | 241 | "cell_type": "markdown", |
|
242 | 242 | "metadata": {}, |
|
243 | 243 | "source": [ |
|
244 | 244 | "After this point, my notebooks should be importable.\n", |
|
245 | 245 | "\n", |
|
246 | 246 | "Let's look at what we have in the CWD:" |
|
247 | 247 | ] |
|
248 | 248 | }, |
|
249 | 249 | { |
|
250 | 250 | "cell_type": "code", |
|
251 |
"execution_count": |
|
|
251 | "execution_count": 7, | |
|
252 | 252 | "metadata": { |
|
253 | 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 | 264 | "source": [ |
|
257 | 265 | "ls nbpackage" |
|
258 | 266 | ] |
|
259 | 267 | }, |
|
260 | 268 | { |
|
261 | 269 | "cell_type": "markdown", |
|
262 | 270 | "metadata": {}, |
|
263 | 271 | "source": [ |
|
264 | 272 | "So I should be able to `import nbimp.mynotebook`.\n" |
|
265 | 273 | ] |
|
266 | 274 | }, |
|
267 | 275 | { |
|
268 | 276 | "cell_type": "markdown", |
|
269 | 277 | "metadata": {}, |
|
270 | 278 | "source": [ |
|
271 | 279 | "### Aside: displaying notebooks" |
|
272 | 280 | ] |
|
273 | 281 | }, |
|
274 | 282 | { |
|
275 | 283 | "cell_type": "markdown", |
|
276 | 284 | "metadata": {}, |
|
277 | 285 | "source": [ |
|
278 | 286 | "Here is some simple code to display the contents of a notebook\n", |
|
279 | 287 | "with syntax highlighting, etc." |
|
280 | 288 | ] |
|
281 | 289 | }, |
|
282 | 290 | { |
|
283 | 291 | "cell_type": "code", |
|
284 |
"execution_count": |
|
|
292 | "execution_count": 8, | |
|
285 | 293 | "metadata": { |
|
286 | 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 | 374 | "source": [ |
|
290 | 375 | "from pygments import highlight\n", |
|
291 | 376 | "from pygments.lexers import PythonLexer\n", |
|
292 | 377 | "from pygments.formatters import HtmlFormatter\n", |
|
293 | 378 | "\n", |
|
294 | 379 | "from IPython.display import display, HTML\n", |
|
295 | 380 | "\n", |
|
296 | 381 | "formatter = HtmlFormatter()\n", |
|
297 | 382 | "lexer = PythonLexer()\n", |
|
298 | 383 | "\n", |
|
299 | 384 | "# publish the CSS for pygments highlighting\n", |
|
300 | 385 | "display(HTML(\"\"\"\n", |
|
301 | 386 | "<style type='text/css'>\n", |
|
302 | 387 | "%s\n", |
|
303 | 388 | "</style>\n", |
|
304 | 389 | "\"\"\" % formatter.get_style_defs()\n", |
|
305 | 390 | "))" |
|
306 | 391 | ] |
|
307 | 392 | }, |
|
308 | 393 | { |
|
309 | 394 | "cell_type": "code", |
|
310 |
"execution_count": |
|
|
395 | "execution_count": 9, | |
|
311 | 396 | "metadata": { |
|
312 | 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\">"foo"</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 | 429 | "source": [ |
|
316 | 430 | "def show_notebook(fname):\n", |
|
317 | 431 | " \"\"\"display a short summary of the cells of a notebook\"\"\"\n", |
|
318 | " with io.open(fname, 'r', encoding='utf-8') as f:\n", | |
|
319 | " nb = current.read(f, 'json')\n", | |
|
432 | " nb = nbformat.read(fname, as_version=4)\n", | |
|
320 | 433 | " html = []\n", |
|
321 |
" for cell in nb. |
|
|
434 | " for cell in nb.cells:\n", | |
|
322 | 435 | " html.append(\"<h4>%s cell</h4>\" % cell.cell_type)\n", |
|
323 | 436 | " if cell.cell_type == 'code':\n", |
|
324 |
" html.append(highlight(cell. |
|
|
437 | " html.append(highlight(cell.source, lexer, formatter))\n", | |
|
325 | 438 | " else:\n", |
|
326 | 439 | " html.append(\"<pre>%s</pre>\" % cell.source)\n", |
|
327 | 440 | " display(HTML('\\n'.join(html)))\n", |
|
328 | 441 | "\n", |
|
329 | 442 | "show_notebook(os.path.join(\"nbpackage\", \"mynotebook.ipynb\"))" |
|
330 | 443 | ] |
|
331 | 444 | }, |
|
332 | 445 | { |
|
333 | 446 | "cell_type": "markdown", |
|
334 | 447 | "metadata": {}, |
|
335 | 448 | "source": [ |
|
336 | 449 | "So my notebook has a heading cell and some code cells,\n", |
|
337 | 450 | "one of which contains some IPython syntax.\n", |
|
338 | 451 | "\n", |
|
339 | 452 | "Let's see what happens when we import it" |
|
340 | 453 | ] |
|
341 | 454 | }, |
|
342 | 455 | { |
|
343 | 456 | "cell_type": "code", |
|
344 |
"execution_count": |
|
|
457 | "execution_count": 10, | |
|
345 | 458 | "metadata": { |
|
346 | 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 | 470 | "source": [ |
|
350 | 471 | "from nbpackage import mynotebook" |
|
351 | 472 | ] |
|
352 | 473 | }, |
|
353 | 474 | { |
|
354 | 475 | "cell_type": "markdown", |
|
355 | 476 | "metadata": {}, |
|
356 | 477 | "source": [ |
|
357 | 478 | "Hooray, it imported! Does it work?" |
|
358 | 479 | ] |
|
359 | 480 | }, |
|
360 | 481 | { |
|
361 | 482 | "cell_type": "code", |
|
362 |
"execution_count": |
|
|
483 | "execution_count": 11, | |
|
363 | 484 | "metadata": { |
|
364 | 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 | 499 | "source": [ |
|
368 | 500 | "mynotebook.foo()" |
|
369 | 501 | ] |
|
370 | 502 | }, |
|
371 | 503 | { |
|
372 | 504 | "cell_type": "markdown", |
|
373 | 505 | "metadata": {}, |
|
374 | 506 | "source": [ |
|
375 | 507 | "Hooray again!\n", |
|
376 | 508 | "\n", |
|
377 | 509 | "Even the function that contains IPython syntax works:" |
|
378 | 510 | ] |
|
379 | 511 | }, |
|
380 | 512 | { |
|
381 | 513 | "cell_type": "code", |
|
382 |
"execution_count": |
|
|
514 | "execution_count": 12, | |
|
383 | 515 | "metadata": { |
|
384 | 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 | 554 | "source": [ |
|
388 | 555 | "mynotebook.has_ip_syntax()" |
|
389 | 556 | ] |
|
390 | 557 | }, |
|
391 | 558 | { |
|
392 | 559 | "cell_type": "markdown", |
|
393 | 560 | "metadata": {}, |
|
394 | 561 | "source": [ |
|
395 | 562 | "## Notebooks in packages" |
|
396 | 563 | ] |
|
397 | 564 | }, |
|
398 | 565 | { |
|
399 | 566 | "cell_type": "markdown", |
|
400 | 567 | "metadata": {}, |
|
401 | 568 | "source": [ |
|
402 | 569 | "We also have a notebook inside the `nb` package,\n", |
|
403 | 570 | "so let's make sure that works as well." |
|
404 | 571 | ] |
|
405 | 572 | }, |
|
406 | 573 | { |
|
407 | 574 | "cell_type": "code", |
|
408 |
"execution_count": |
|
|
575 | "execution_count": 13, | |
|
409 | 576 | "metadata": { |
|
410 | 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 | 588 | "source": [ |
|
414 | 589 | "ls nbpackage/nbs" |
|
415 | 590 | ] |
|
416 | 591 | }, |
|
417 | 592 | { |
|
418 | 593 | "cell_type": "markdown", |
|
419 | 594 | "metadata": {}, |
|
420 | 595 | "source": [ |
|
421 | 596 | "Note that the `__init__.py` is necessary for `nb` to be considered a package,\n", |
|
422 | 597 | "just like usual." |
|
423 | 598 | ] |
|
424 | 599 | }, |
|
425 | 600 | { |
|
426 | 601 | "cell_type": "code", |
|
427 |
"execution_count": |
|
|
602 | "execution_count": 14, | |
|
428 | 603 | "metadata": { |
|
429 | 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\">"bar"</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 | 625 | "source": [ |
|
433 | 626 | "show_notebook(os.path.join(\"nbpackage\", \"nbs\", \"other.ipynb\"))" |
|
434 | 627 | ] |
|
435 | 628 | }, |
|
436 | 629 | { |
|
437 | 630 | "cell_type": "code", |
|
438 |
"execution_count": |
|
|
631 | "execution_count": 15, | |
|
439 | 632 | "metadata": { |
|
440 | 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 | 654 | "source": [ |
|
444 | 655 | "from nbpackage.nbs import other\n", |
|
445 | 656 | "other.bar(5)" |
|
446 | 657 | ] |
|
447 | 658 | }, |
|
448 | 659 | { |
|
449 | 660 | "cell_type": "markdown", |
|
450 | 661 | "metadata": {}, |
|
451 | 662 | "source": [ |
|
452 | 663 | "So now we have importable notebooks, from both the local directory and inside packages.\n", |
|
453 | 664 | "\n", |
|
454 | 665 | "I can even put a notebook inside IPython, to further demonstrate that this is working properly:" |
|
455 | 666 | ] |
|
456 | 667 | }, |
|
457 | 668 | { |
|
458 | 669 | "cell_type": "code", |
|
459 |
"execution_count": |
|
|
670 | "execution_count": 16, | |
|
460 | 671 | "metadata": { |
|
461 | 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 | 686 | "source": [ |
|
465 | 687 | "import shutil\n", |
|
466 |
"from IPython. |
|
|
688 | "from IPython.paths import get_ipython_package_dir\n", | |
|
467 | 689 | "\n", |
|
468 | 690 | "utils = os.path.join(get_ipython_package_dir(), 'utils')\n", |
|
469 | 691 | "shutil.copy(os.path.join(\"nbpackage\", \"mynotebook.ipynb\"),\n", |
|
470 | 692 | " os.path.join(utils, \"inside_ipython.ipynb\")\n", |
|
471 | 693 | ")" |
|
472 | 694 | ] |
|
473 | 695 | }, |
|
474 | 696 | { |
|
475 | 697 | "cell_type": "markdown", |
|
476 | 698 | "metadata": {}, |
|
477 | 699 | "source": [ |
|
478 | 700 | "and import the notebook from `IPython.utils`" |
|
479 | 701 | ] |
|
480 | 702 | }, |
|
481 | 703 | { |
|
482 | 704 | "cell_type": "code", |
|
483 |
"execution_count": |
|
|
705 | "execution_count": 17, | |
|
484 | 706 | "metadata": { |
|
485 | 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 | 728 | "source": [ |
|
489 | 729 | "from IPython.utils import inside_ipython\n", |
|
490 | 730 | "inside_ipython.whatsmyname()" |
|
491 | 731 | ] |
|
492 | 732 | }, |
|
493 | 733 | { |
|
494 | 734 | "cell_type": "markdown", |
|
495 | 735 | "metadata": {}, |
|
496 | 736 | "source": [ |
|
497 | 737 | "This approach can even import functions and classes that are defined in a notebook using the `%%cython` magic." |
|
498 | 738 | ] |
|
499 | 739 | } |
|
500 | 740 | ], |
|
501 | 741 | "metadata": { |
|
502 | 742 | "gist_id": "6011986", |
|
503 | 743 | "kernelspec": { |
|
504 | 744 | "display_name": "Python 3", |
|
505 | 745 | "language": "python", |
|
506 | 746 | "name": "python3" |
|
507 | 747 | }, |
|
508 | 748 | "language_info": { |
|
509 | 749 | "codemirror_mode": { |
|
510 | 750 | "name": "ipython", |
|
511 | 751 | "version": 3 |
|
512 | 752 | }, |
|
513 | 753 | "file_extension": ".py", |
|
514 | 754 | "mimetype": "text/x-python", |
|
515 | 755 | "name": "python", |
|
516 | 756 | "nbconvert_exporter": "python", |
|
517 | 757 | "pygments_lexer": "ipython3", |
|
518 | 758 | "version": "3.4.3" |
|
519 | 759 | } |
|
520 | 760 | }, |
|
521 | 761 | "nbformat": 4, |
|
522 | 762 | "nbformat_minor": 0 |
|
523 | 763 | } |
General Comments 0
You need to be logged in to leave comments.
Login now