##// END OF EJS Templates
update raw_input in example notebook
MinRK -
Show More
@@ -1,271 +1,322 b''
1 1 {
2 2 "metadata": {
3 "name": "Frontend-Kernel Model"
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 "# Python 3 compat\n",
156 "try:\n",
157 " raw_input\n",
158 "except NameError:\n",
159 " raw_input = input"
160 ],
161 "language": "python",
162 "metadata": {},
163 "outputs": [],
164 "prompt_number": 1
165 },
166 {
167 "cell_type": "code",
168 "collapsed": false,
169 "input": [
155 170 "name = raw_input(\"What is your name? \")\n",
156 171 "name"
157 172 ],
158 173 "language": "python",
159 174 "metadata": {},
160 175 "outputs": [
161 176 {
162 177 "name": "stdout",
163 178 "output_type": "stream",
164 179 "stream": "stdout",
165 180 "text": [
166 181 "What is your name? Sir Robin\n"
167 182 ]
168 183 },
169 184 {
170 185 "metadata": {},
171 186 "output_type": "pyout",
172 "prompt_number": 1,
187 "prompt_number": 2,
173 188 "text": [
174 "u'Sir Robin'"
189 "'Sir Robin'"
175 190 ]
176 191 }
177 192 ],
178 "prompt_number": 1
193 "prompt_number": 2
194 },
195 {
196 "cell_type": "markdown",
197 "metadata": {},
198 "source": [
199 "**Python 2-only**: the eval input works as well (`input` is just `eval(raw_input(prompt))`)"
200 ]
201 },
202 {
203 "cell_type": "code",
204 "collapsed": false,
205 "input": [
206 "fingers = input(\"How many fingers? \")\n",
207 "fingers, type(fingers)"
208 ],
209 "language": "python",
210 "metadata": {},
211 "outputs": [
212 {
213 "name": "stdout",
214 "output_type": "stream",
215 "stream": "stdout",
216 "text": [
217 "How many fingers? 4\n"
218 ]
219 },
220 {
221 "metadata": {},
222 "output_type": "pyout",
223 "prompt_number": 3,
224 "text": [
225 "(4, int)"
226 ]
227 }
228 ],
229 "prompt_number": 3
179 230 },
180 231 {
181 232 "cell_type": "code",
182 233 "collapsed": false,
183 234 "input": [
184 235 "def div(x, y):\n",
185 236 " return x/y\n",
186 237 "\n",
187 238 "div(1,0)"
188 239 ],
189 240 "language": "python",
190 241 "metadata": {},
191 242 "outputs": [
192 243 {
193 244 "ename": "ZeroDivisionError",
194 245 "evalue": "integer division or modulo by zero",
195 246 "output_type": "pyerr",
196 247 "traceback": [
197 248 "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
198 "\u001b[1;32m<ipython-input-2-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",
199 "\u001b[1;32m<ipython-input-2-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;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",
200 251 "\u001b[1;31mZeroDivisionError\u001b[0m: integer division or modulo by zero"
201 252 ]
202 253 }
203 254 ],
204 "prompt_number": 2
255 "prompt_number": 4
205 256 },
206 257 {
207 258 "cell_type": "code",
208 259 "collapsed": false,
209 260 "input": [
210 261 "%debug"
211 262 ],
212 263 "language": "python",
213 264 "metadata": {},
214 265 "outputs": [
215 266 {
216 267 "output_type": "stream",
217 268 "stream": "stdout",
218 269 "text": [
219 "> \u001b[1;32m<ipython-input-2-a5097cc0c0c5>\u001b[0m(2)\u001b[0;36mdiv\u001b[1;34m()\u001b[0m\n",
270 "> \u001b[1;32m<ipython-input-4-a5097cc0c0c5>\u001b[0m(2)\u001b[0;36mdiv\u001b[1;34m()\u001b[0m\n",
220 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",
221 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",
222 273 "\u001b[0m\u001b[1;32m 3 \u001b[1;33m\u001b[1;33m\u001b[0m\u001b[0m\n",
223 274 "\u001b[0m\n"
224 275 ]
225 276 },
226 277 {
227 278 "name": "stdout",
228 279 "output_type": "stream",
229 280 "stream": "stdout",
230 281 "text": [
231 282 "ipdb> x\n"
232 283 ]
233 284 },
234 285 {
235 286 "output_type": "stream",
236 287 "stream": "stdout",
237 288 "text": [
238 289 "1\n"
239 290 ]
240 291 },
241 292 {
242 293 "name": "stdout",
243 294 "output_type": "stream",
244 295 "stream": "stdout",
245 296 "text": [
246 297 "ipdb> y\n"
247 298 ]
248 299 },
249 300 {
250 301 "output_type": "stream",
251 302 "stream": "stdout",
252 303 "text": [
253 304 "0\n"
254 305 ]
255 306 },
256 307 {
257 308 "name": "stdout",
258 309 "output_type": "stream",
259 310 "stream": "stdout",
260 311 "text": [
261 312 "ipdb> exit\n"
262 313 ]
263 314 }
264 315 ],
265 "prompt_number": 3
316 "prompt_number": 5
266 317 }
267 318 ],
268 319 "metadata": {}
269 320 }
270 321 ]
271 322 } No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now