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