##// END OF EJS Templates
Merge pull request #3508 from minrk/raw_input...
Matthias Bussonnier -
r11189:5f181018 merge
parent child Browse files
Show More
@@ -1,157 +1,271
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "name": "Frontend-Kernel Model"
3 "name": "Frontend-Kernel Model"
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 one feature the notebook currently doesn't support as a Frontend is the ability to send data to the Kernel's standard input socket. That is, if the Kernel requires information to be typed interactively by calling the builtin `raw_input` function, the Notebook will be blocked. This happens for example if you run a script that queries interactively for parameters, and very importantly, is how the interactive IPython debugger that activates when you type `%debug` works.\n",
148 "The Notebook has added support for `raw_input` and `%debug`, as of 1.0."
149 "\n",
150 "So, in order to be able to use `%debug` or anything else that requires `raw_input`, you can either use a Terminal Console or Qt Console connected to a Notebook's Kernel as described above.\n"
151 ]
149 ]
150 },
151 {
152 "cell_type": "code",
153 "collapsed": false,
154 "input": [
155 "name = raw_input(\"What is your name? \")\n",
156 "name"
157 ],
158 "language": "python",
159 "metadata": {},
160 "outputs": [
161 {
162 "name": "stdout",
163 "output_type": "stream",
164 "stream": "stdout",
165 "text": [
166 "What is your name? Sir Robin\n"
167 ]
168 },
169 {
170 "metadata": {},
171 "output_type": "pyout",
172 "prompt_number": 1,
173 "text": [
174 "u'Sir Robin'"
175 ]
176 }
177 ],
178 "prompt_number": 1
179 },
180 {
181 "cell_type": "code",
182 "collapsed": false,
183 "input": [
184 "def div(x, y):\n",
185 " return x/y\n",
186 "\n",
187 "div(1,0)"
188 ],
189 "language": "python",
190 "metadata": {},
191 "outputs": [
192 {
193 "ename": "ZeroDivisionError",
194 "evalue": "integer division or modulo by zero",
195 "output_type": "pyerr",
196 "traceback": [
197 "\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",
200 "\u001b[1;31mZeroDivisionError\u001b[0m: integer division or modulo by zero"
201 ]
202 }
203 ],
204 "prompt_number": 2
205 },
206 {
207 "cell_type": "code",
208 "collapsed": false,
209 "input": [
210 "%debug"
211 ],
212 "language": "python",
213 "metadata": {},
214 "outputs": [
215 {
216 "output_type": "stream",
217 "stream": "stdout",
218 "text": [
219 "> \u001b[1;32m<ipython-input-2-a5097cc0c0c5>\u001b[0m(2)\u001b[0;36mdiv\u001b[1;34m()\u001b[0m\n",
220 "\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 "\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 "\u001b[0m\u001b[1;32m 3 \u001b[1;33m\u001b[1;33m\u001b[0m\u001b[0m\n",
223 "\u001b[0m\n"
224 ]
225 },
226 {
227 "name": "stdout",
228 "output_type": "stream",
229 "stream": "stdout",
230 "text": [
231 "ipdb> x\n"
232 ]
233 },
234 {
235 "output_type": "stream",
236 "stream": "stdout",
237 "text": [
238 "1\n"
239 ]
240 },
241 {
242 "name": "stdout",
243 "output_type": "stream",
244 "stream": "stdout",
245 "text": [
246 "ipdb> y\n"
247 ]
248 },
249 {
250 "output_type": "stream",
251 "stream": "stdout",
252 "text": [
253 "0\n"
254 ]
255 },
256 {
257 "name": "stdout",
258 "output_type": "stream",
259 "stream": "stdout",
260 "text": [
261 "ipdb> exit\n"
262 ]
263 }
264 ],
265 "prompt_number": 3
152 }
266 }
153 ],
267 ],
154 "metadata": {}
268 "metadata": {}
155 }
269 }
156 ]
270 ]
157 } No newline at end of file
271 }
General Comments 0
You need to be logged in to leave comments. Login now