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