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