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