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