Show More
@@ -1,650 +1,654 b'' | |||||
1 | .. _qtconsole: |
|
1 | .. _qtconsole: | |
2 |
|
2 | |||
3 | ========================= |
|
3 | ========================= | |
4 | A Qt Console for IPython |
|
4 | A Qt Console for IPython | |
5 | ========================= |
|
5 | ========================= | |
6 |
|
6 | |||
|
7 | To start the Qt Console:: | |||
|
8 | ||||
|
9 | ipython qtconsole | |||
|
10 | ||||
7 | We now have a version of IPython, using the new two-process :ref:`ZeroMQ Kernel |
|
11 | We now have a version of IPython, using the new two-process :ref:`ZeroMQ Kernel | |
8 | <ipythonzmq>`, running in a PyQt_ GUI. This is a very lightweight widget that |
|
12 | <ipythonzmq>`, running in a PyQt_ GUI. This is a very lightweight widget that | |
9 | largely feels like a terminal, but provides a number of enhancements only |
|
13 | largely feels like a terminal, but provides a number of enhancements only | |
10 | possible in a GUI, such as inline figures, proper multiline editing with syntax |
|
14 | possible in a GUI, such as inline figures, proper multiline editing with syntax | |
11 | highlighting, graphical calltips, and much more. |
|
15 | highlighting, graphical calltips, and much more. | |
12 |
|
16 | |||
13 | .. figure:: ../_images/qtconsole.png |
|
17 | .. figure:: ../_images/qtconsole.png | |
14 | :width: 400px |
|
18 | :width: 400px | |
15 | :alt: IPython Qt console with embedded plots |
|
19 | :alt: IPython Qt console with embedded plots | |
16 | :align: center |
|
20 | :align: center | |
17 | :target: ../_images/qtconsole.png |
|
21 | :target: ../_images/qtconsole.png | |
18 |
|
22 | |||
19 | The Qt console for IPython, using inline matplotlib plots. |
|
23 | The Qt console for IPython, using inline matplotlib plots. | |
20 |
|
24 | |||
21 | To get acquainted with the Qt console, type `%guiref` to see a quick |
|
25 | To get acquainted with the Qt console, type `%guiref` to see a quick | |
22 | introduction of its main features. |
|
26 | introduction of its main features. | |
23 |
|
27 | |||
24 | The Qt frontend has hand-coded emacs-style bindings for text navigation. This |
|
28 | The Qt frontend has hand-coded emacs-style bindings for text navigation. This | |
25 | is not yet configurable. |
|
29 | is not yet configurable. | |
26 |
|
30 | |||
27 | .. tip:: |
|
31 | .. tip:: | |
28 |
|
32 | |||
29 | Since the Qt console tries hard to behave like a terminal, by default it |
|
33 | Since the Qt console tries hard to behave like a terminal, by default it | |
30 | immediately executes single lines of input that are complete. If you want |
|
34 | immediately executes single lines of input that are complete. If you want | |
31 | to force multiline input, hit :kbd:`Ctrl-Enter` at the end of the first line |
|
35 | to force multiline input, hit :kbd:`Ctrl-Enter` at the end of the first line | |
32 | instead of :kbd:`Enter`, and it will open a new line for input. At any |
|
36 | instead of :kbd:`Enter`, and it will open a new line for input. At any | |
33 | point in a multiline block, you can force its execution (without having to |
|
37 | point in a multiline block, you can force its execution (without having to | |
34 | go to the bottom) with :kbd:`Shift-Enter`. |
|
38 | go to the bottom) with :kbd:`Shift-Enter`. | |
35 |
|
39 | |||
36 | ``%load`` |
|
40 | ``%load`` | |
37 | ========= |
|
41 | ========= | |
38 |
|
42 | |||
39 | The new ``%load`` magic (previously ``%loadpy``) takes any script, and pastes |
|
43 | The new ``%load`` magic (previously ``%loadpy``) takes any script, and pastes | |
40 | its contents as your next input, so you can edit it before executing. The |
|
44 | its contents as your next input, so you can edit it before executing. The | |
41 | script may be on your machine, but you can also specify an history range, or a |
|
45 | script may be on your machine, but you can also specify an history range, or a | |
42 | url, and it will download the script from the web. This is particularly useful |
|
46 | url, and it will download the script from the web. This is particularly useful | |
43 | for playing with examples from documentation, such as matplotlib. |
|
47 | for playing with examples from documentation, such as matplotlib. | |
44 |
|
48 | |||
45 | .. sourcecode:: ipython |
|
49 | .. sourcecode:: ipython | |
46 |
|
50 | |||
47 | In [6]: %load http://matplotlib.org/plot_directive/mpl_examples/mplot3d/contour3d_demo.py |
|
51 | In [6]: %load http://matplotlib.org/plot_directive/mpl_examples/mplot3d/contour3d_demo.py | |
48 |
|
52 | |||
49 | In [7]: from mpl_toolkits.mplot3d import axes3d |
|
53 | In [7]: from mpl_toolkits.mplot3d import axes3d | |
50 | ...: import matplotlib.pyplot as plt |
|
54 | ...: import matplotlib.pyplot as plt | |
51 | ...: |
|
55 | ...: | |
52 | ...: fig = plt.figure() |
|
56 | ...: fig = plt.figure() | |
53 | ...: ax = fig.add_subplot(111, projection='3d') |
|
57 | ...: ax = fig.add_subplot(111, projection='3d') | |
54 | ...: X, Y, Z = axes3d.get_test_data(0.05) |
|
58 | ...: X, Y, Z = axes3d.get_test_data(0.05) | |
55 | ...: cset = ax.contour(X, Y, Z) |
|
59 | ...: cset = ax.contour(X, Y, Z) | |
56 | ...: ax.clabel(cset, fontsize=9, inline=1) |
|
60 | ...: ax.clabel(cset, fontsize=9, inline=1) | |
57 | ...: |
|
61 | ...: | |
58 | ...: plt.show() |
|
62 | ...: plt.show() | |
59 |
|
63 | |||
60 | The ``%load`` magic can also load source code from objects in the user or |
|
64 | The ``%load`` magic can also load source code from objects in the user or | |
61 | global namespace by invoking the ``-n`` option. |
|
65 | global namespace by invoking the ``-n`` option. | |
62 |
|
66 | |||
63 | .. sourcecode:: ipython |
|
67 | .. sourcecode:: ipython | |
64 |
|
68 | |||
65 | In [1]: import hello_world |
|
69 | In [1]: import hello_world | |
66 | ...: %load -n hello_world.say_hello |
|
70 | ...: %load -n hello_world.say_hello | |
67 |
|
71 | |||
68 | In [3]: def say_hello() : |
|
72 | In [3]: def say_hello() : | |
69 | ...: print("Hello World!") |
|
73 | ...: print("Hello World!") | |
70 |
|
74 | |||
71 | Inline Matplotlib |
|
75 | Inline Matplotlib | |
72 | ================= |
|
76 | ================= | |
73 |
|
77 | |||
74 | One of the most exciting features of the QtConsole is embedded matplotlib |
|
78 | One of the most exciting features of the QtConsole is embedded matplotlib | |
75 | figures. You can use any standard matplotlib GUI backend |
|
79 | figures. You can use any standard matplotlib GUI backend | |
76 | to draw the figures, and since there is now a two-process model, there is no |
|
80 | to draw the figures, and since there is now a two-process model, there is no | |
77 | longer a conflict between user input and the drawing eventloop. |
|
81 | longer a conflict between user input and the drawing eventloop. | |
78 |
|
82 | |||
79 | .. image:: figs/besselj.png |
|
83 | .. image:: figs/besselj.png | |
80 | :width: 519px |
|
84 | :width: 519px | |
81 |
|
85 | |||
82 | .. _display: |
|
86 | .. _display: | |
83 |
|
87 | |||
84 | :func:`display` |
|
88 | :func:`display` | |
85 | *************** |
|
89 | *************** | |
86 |
|
90 | |||
87 | IPython provides a function :func:`display` for displaying rich representations |
|
91 | IPython provides a function :func:`display` for displaying rich representations | |
88 | of objects if they are available. The IPython display |
|
92 | of objects if they are available. The IPython display | |
89 | system provides a mechanism for specifying PNG or SVG (and more) |
|
93 | system provides a mechanism for specifying PNG or SVG (and more) | |
90 | representations of objects for GUI frontends. |
|
94 | representations of objects for GUI frontends. | |
91 | When you enable matplotlib integration via the ``%matplotlib`` magic, IPython registers |
|
95 | When you enable matplotlib integration via the ``%matplotlib`` magic, IPython registers | |
92 | convenient PNG and SVG renderers for matplotlib figures, so you can embed them |
|
96 | convenient PNG and SVG renderers for matplotlib figures, so you can embed them | |
93 | in your document by calling :func:`display` on one or more of them. This is |
|
97 | in your document by calling :func:`display` on one or more of them. This is | |
94 | especially useful for saving_ your work. |
|
98 | especially useful for saving_ your work. | |
95 |
|
99 | |||
96 | .. sourcecode:: ipython |
|
100 | .. sourcecode:: ipython | |
97 |
|
101 | |||
98 | In [4]: from IPython.display import display |
|
102 | In [4]: from IPython.display import display | |
99 |
|
103 | |||
100 | In [5]: plt.plot(range(5)) # plots in the matplotlib window |
|
104 | In [5]: plt.plot(range(5)) # plots in the matplotlib window | |
101 |
|
105 | |||
102 | In [6]: display(plt.gcf()) # embeds the current figure in the qtconsole |
|
106 | In [6]: display(plt.gcf()) # embeds the current figure in the qtconsole | |
103 |
|
107 | |||
104 | In [7]: display(*getfigs()) # embeds all active figures in the qtconsole |
|
108 | In [7]: display(*getfigs()) # embeds all active figures in the qtconsole | |
105 |
|
109 | |||
106 | If you have a reference to a matplotlib figure object, you can always display |
|
110 | If you have a reference to a matplotlib figure object, you can always display | |
107 | that specific figure: |
|
111 | that specific figure: | |
108 |
|
112 | |||
109 | .. sourcecode:: ipython |
|
113 | .. sourcecode:: ipython | |
110 |
|
114 | |||
111 | In [1]: f = plt.figure() |
|
115 | In [1]: f = plt.figure() | |
112 |
|
116 | |||
113 | In [2]: plt.plot(np.rand(100)) |
|
117 | In [2]: plt.plot(np.rand(100)) | |
114 | Out[2]: [<matplotlib.lines.Line2D at 0x7fc6ac03dd90>] |
|
118 | Out[2]: [<matplotlib.lines.Line2D at 0x7fc6ac03dd90>] | |
115 |
|
119 | |||
116 | In [3]: display(f) |
|
120 | In [3]: display(f) | |
117 |
|
121 | |||
118 | # Plot is shown here |
|
122 | # Plot is shown here | |
119 |
|
123 | |||
120 | In [4]: plt.title('A title') |
|
124 | In [4]: plt.title('A title') | |
121 | Out[4]: <matplotlib.text.Text at 0x7fc6ac023450> |
|
125 | Out[4]: <matplotlib.text.Text at 0x7fc6ac023450> | |
122 |
|
126 | |||
123 | In [5]: display(f) |
|
127 | In [5]: display(f) | |
124 |
|
128 | |||
125 | # Updated plot with title is shown here. |
|
129 | # Updated plot with title is shown here. | |
126 |
|
130 | |||
127 | .. _inline: |
|
131 | .. _inline: | |
128 |
|
132 | |||
129 | ``--matplotlib inline`` |
|
133 | ``--matplotlib inline`` | |
130 | *********************** |
|
134 | *********************** | |
131 |
|
135 | |||
132 | If you want to have all of your figures embedded in your session, instead of |
|
136 | If you want to have all of your figures embedded in your session, instead of | |
133 | calling :func:`display`, you can specify ``--matplotlib inline`` when you start the |
|
137 | calling :func:`display`, you can specify ``--matplotlib inline`` when you start the | |
134 | console, and each time you make a plot, it will show up in your document, as if |
|
138 | console, and each time you make a plot, it will show up in your document, as if | |
135 | you had called :func:`display(fig)`. |
|
139 | you had called :func:`display(fig)`. | |
136 |
|
140 | |||
137 | The inline backend can use either SVG or PNG figures (PNG being the default). |
|
141 | The inline backend can use either SVG or PNG figures (PNG being the default). | |
138 | It also supports the special key ``'retina'``, which is 2x PNG for high-DPI displays. |
|
142 | It also supports the special key ``'retina'``, which is 2x PNG for high-DPI displays. | |
139 | To switch between them, set the ``InlineBackend.figure_format`` configurable |
|
143 | To switch between them, set the ``InlineBackend.figure_format`` configurable | |
140 | in a config file, or via the ``%config`` magic: |
|
144 | in a config file, or via the ``%config`` magic: | |
141 |
|
145 | |||
142 | .. sourcecode:: ipython |
|
146 | .. sourcecode:: ipython | |
143 |
|
147 | |||
144 | In [10]: %config InlineBackend.figure_format = 'svg' |
|
148 | In [10]: %config InlineBackend.figure_format = 'svg' | |
145 |
|
149 | |||
146 | .. note:: |
|
150 | .. note:: | |
147 |
|
151 | |||
148 | Changing the inline figure format also affects calls to :func:`display` above, |
|
152 | Changing the inline figure format also affects calls to :func:`display` above, | |
149 | even if you are not using the inline backend for all figures. |
|
153 | even if you are not using the inline backend for all figures. | |
150 |
|
154 | |||
151 | By default, IPython closes all figures at the completion of each execution. This means you |
|
155 | By default, IPython closes all figures at the completion of each execution. This means you | |
152 | don't have to manually close figures, which is less convenient when figures aren't attached |
|
156 | don't have to manually close figures, which is less convenient when figures aren't attached | |
153 | to windows with an obvious close button. It also means that the first matplotlib call in |
|
157 | to windows with an obvious close button. It also means that the first matplotlib call in | |
154 | each cell will always create a new figure: |
|
158 | each cell will always create a new figure: | |
155 |
|
159 | |||
156 | .. sourcecode:: ipython |
|
160 | .. sourcecode:: ipython | |
157 |
|
161 | |||
158 | In [11]: plt.plot(range(100)) |
|
162 | In [11]: plt.plot(range(100)) | |
159 | <single-line plot> |
|
163 | <single-line plot> | |
160 |
|
164 | |||
161 | In [12]: plt.plot([1,3,2]) |
|
165 | In [12]: plt.plot([1,3,2]) | |
162 | <another single-line plot> |
|
166 | <another single-line plot> | |
163 |
|
167 | |||
164 |
|
168 | |||
165 | However, it does prevent the list of active figures surviving from one input cell to the |
|
169 | However, it does prevent the list of active figures surviving from one input cell to the | |
166 | next, so if you want to continue working with a figure, you must hold on to a reference to |
|
170 | next, so if you want to continue working with a figure, you must hold on to a reference to | |
167 | it: |
|
171 | it: | |
168 |
|
172 | |||
169 | .. sourcecode:: ipython |
|
173 | .. sourcecode:: ipython | |
170 |
|
174 | |||
171 | In [11]: fig = gcf() |
|
175 | In [11]: fig = gcf() | |
172 | ....: fig.plot(rand(100)) |
|
176 | ....: fig.plot(rand(100)) | |
173 | <plot> |
|
177 | <plot> | |
174 | In [12]: fig.title('Random Title') |
|
178 | In [12]: fig.title('Random Title') | |
175 | <redraw plot with title> |
|
179 | <redraw plot with title> | |
176 |
|
180 | |||
177 | This behavior is controlled by the :attr:`InlineBackend.close_figures` configurable, and |
|
181 | This behavior is controlled by the :attr:`InlineBackend.close_figures` configurable, and | |
178 | if you set it to False, via %config or config file, then IPython will *not* close figures, |
|
182 | if you set it to False, via %config or config file, then IPython will *not* close figures, | |
179 | and tools like :func:`gcf`, :func:`gca`, :func:`getfigs` will behave the same as they |
|
183 | and tools like :func:`gcf`, :func:`gca`, :func:`getfigs` will behave the same as they | |
180 | do with other backends. You will, however, have to manually close figures: |
|
184 | do with other backends. You will, however, have to manually close figures: | |
181 |
|
185 | |||
182 | .. sourcecode:: ipython |
|
186 | .. sourcecode:: ipython | |
183 |
|
187 | |||
184 | # close all active figures: |
|
188 | # close all active figures: | |
185 | In [13]: [ fig.close() for fig in getfigs() ] |
|
189 | In [13]: [ fig.close() for fig in getfigs() ] | |
186 |
|
190 | |||
187 |
|
191 | |||
188 |
|
192 | |||
189 | .. _saving: |
|
193 | .. _saving: | |
190 |
|
194 | |||
191 | Saving and Printing |
|
195 | Saving and Printing | |
192 | =================== |
|
196 | =================== | |
193 |
|
197 | |||
194 | IPythonQt has the ability to save your current session, as either HTML or |
|
198 | IPythonQt has the ability to save your current session, as either HTML or | |
195 | XHTML. If you have been using :func:`display` or inline_ matplotlib, your figures |
|
199 | XHTML. If you have been using :func:`display` or inline_ matplotlib, your figures | |
196 | will be PNG in HTML, or inlined as SVG in XHTML. PNG images have the option to |
|
200 | will be PNG in HTML, or inlined as SVG in XHTML. PNG images have the option to | |
197 | be either in an external folder, as in many browsers' "Webpage, Complete" |
|
201 | be either in an external folder, as in many browsers' "Webpage, Complete" | |
198 | option, or inlined as well, for a larger, but more portable file. |
|
202 | option, or inlined as well, for a larger, but more portable file. | |
199 |
|
203 | |||
200 | .. note:: |
|
204 | .. note:: | |
201 |
|
205 | |||
202 | Export to SVG+XHTML requires that you are using SVG figures, which is *not* |
|
206 | Export to SVG+XHTML requires that you are using SVG figures, which is *not* | |
203 | the default. To switch the inline figure format to use SVG during an active |
|
207 | the default. To switch the inline figure format to use SVG during an active | |
204 | session, do: |
|
208 | session, do: | |
205 |
|
209 | |||
206 | .. sourcecode:: ipython |
|
210 | .. sourcecode:: ipython | |
207 |
|
211 | |||
208 | In [10]: %config InlineBackend.figure_format = 'svg' |
|
212 | In [10]: %config InlineBackend.figure_format = 'svg' | |
209 |
|
213 | |||
210 | Or, you can add the same line (c.Inline... instead of %config Inline...) to |
|
214 | Or, you can add the same line (c.Inline... instead of %config Inline...) to | |
211 | your config files. |
|
215 | your config files. | |
212 |
|
216 | |||
213 | This will only affect figures plotted after making this call |
|
217 | This will only affect figures plotted after making this call | |
214 |
|
218 | |||
215 |
|
219 | |||
216 | The widget also exposes the ability to print directly, via the default print |
|
220 | The widget also exposes the ability to print directly, via the default print | |
217 | shortcut or context menu. |
|
221 | shortcut or context menu. | |
218 |
|
222 | |||
219 |
|
223 | |||
220 | .. Note:: |
|
224 | .. Note:: | |
221 |
|
225 | |||
222 | Saving is only available to richtext Qt widgets, which are used by default, |
|
226 | Saving is only available to richtext Qt widgets, which are used by default, | |
223 | but if you pass the ``--plain`` flag, saving will not be available to you. |
|
227 | but if you pass the ``--plain`` flag, saving will not be available to you. | |
224 |
|
228 | |||
225 |
|
229 | |||
226 | See these examples of :download:`png/html<figs/jn.html>` and |
|
230 | See these examples of :download:`png/html<figs/jn.html>` and | |
227 | :download:`svg/xhtml <figs/jn.xhtml>` output. Note that syntax highlighting |
|
231 | :download:`svg/xhtml <figs/jn.xhtml>` output. Note that syntax highlighting | |
228 | does not survive export. This is a known issue, and is being investigated. |
|
232 | does not survive export. This is a known issue, and is being investigated. | |
229 |
|
233 | |||
230 |
|
234 | |||
231 | Colors and Highlighting |
|
235 | Colors and Highlighting | |
232 | ======================= |
|
236 | ======================= | |
233 |
|
237 | |||
234 | Terminal IPython has always had some coloring, but never syntax |
|
238 | Terminal IPython has always had some coloring, but never syntax | |
235 | highlighting. There are a few simple color choices, specified by the ``colors`` |
|
239 | highlighting. There are a few simple color choices, specified by the ``colors`` | |
236 | flag or ``%colors`` magic: |
|
240 | flag or ``%colors`` magic: | |
237 |
|
241 | |||
238 | * LightBG for light backgrounds |
|
242 | * LightBG for light backgrounds | |
239 | * Linux for dark backgrounds |
|
243 | * Linux for dark backgrounds | |
240 | * NoColor for a simple colorless terminal |
|
244 | * NoColor for a simple colorless terminal | |
241 |
|
245 | |||
242 | The Qt widget has full support for the ``colors`` flag used in the terminal shell. |
|
246 | The Qt widget has full support for the ``colors`` flag used in the terminal shell. | |
243 |
|
247 | |||
244 | The Qt widget, however, has full syntax highlighting as you type, handled by |
|
248 | The Qt widget, however, has full syntax highlighting as you type, handled by | |
245 | the `pygments`_ library. The ``style`` argument exposes access to any style by |
|
249 | the `pygments`_ library. The ``style`` argument exposes access to any style by | |
246 | name that can be found by pygments, and there are several already |
|
250 | name that can be found by pygments, and there are several already | |
247 | installed. The ``colors`` argument, if unspecified, will be guessed based on |
|
251 | installed. The ``colors`` argument, if unspecified, will be guessed based on | |
248 | the chosen style. Similarly, there are default styles associated with each |
|
252 | the chosen style. Similarly, there are default styles associated with each | |
249 | ``colors`` option. |
|
253 | ``colors`` option. | |
250 |
|
254 | |||
251 |
|
255 | |||
252 | Screenshot of ``ipython qtconsole --colors=linux``, which uses the 'monokai' |
|
256 | Screenshot of ``ipython qtconsole --colors=linux``, which uses the 'monokai' | |
253 | theme by default: |
|
257 | theme by default: | |
254 |
|
258 | |||
255 | .. image:: figs/colors_dark.png |
|
259 | .. image:: figs/colors_dark.png | |
256 | :width: 627px |
|
260 | :width: 627px | |
257 |
|
261 | |||
258 | .. Note:: |
|
262 | .. Note:: | |
259 |
|
263 | |||
260 | Calling ``ipython qtconsole -h`` will show all the style names that |
|
264 | Calling ``ipython qtconsole -h`` will show all the style names that | |
261 | pygments can find on your system. |
|
265 | pygments can find on your system. | |
262 |
|
266 | |||
263 | You can also pass the filename of a custom CSS stylesheet, if you want to do |
|
267 | You can also pass the filename of a custom CSS stylesheet, if you want to do | |
264 | your own coloring, via the ``stylesheet`` argument. The default LightBG |
|
268 | your own coloring, via the ``stylesheet`` argument. The default LightBG | |
265 | stylesheet: |
|
269 | stylesheet: | |
266 |
|
270 | |||
267 | .. sourcecode:: css |
|
271 | .. sourcecode:: css | |
268 |
|
272 | |||
269 | QPlainTextEdit, QTextEdit { background-color: white; |
|
273 | QPlainTextEdit, QTextEdit { background-color: white; | |
270 | color: black ; |
|
274 | color: black ; | |
271 | selection-background-color: #ccc} |
|
275 | selection-background-color: #ccc} | |
272 | .error { color: red; } |
|
276 | .error { color: red; } | |
273 | .in-prompt { color: navy; } |
|
277 | .in-prompt { color: navy; } | |
274 | .in-prompt-number { font-weight: bold; } |
|
278 | .in-prompt-number { font-weight: bold; } | |
275 | .out-prompt { color: darkred; } |
|
279 | .out-prompt { color: darkred; } | |
276 | .out-prompt-number { font-weight: bold; } |
|
280 | .out-prompt-number { font-weight: bold; } | |
277 | /* .inverted is used to highlight selected completion */ |
|
281 | /* .inverted is used to highlight selected completion */ | |
278 | .inverted { background-color: black ; color: white; } |
|
282 | .inverted { background-color: black ; color: white; } | |
279 |
|
283 | |||
280 | Fonts |
|
284 | Fonts | |
281 | ===== |
|
285 | ===== | |
282 |
|
286 | |||
283 | The QtConsole has configurable via the ConsoleWidget. To change these, set the |
|
287 | The QtConsole has configurable via the ConsoleWidget. To change these, set the | |
284 | ``font_family`` or ``font_size`` traits of the ConsoleWidget. For instance, to |
|
288 | ``font_family`` or ``font_size`` traits of the ConsoleWidget. For instance, to | |
285 | use 9pt Anonymous Pro:: |
|
289 | use 9pt Anonymous Pro:: | |
286 |
|
290 | |||
287 | $> ipython qtconsole --ConsoleWidget.font_family="Anonymous Pro" --ConsoleWidget.font_size=9 |
|
291 | $> ipython qtconsole --ConsoleWidget.font_family="Anonymous Pro" --ConsoleWidget.font_size=9 | |
288 |
|
292 | |||
289 | Process Management |
|
293 | Process Management | |
290 | ================== |
|
294 | ================== | |
291 |
|
295 | |||
292 | With the two-process ZMQ model, the frontend does not block input during |
|
296 | With the two-process ZMQ model, the frontend does not block input during | |
293 | execution. This means that actions can be taken by the frontend while the |
|
297 | execution. This means that actions can be taken by the frontend while the | |
294 | Kernel is executing, or even after it crashes. The most basic such command is |
|
298 | Kernel is executing, or even after it crashes. The most basic such command is | |
295 | via 'Ctrl-.', which restarts the kernel. This can be done in the middle of a |
|
299 | via 'Ctrl-.', which restarts the kernel. This can be done in the middle of a | |
296 | blocking execution. The frontend can also know, via a heartbeat mechanism, that |
|
300 | blocking execution. The frontend can also know, via a heartbeat mechanism, that | |
297 | the kernel has died. This means that the frontend can safely restart the |
|
301 | the kernel has died. This means that the frontend can safely restart the | |
298 | kernel. |
|
302 | kernel. | |
299 |
|
303 | |||
300 | .. _multiple_consoles: |
|
304 | .. _multiple_consoles: | |
301 |
|
305 | |||
302 | Multiple Consoles |
|
306 | Multiple Consoles | |
303 | ***************** |
|
307 | ***************** | |
304 |
|
308 | |||
305 | Since the Kernel listens on the network, multiple frontends can connect to it. |
|
309 | Since the Kernel listens on the network, multiple frontends can connect to it. | |
306 | These do not have to all be qt frontends - any IPython frontend can connect and |
|
310 | These do not have to all be qt frontends - any IPython frontend can connect and | |
307 | run code. When you start ipython qtconsole, there will be an output line, |
|
311 | run code. When you start ipython qtconsole, there will be an output line, | |
308 | like:: |
|
312 | like:: | |
309 |
|
313 | |||
310 | [IPKernelApp] To connect another client to this kernel, use: |
|
314 | [IPKernelApp] To connect another client to this kernel, use: | |
311 | [IPKernelApp] --existing kernel-12345.json |
|
315 | [IPKernelApp] --existing kernel-12345.json | |
312 |
|
316 | |||
313 | Other frontends can connect to your kernel, and share in the execution. This is |
|
317 | Other frontends can connect to your kernel, and share in the execution. This is | |
314 | great for collaboration. The ``--existing`` flag means connect to a kernel |
|
318 | great for collaboration. The ``--existing`` flag means connect to a kernel | |
315 | that already exists. Starting other consoles |
|
319 | that already exists. Starting other consoles | |
316 | with that flag will not try to start their own kernel, but rather connect to |
|
320 | with that flag will not try to start their own kernel, but rather connect to | |
317 | yours. :file:`kernel-12345.json` is a small JSON file with the ip, port, and |
|
321 | yours. :file:`kernel-12345.json` is a small JSON file with the ip, port, and | |
318 | authentication information necessary to connect to your kernel. By default, this file |
|
322 | authentication information necessary to connect to your kernel. By default, this file | |
319 | will be in your default profile's security directory. If it is somewhere else, |
|
323 | will be in your default profile's security directory. If it is somewhere else, | |
320 | the output line will print the full path of the connection file, rather than |
|
324 | the output line will print the full path of the connection file, rather than | |
321 | just its filename. |
|
325 | just its filename. | |
322 |
|
326 | |||
323 | If you need to find the connection info to send, and don't know where your connection file |
|
327 | If you need to find the connection info to send, and don't know where your connection file | |
324 | lives, there are a couple of ways to get it. If you are already running an IPython console |
|
328 | lives, there are a couple of ways to get it. If you are already running an IPython console | |
325 | connected to the kernel, you can use the ``%connect_info`` magic to display the information |
|
329 | connected to the kernel, you can use the ``%connect_info`` magic to display the information | |
326 | necessary to connect another frontend to the kernel. |
|
330 | necessary to connect another frontend to the kernel. | |
327 |
|
331 | |||
328 | .. sourcecode:: ipython |
|
332 | .. sourcecode:: ipython | |
329 |
|
333 | |||
330 | In [2]: %connect_info |
|
334 | In [2]: %connect_info | |
331 | { |
|
335 | { | |
332 | "stdin_port":50255, |
|
336 | "stdin_port":50255, | |
333 | "ip":"127.0.0.1", |
|
337 | "ip":"127.0.0.1", | |
334 | "hb_port":50256, |
|
338 | "hb_port":50256, | |
335 | "key":"70be6f0f-1564-4218-8cda-31be40a4d6aa", |
|
339 | "key":"70be6f0f-1564-4218-8cda-31be40a4d6aa", | |
336 | "shell_port":50253, |
|
340 | "shell_port":50253, | |
337 | "iopub_port":50254 |
|
341 | "iopub_port":50254 | |
338 | } |
|
342 | } | |
339 |
|
343 | |||
340 | Paste the above JSON into a file, and connect with: |
|
344 | Paste the above JSON into a file, and connect with: | |
341 | $> ipython <app> --existing <file> |
|
345 | $> ipython <app> --existing <file> | |
342 | or, if you are local, you can connect with just: |
|
346 | or, if you are local, you can connect with just: | |
343 | $> ipython <app> --existing kernel-12345.json |
|
347 | $> ipython <app> --existing kernel-12345.json | |
344 | or even just: |
|
348 | or even just: | |
345 | $> ipython <app> --existing |
|
349 | $> ipython <app> --existing | |
346 | if this is the most recent IPython session you have started. |
|
350 | if this is the most recent IPython session you have started. | |
347 |
|
351 | |||
348 | Otherwise, you can find a connection file by name (and optionally profile) with |
|
352 | Otherwise, you can find a connection file by name (and optionally profile) with | |
349 | :func:`IPython.lib.kernel.find_connection_file`: |
|
353 | :func:`IPython.lib.kernel.find_connection_file`: | |
350 |
|
354 | |||
351 | .. sourcecode:: bash |
|
355 | .. sourcecode:: bash | |
352 |
|
356 | |||
353 | $> python -c "from IPython.lib.kernel import find_connection_file;\ |
|
357 | $> python -c "from IPython.lib.kernel import find_connection_file;\ | |
354 | print find_connection_file('kernel-12345.json')" |
|
358 | print find_connection_file('kernel-12345.json')" | |
355 | /home/you/.ipython/profile_default/security/kernel-12345.json |
|
359 | /home/you/.ipython/profile_default/security/kernel-12345.json | |
356 |
|
360 | |||
357 | And if you are using a particular IPython profile: |
|
361 | And if you are using a particular IPython profile: | |
358 |
|
362 | |||
359 | .. sourcecode:: bash |
|
363 | .. sourcecode:: bash | |
360 |
|
364 | |||
361 | $> python -c "from IPython.lib.kernel import find_connection_file;\ |
|
365 | $> python -c "from IPython.lib.kernel import find_connection_file;\ | |
362 | print find_connection_file('kernel-12345.json', profile='foo')" |
|
366 | print find_connection_file('kernel-12345.json', profile='foo')" | |
363 | /home/you/.ipython/profile_foo/security/kernel-12345.json |
|
367 | /home/you/.ipython/profile_foo/security/kernel-12345.json | |
364 |
|
368 | |||
365 | You can even launch a standalone kernel, and connect and disconnect Qt Consoles |
|
369 | You can even launch a standalone kernel, and connect and disconnect Qt Consoles | |
366 | from various machines. This lets you keep the same running IPython session |
|
370 | from various machines. This lets you keep the same running IPython session | |
367 | on your work machine (with matplotlib plots and everything), logging in from home, |
|
371 | on your work machine (with matplotlib plots and everything), logging in from home, | |
368 | cafΓ©s, etc.:: |
|
372 | cafΓ©s, etc.:: | |
369 |
|
373 | |||
370 | $> ipython kernel |
|
374 | $> ipython kernel | |
371 | [IPKernelApp] To connect another client to this kernel, use: |
|
375 | [IPKernelApp] To connect another client to this kernel, use: | |
372 | [IPKernelApp] --existing kernel-12345.json |
|
376 | [IPKernelApp] --existing kernel-12345.json | |
373 |
|
377 | |||
374 | This is actually exactly the same as the subprocess launched by the qtconsole, so |
|
378 | This is actually exactly the same as the subprocess launched by the qtconsole, so | |
375 | all the information about connecting to a standalone kernel is identical to that |
|
379 | all the information about connecting to a standalone kernel is identical to that | |
376 | of connecting to the kernel attached to a running console. |
|
380 | of connecting to the kernel attached to a running console. | |
377 |
|
381 | |||
378 | .. _kernel_security: |
|
382 | .. _kernel_security: | |
379 |
|
383 | |||
380 | Security |
|
384 | Security | |
381 | -------- |
|
385 | -------- | |
382 |
|
386 | |||
383 | .. warning:: |
|
387 | .. warning:: | |
384 |
|
388 | |||
385 | Since the ZMQ code currently has no encryption, listening on an |
|
389 | Since the ZMQ code currently has no encryption, listening on an | |
386 | external-facing IP is dangerous. You are giving any computer that can see |
|
390 | external-facing IP is dangerous. You are giving any computer that can see | |
387 | you on the network the ability to connect to your kernel, and view your traffic. |
|
391 | you on the network the ability to connect to your kernel, and view your traffic. | |
388 | Read the rest of this section before listening on external ports |
|
392 | Read the rest of this section before listening on external ports | |
389 | or running an IPython kernel on a shared machine. |
|
393 | or running an IPython kernel on a shared machine. | |
390 |
|
394 | |||
391 | By default (for security reasons), the kernel only listens on localhost, so you |
|
395 | By default (for security reasons), the kernel only listens on localhost, so you | |
392 | can only connect multiple frontends to the kernel from your local machine. You |
|
396 | can only connect multiple frontends to the kernel from your local machine. You | |
393 | can specify to listen on an external interface by specifying the ``ip`` |
|
397 | can specify to listen on an external interface by specifying the ``ip`` | |
394 | argument:: |
|
398 | argument:: | |
395 |
|
399 | |||
396 | $> ipython qtconsole --ip=192.168.1.123 |
|
400 | $> ipython qtconsole --ip=192.168.1.123 | |
397 |
|
401 | |||
398 | If you specify the ip as 0.0.0.0 or '*', that means all interfaces, so any |
|
402 | If you specify the ip as 0.0.0.0 or '*', that means all interfaces, so any | |
399 | computer that can see yours on the network can connect to the kernel. |
|
403 | computer that can see yours on the network can connect to the kernel. | |
400 |
|
404 | |||
401 | Messages are not encrypted, so users with access to the ports your kernel is using will be |
|
405 | Messages are not encrypted, so users with access to the ports your kernel is using will be | |
402 | able to see any output of the kernel. They will **NOT** be able to issue shell commands as |
|
406 | able to see any output of the kernel. They will **NOT** be able to issue shell commands as | |
403 | you due to message signatures, which are enabled by default as of IPython 0.12. |
|
407 | you due to message signatures, which are enabled by default as of IPython 0.12. | |
404 |
|
408 | |||
405 | .. warning:: |
|
409 | .. warning:: | |
406 |
|
410 | |||
407 | If you disable message signatures, then any user with access to the ports your |
|
411 | If you disable message signatures, then any user with access to the ports your | |
408 | kernel is listening on can issue arbitrary code as you. **DO NOT** disable message |
|
412 | kernel is listening on can issue arbitrary code as you. **DO NOT** disable message | |
409 | signatures unless you have a lot of trust in your environment. |
|
413 | signatures unless you have a lot of trust in your environment. | |
410 |
|
414 | |||
411 | The one security feature IPython does provide is protection from unauthorized execution. |
|
415 | The one security feature IPython does provide is protection from unauthorized execution. | |
412 | IPython's messaging system will sign messages with HMAC digests using a shared-key. The key |
|
416 | IPython's messaging system will sign messages with HMAC digests using a shared-key. The key | |
413 | is never sent over the network, it is only used to generate a unique hash for each message, |
|
417 | is never sent over the network, it is only used to generate a unique hash for each message, | |
414 | based on its content. When IPython receives a message, it will check that the digest |
|
418 | based on its content. When IPython receives a message, it will check that the digest | |
415 | matches, and discard the message. You can use any file that only you have access to to |
|
419 | matches, and discard the message. You can use any file that only you have access to to | |
416 | generate this key, but the default is just to generate a new UUID. You can generate a random |
|
420 | generate this key, but the default is just to generate a new UUID. You can generate a random | |
417 | private key with:: |
|
421 | private key with:: | |
418 |
|
422 | |||
419 | # generate 1024b of random data, and store in a file only you can read: |
|
423 | # generate 1024b of random data, and store in a file only you can read: | |
420 | # (assumes IPYTHONDIR is defined, otherwise use your IPython directory) |
|
424 | # (assumes IPYTHONDIR is defined, otherwise use your IPython directory) | |
421 | $> python -c "import os; print os.urandom(128).encode('base64')" > $IPYTHONDIR/sessionkey |
|
425 | $> python -c "import os; print os.urandom(128).encode('base64')" > $IPYTHONDIR/sessionkey | |
422 | $> chmod 600 $IPYTHONDIR/sessionkey |
|
426 | $> chmod 600 $IPYTHONDIR/sessionkey | |
423 |
|
427 | |||
424 | The *contents* of this file will be stored in the JSON connection file, so that file |
|
428 | The *contents* of this file will be stored in the JSON connection file, so that file | |
425 | contains everything you need to connect to and use a kernel. |
|
429 | contains everything you need to connect to and use a kernel. | |
426 |
|
430 | |||
427 | To use this generated key, simply specify the ``Session.keyfile`` configurable |
|
431 | To use this generated key, simply specify the ``Session.keyfile`` configurable | |
428 | in :file:`ipython_config.py` or at the command-line, as in:: |
|
432 | in :file:`ipython_config.py` or at the command-line, as in:: | |
429 |
|
433 | |||
430 | # instruct IPython to sign messages with that key, instead of a new UUID |
|
434 | # instruct IPython to sign messages with that key, instead of a new UUID | |
431 | $> ipython qtconsole --Session.keyfile=$IPYTHONDIR/sessionkey |
|
435 | $> ipython qtconsole --Session.keyfile=$IPYTHONDIR/sessionkey | |
432 |
|
436 | |||
433 | .. _ssh_tunnels: |
|
437 | .. _ssh_tunnels: | |
434 |
|
438 | |||
435 | SSH Tunnels |
|
439 | SSH Tunnels | |
436 | ----------- |
|
440 | ----------- | |
437 |
|
441 | |||
438 | Sometimes you want to connect to machines across the internet, or just across |
|
442 | Sometimes you want to connect to machines across the internet, or just across | |
439 | a LAN that either doesn't permit open ports or you don't trust the other |
|
443 | a LAN that either doesn't permit open ports or you don't trust the other | |
440 | machines on the network. To do this, you can use SSH tunnels. SSH tunnels |
|
444 | machines on the network. To do this, you can use SSH tunnels. SSH tunnels | |
441 | are a way to securely forward ports on your local machine to ports on another |
|
445 | are a way to securely forward ports on your local machine to ports on another | |
442 | machine, to which you have SSH access. |
|
446 | machine, to which you have SSH access. | |
443 |
|
447 | |||
444 | In simple cases, IPython's tools can forward ports over ssh by simply adding the |
|
448 | In simple cases, IPython's tools can forward ports over ssh by simply adding the | |
445 | ``--ssh=remote`` argument to the usual ``--existing...`` set of flags for connecting |
|
449 | ``--ssh=remote`` argument to the usual ``--existing...`` set of flags for connecting | |
446 | to a running kernel, after copying the JSON connection file (or its contents) to |
|
450 | to a running kernel, after copying the JSON connection file (or its contents) to | |
447 | the second computer. |
|
451 | the second computer. | |
448 |
|
452 | |||
449 | .. warning:: |
|
453 | .. warning:: | |
450 |
|
454 | |||
451 | Using SSH tunnels does *not* increase localhost security. In fact, when |
|
455 | Using SSH tunnels does *not* increase localhost security. In fact, when | |
452 | tunneling from one machine to another *both* machines have open |
|
456 | tunneling from one machine to another *both* machines have open | |
453 | ports on localhost available for connections to the kernel. |
|
457 | ports on localhost available for connections to the kernel. | |
454 |
|
458 | |||
455 | There are two primary models for using SSH tunnels with IPython. The first |
|
459 | There are two primary models for using SSH tunnels with IPython. The first | |
456 | is to have the Kernel listen only on localhost, and connect to it from |
|
460 | is to have the Kernel listen only on localhost, and connect to it from | |
457 | another machine on the same LAN. |
|
461 | another machine on the same LAN. | |
458 |
|
462 | |||
459 | First, let's start a kernel on machine **worker**, listening only |
|
463 | First, let's start a kernel on machine **worker**, listening only | |
460 | on loopback:: |
|
464 | on loopback:: | |
461 |
|
465 | |||
462 | user@worker $> ipython kernel |
|
466 | user@worker $> ipython kernel | |
463 | [IPKernelApp] To connect another client to this kernel, use: |
|
467 | [IPKernelApp] To connect another client to this kernel, use: | |
464 | [IPKernelApp] --existing kernel-12345.json |
|
468 | [IPKernelApp] --existing kernel-12345.json | |
465 |
|
469 | |||
466 | In this case, the IP that you would connect |
|
470 | In this case, the IP that you would connect | |
467 | to would still be 127.0.0.1, but you want to specify the additional ``--ssh`` argument |
|
471 | to would still be 127.0.0.1, but you want to specify the additional ``--ssh`` argument | |
468 | with the hostname of the kernel (in this example, it's 'worker'):: |
|
472 | with the hostname of the kernel (in this example, it's 'worker'):: | |
469 |
|
473 | |||
470 | user@client $> ipython qtconsole --ssh=worker --existing /path/to/kernel-12345.json |
|
474 | user@client $> ipython qtconsole --ssh=worker --existing /path/to/kernel-12345.json | |
471 |
|
475 | |||
472 | Which will write a new connection file with the forwarded ports, so you can reuse them:: |
|
476 | Which will write a new connection file with the forwarded ports, so you can reuse them:: | |
473 |
|
477 | |||
474 | [IPythonQtConsoleApp] To connect another client via this tunnel, use: |
|
478 | [IPythonQtConsoleApp] To connect another client via this tunnel, use: | |
475 | [IPythonQtConsoleApp] --existing kernel-12345-ssh.json |
|
479 | [IPythonQtConsoleApp] --existing kernel-12345-ssh.json | |
476 |
|
480 | |||
477 | Note again that this opens ports on the *client* machine that point to your kernel. |
|
481 | Note again that this opens ports on the *client* machine that point to your kernel. | |
478 |
|
482 | |||
479 | .. note:: |
|
483 | .. note:: | |
480 |
|
484 | |||
481 | the ssh argument is simply passed to openssh, so it can be fully specified ``user@host:port`` |
|
485 | the ssh argument is simply passed to openssh, so it can be fully specified ``user@host:port`` | |
482 | but it will also respect your aliases, etc. in :file:`.ssh/config` if you have any. |
|
486 | but it will also respect your aliases, etc. in :file:`.ssh/config` if you have any. | |
483 |
|
487 | |||
484 | The second pattern is for connecting to a machine behind a firewall across the internet |
|
488 | The second pattern is for connecting to a machine behind a firewall across the internet | |
485 | (or otherwise wide network). This time, we have a machine **login** that you have ssh access |
|
489 | (or otherwise wide network). This time, we have a machine **login** that you have ssh access | |
486 | to, which can see **kernel**, but **client** is on another network. The important difference |
|
490 | to, which can see **kernel**, but **client** is on another network. The important difference | |
487 | now is that **client** can see **login**, but *not* **worker**. So we need to forward ports from |
|
491 | now is that **client** can see **login**, but *not* **worker**. So we need to forward ports from | |
488 | client to worker *via* login. This means that the kernel must be started listening |
|
492 | client to worker *via* login. This means that the kernel must be started listening | |
489 | on external interfaces, so that its ports are visible to `login`:: |
|
493 | on external interfaces, so that its ports are visible to `login`:: | |
490 |
|
494 | |||
491 | user@worker $> ipython kernel --ip=0.0.0.0 |
|
495 | user@worker $> ipython kernel --ip=0.0.0.0 | |
492 | [IPKernelApp] To connect another client to this kernel, use: |
|
496 | [IPKernelApp] To connect another client to this kernel, use: | |
493 | [IPKernelApp] --existing kernel-12345.json |
|
497 | [IPKernelApp] --existing kernel-12345.json | |
494 |
|
498 | |||
495 | Which we can connect to from the client with:: |
|
499 | Which we can connect to from the client with:: | |
496 |
|
500 | |||
497 | user@client $> ipython qtconsole --ssh=login --ip=192.168.1.123 --existing /path/to/kernel-12345.json |
|
501 | user@client $> ipython qtconsole --ssh=login --ip=192.168.1.123 --existing /path/to/kernel-12345.json | |
498 |
|
502 | |||
499 | .. note:: |
|
503 | .. note:: | |
500 |
|
504 | |||
501 | The IP here is the address of worker as seen from *login*, and need only be specified if |
|
505 | The IP here is the address of worker as seen from *login*, and need only be specified if | |
502 | the kernel used the ambiguous 0.0.0.0 (all interfaces) address. If it had used |
|
506 | the kernel used the ambiguous 0.0.0.0 (all interfaces) address. If it had used | |
503 | 192.168.1.123 to start with, it would not be needed. |
|
507 | 192.168.1.123 to start with, it would not be needed. | |
504 |
|
508 | |||
505 |
|
509 | |||
506 | Manual SSH tunnels |
|
510 | Manual SSH tunnels | |
507 | ------------------ |
|
511 | ------------------ | |
508 |
|
512 | |||
509 | It's possible that IPython's ssh helper functions won't work for you, for various |
|
513 | It's possible that IPython's ssh helper functions won't work for you, for various | |
510 | reasons. You can still connect to remote machines, as long as you set up the tunnels |
|
514 | reasons. You can still connect to remote machines, as long as you set up the tunnels | |
511 | yourself. The basic format of forwarding a local port to a remote one is:: |
|
515 | yourself. The basic format of forwarding a local port to a remote one is:: | |
512 |
|
516 | |||
513 | [client] $> ssh <server> <localport>:<remoteip>:<remoteport> -f -N |
|
517 | [client] $> ssh <server> <localport>:<remoteip>:<remoteport> -f -N | |
514 |
|
518 | |||
515 | This will forward local connections to **localport** on client to **remoteip:remoteport** |
|
519 | This will forward local connections to **localport** on client to **remoteip:remoteport** | |
516 | *via* **server**. Note that remoteip is interpreted relative to *server*, not the client. |
|
520 | *via* **server**. Note that remoteip is interpreted relative to *server*, not the client. | |
517 | So if you have direct ssh access to the machine to which you want to forward connections, |
|
521 | So if you have direct ssh access to the machine to which you want to forward connections, | |
518 | then the server *is* the remote machine, and remoteip should be server's IP as seen from the |
|
522 | then the server *is* the remote machine, and remoteip should be server's IP as seen from the | |
519 | server itself, i.e. 127.0.0.1. Thus, to forward local port 12345 to remote port 54321 on |
|
523 | server itself, i.e. 127.0.0.1. Thus, to forward local port 12345 to remote port 54321 on | |
520 | a machine you can see, do:: |
|
524 | a machine you can see, do:: | |
521 |
|
525 | |||
522 | [client] $> ssh machine 12345:127.0.0.1:54321 -f -N |
|
526 | [client] $> ssh machine 12345:127.0.0.1:54321 -f -N | |
523 |
|
527 | |||
524 | But if your target is actually on a LAN at 192.168.1.123, behind another machine called **login**, |
|
528 | But if your target is actually on a LAN at 192.168.1.123, behind another machine called **login**, | |
525 | then you would do:: |
|
529 | then you would do:: | |
526 |
|
530 | |||
527 | [client] $> ssh login 12345:192.168.1.16:54321 -f -N |
|
531 | [client] $> ssh login 12345:192.168.1.16:54321 -f -N | |
528 |
|
532 | |||
529 | The ``-f -N`` on the end are flags that tell ssh to run in the background, |
|
533 | The ``-f -N`` on the end are flags that tell ssh to run in the background, | |
530 | and don't actually run any commands beyond creating the tunnel. |
|
534 | and don't actually run any commands beyond creating the tunnel. | |
531 |
|
535 | |||
532 | .. seealso:: |
|
536 | .. seealso:: | |
533 |
|
537 | |||
534 | A short discussion of ssh tunnels: http://www.revsys.com/writings/quicktips/ssh-tunnel.html |
|
538 | A short discussion of ssh tunnels: http://www.revsys.com/writings/quicktips/ssh-tunnel.html | |
535 |
|
539 | |||
536 |
|
540 | |||
537 |
|
541 | |||
538 | Stopping Kernels and Consoles |
|
542 | Stopping Kernels and Consoles | |
539 | ***************************** |
|
543 | ***************************** | |
540 |
|
544 | |||
541 | Since there can be many consoles per kernel, the shutdown mechanism and dialog |
|
545 | Since there can be many consoles per kernel, the shutdown mechanism and dialog | |
542 | are probably more complicated than you are used to. Since you don't always want |
|
546 | are probably more complicated than you are used to. Since you don't always want | |
543 | to shutdown a kernel when you close a window, you are given the option to just |
|
547 | to shutdown a kernel when you close a window, you are given the option to just | |
544 | close the console window or also close the Kernel and *all other windows*. Note |
|
548 | close the console window or also close the Kernel and *all other windows*. Note | |
545 | that this only refers to all other *local* windows, as remote Consoles are not |
|
549 | that this only refers to all other *local* windows, as remote Consoles are not | |
546 | allowed to shutdown the kernel, and shutdowns do not close Remote consoles (to |
|
550 | allowed to shutdown the kernel, and shutdowns do not close Remote consoles (to | |
547 | allow for saving, etc.). |
|
551 | allow for saving, etc.). | |
548 |
|
552 | |||
549 | Rules: |
|
553 | Rules: | |
550 |
|
554 | |||
551 | * Restarting the kernel automatically clears all *local* Consoles, and prompts remote |
|
555 | * Restarting the kernel automatically clears all *local* Consoles, and prompts remote | |
552 | Consoles about the reset. |
|
556 | Consoles about the reset. | |
553 | * Shutdown closes all *local* Consoles, and notifies remotes that |
|
557 | * Shutdown closes all *local* Consoles, and notifies remotes that | |
554 | the Kernel has been shutdown. |
|
558 | the Kernel has been shutdown. | |
555 | * Remote Consoles may not restart or shutdown the kernel. |
|
559 | * Remote Consoles may not restart or shutdown the kernel. | |
556 |
|
560 | |||
557 | Qt and the QtConsole |
|
561 | Qt and the QtConsole | |
558 | ==================== |
|
562 | ==================== | |
559 |
|
563 | |||
560 | An important part of working with the QtConsole when you are writing your own |
|
564 | An important part of working with the QtConsole when you are writing your own | |
561 | Qt code is to remember that user code (in the kernel) is *not* in the same |
|
565 | Qt code is to remember that user code (in the kernel) is *not* in the same | |
562 | process as the frontend. This means that there is not necessarily any Qt code |
|
566 | process as the frontend. This means that there is not necessarily any Qt code | |
563 | running in the kernel, and under most normal circumstances there isn't. If, |
|
567 | running in the kernel, and under most normal circumstances there isn't. If, | |
564 | however, you specify ``--matplotlib qt`` at the command-line, then there *will* be a |
|
568 | however, you specify ``--matplotlib qt`` at the command-line, then there *will* be a | |
565 | :class:`QCoreApplication` instance running in the kernel process along with |
|
569 | :class:`QCoreApplication` instance running in the kernel process along with | |
566 | user-code. To get a reference to this application, do: |
|
570 | user-code. To get a reference to this application, do: | |
567 |
|
571 | |||
568 | .. sourcecode:: python |
|
572 | .. sourcecode:: python | |
569 |
|
573 | |||
570 | from PyQt4 import QtCore |
|
574 | from PyQt4 import QtCore | |
571 | app = QtCore.QCoreApplication.instance() |
|
575 | app = QtCore.QCoreApplication.instance() | |
572 | # app will be None if there is no such instance |
|
576 | # app will be None if there is no such instance | |
573 |
|
577 | |||
574 | A common problem listed in the PyQt4 Gotchas_ is the fact that Python's garbage |
|
578 | A common problem listed in the PyQt4 Gotchas_ is the fact that Python's garbage | |
575 | collection will destroy Qt objects (Windows, etc.) once there is no longer a |
|
579 | collection will destroy Qt objects (Windows, etc.) once there is no longer a | |
576 | Python reference to them, so you have to hold on to them. For instance, in: |
|
580 | Python reference to them, so you have to hold on to them. For instance, in: | |
577 |
|
581 | |||
578 | .. sourcecode:: python |
|
582 | .. sourcecode:: python | |
579 |
|
583 | |||
580 | def make_window(): |
|
584 | def make_window(): | |
581 | win = QtGui.QMainWindow() |
|
585 | win = QtGui.QMainWindow() | |
582 |
|
586 | |||
583 | def make_and_return_window(): |
|
587 | def make_and_return_window(): | |
584 | win = QtGui.QMainWindow() |
|
588 | win = QtGui.QMainWindow() | |
585 | return win |
|
589 | return win | |
586 |
|
590 | |||
587 | :func:`make_window` will never draw a window, because garbage collection will |
|
591 | :func:`make_window` will never draw a window, because garbage collection will | |
588 | destroy it before it is drawn, whereas :func:`make_and_return_window` lets the |
|
592 | destroy it before it is drawn, whereas :func:`make_and_return_window` lets the | |
589 | caller decide when the window object should be destroyed. If, as a developer, |
|
593 | caller decide when the window object should be destroyed. If, as a developer, | |
590 | you know that you always want your objects to last as long as the process, you |
|
594 | you know that you always want your objects to last as long as the process, you | |
591 | can attach them to the QApplication instance itself: |
|
595 | can attach them to the QApplication instance itself: | |
592 |
|
596 | |||
593 | .. sourcecode:: python |
|
597 | .. sourcecode:: python | |
594 |
|
598 | |||
595 | # do this just once: |
|
599 | # do this just once: | |
596 | app = QtCore.QCoreApplication.instance() |
|
600 | app = QtCore.QCoreApplication.instance() | |
597 | app.references = set() |
|
601 | app.references = set() | |
598 | # then when you create Windows, add them to the set |
|
602 | # then when you create Windows, add them to the set | |
599 | def make_window(): |
|
603 | def make_window(): | |
600 | win = QtGui.QMainWindow() |
|
604 | win = QtGui.QMainWindow() | |
601 | app.references.add(win) |
|
605 | app.references.add(win) | |
602 |
|
606 | |||
603 | Now the QApplication itself holds a reference to ``win``, so it will never be |
|
607 | Now the QApplication itself holds a reference to ``win``, so it will never be | |
604 | garbage collected until the application itself is destroyed. |
|
608 | garbage collected until the application itself is destroyed. | |
605 |
|
609 | |||
606 | .. _Gotchas: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/gotchas.html#garbage-collection |
|
610 | .. _Gotchas: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/gotchas.html#garbage-collection | |
607 |
|
611 | |||
608 | Embedding the QtConsole in a Qt application |
|
612 | Embedding the QtConsole in a Qt application | |
609 | ******************************************* |
|
613 | ******************************************* | |
610 |
|
614 | |||
611 | In order to make the QtConsole available to an external Qt GUI application (just as |
|
615 | In order to make the QtConsole available to an external Qt GUI application (just as | |
612 | :func:`IPython.embed` enables one to embed a terminal session of IPython in a |
|
616 | :func:`IPython.embed` enables one to embed a terminal session of IPython in a | |
613 | command-line application), there are a few options: |
|
617 | command-line application), there are a few options: | |
614 |
|
618 | |||
615 | * First start IPython, and then start the external Qt application from IPython, |
|
619 | * First start IPython, and then start the external Qt application from IPython, | |
616 | as described above. Effectively, this embeds your application in IPython |
|
620 | as described above. Effectively, this embeds your application in IPython | |
617 | rather than the other way round. |
|
621 | rather than the other way round. | |
618 |
|
622 | |||
619 | * Use :class:`IPython.qt.console.rich_ipython_widget.RichIPythonWidget` in your |
|
623 | * Use :class:`IPython.qt.console.rich_ipython_widget.RichIPythonWidget` in your | |
620 | Qt application. This will embed the console widget in your GUI and start the |
|
624 | Qt application. This will embed the console widget in your GUI and start the | |
621 | kernel in a separate process, so code typed into the console cannot access |
|
625 | kernel in a separate process, so code typed into the console cannot access | |
622 | objects in your application. |
|
626 | objects in your application. | |
623 |
|
627 | |||
624 | * Start a standard IPython kernel in the process of the external Qt |
|
628 | * Start a standard IPython kernel in the process of the external Qt | |
625 | application. See :file:`examples/lib/ipkernel_qtapp.py` for an example. Due |
|
629 | application. See :file:`examples/lib/ipkernel_qtapp.py` for an example. Due | |
626 | to IPython's two-process model, the QtConsole itself will live in another |
|
630 | to IPython's two-process model, the QtConsole itself will live in another | |
627 | process with its own QApplication, and thus cannot be embedded in the main |
|
631 | process with its own QApplication, and thus cannot be embedded in the main | |
628 | GUI. |
|
632 | GUI. | |
629 |
|
633 | |||
630 | * Start a special IPython kernel, the |
|
634 | * Start a special IPython kernel, the | |
631 | :class:`IPython.kernel.inprocess.ipkernel.InProcessKernel`, that allows a |
|
635 | :class:`IPython.kernel.inprocess.ipkernel.InProcessKernel`, that allows a | |
632 | QtConsole in the same process. See :file:`examples/inprocess/embedded_qtconsole.py` |
|
636 | QtConsole in the same process. See :file:`examples/inprocess/embedded_qtconsole.py` | |
633 | for an example. While the QtConsole can now be embedded in the main GUI, one |
|
637 | for an example. While the QtConsole can now be embedded in the main GUI, one | |
634 | cannot connect to the kernel from other consoles as there are no real ZMQ |
|
638 | cannot connect to the kernel from other consoles as there are no real ZMQ | |
635 | sockets anymore. |
|
639 | sockets anymore. | |
636 |
|
640 | |||
637 | Regressions |
|
641 | Regressions | |
638 | =========== |
|
642 | =========== | |
639 |
|
643 | |||
640 | There are some features, where the qt console lags behind the Terminal |
|
644 | There are some features, where the qt console lags behind the Terminal | |
641 | frontend: |
|
645 | frontend: | |
642 |
|
646 | |||
643 | * !cmd input: Due to our use of pexpect, we cannot pass input to subprocesses |
|
647 | * !cmd input: Due to our use of pexpect, we cannot pass input to subprocesses | |
644 | launched using the '!' escape, so you should never call a command that |
|
648 | launched using the '!' escape, so you should never call a command that | |
645 | requires interactive input. For such cases, use the terminal IPython. This |
|
649 | requires interactive input. For such cases, use the terminal IPython. This | |
646 | will not be fixed, as abandoning pexpect would significantly degrade the |
|
650 | will not be fixed, as abandoning pexpect would significantly degrade the | |
647 | console experience. |
|
651 | console experience. | |
648 |
|
652 | |||
649 | .. _PyQt: http://www.riverbankcomputing.co.uk/software/pyqt/download |
|
653 | .. _PyQt: http://www.riverbankcomputing.co.uk/software/pyqt/download | |
650 | .. _pygments: http://pygments.org/ |
|
654 | .. _pygments: http://pygments.org/ |
General Comments 0
You need to be logged in to leave comments.
Login now