Show More
@@ -1,8 +1,9 b'' | |||||
1 | .. _extensions_storemagic: |
|
1 | .. _extensions_storemagic: | |
2 |
|
2 | |||
3 | ========== |
|
3 | ========== | |
4 | storemagic |
|
4 | storemagic | |
5 | ========== |
|
5 | ========== | |
6 |
|
6 | |||
7 | .. automodule:: IPython.extensions.storemagic |
|
7 | .. automodule:: IPython.extensions.storemagic | |
8 | :members: store |
|
8 | ||
|
9 | .. automethod:: StoreMagics.store |
@@ -1,495 +1,495 b'' | |||||
1 | .. _htmlnotebook: |
|
1 | .. _htmlnotebook: | |
2 |
|
2 | |||
3 | The IPython Notebook |
|
3 | The IPython Notebook | |
4 | ==================== |
|
4 | ==================== | |
5 |
|
5 | |||
6 | Introduction |
|
6 | Introduction | |
7 | ------------ |
|
7 | ------------ | |
8 |
|
8 | |||
9 | The notebook extends the console-based approach to interactive computing in |
|
9 | The notebook extends the console-based approach to interactive computing in | |
10 | a qualitatively new direction, providing a web-based application suitable for |
|
10 | a qualitatively new direction, providing a web-based application suitable for | |
11 | capturing the whole computation process: developing, documenting, and |
|
11 | capturing the whole computation process: developing, documenting, and | |
12 | executing code, as well as communicating the results. The IPython notebook |
|
12 | executing code, as well as communicating the results. The IPython notebook | |
13 | combines two components: |
|
13 | combines two components: | |
14 |
|
14 | |||
15 | **A web application**: a browser-based tool for interactive authoring of |
|
15 | **A web application**: a browser-based tool for interactive authoring of | |
16 | documents which combine explanatory text, mathematics, computations and their |
|
16 | documents which combine explanatory text, mathematics, computations and their | |
17 | rich media output. |
|
17 | rich media output. | |
18 |
|
18 | |||
19 | **Notebook documents**: a representation of all content visible in the web |
|
19 | **Notebook documents**: a representation of all content visible in the web | |
20 | application, including inputs and outputs of the computations, explanatory |
|
20 | application, including inputs and outputs of the computations, explanatory | |
21 | text, mathematics, images, and rich media representations of objects. |
|
21 | text, mathematics, images, and rich media representations of objects. | |
22 |
|
22 | |||
23 | .. seealso:: |
|
23 | .. seealso:: | |
24 |
|
24 | |||
25 | See the :ref:`installation documentation <installnotebook>` for directions |
|
25 | See the :ref:`installation documentation <installnotebook>` for directions | |
26 | on how to install the notebook and its dependencies. |
|
26 | on how to install the notebook and its dependencies. | |
27 |
|
27 | |||
28 |
|
28 | |||
29 | Main features of the web application |
|
29 | Main features of the web application | |
30 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
30 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
31 |
|
31 | |||
32 | * In-browser editing for code, with automatic syntax highlighting, |
|
32 | * In-browser editing for code, with automatic syntax highlighting, | |
33 | indentation, and tab completion/introspection. |
|
33 | indentation, and tab completion/introspection. | |
34 |
|
34 | |||
35 | * The ability to execute code from the browser, with the results of |
|
35 | * The ability to execute code from the browser, with the results of | |
36 | computations attached to the code which generated them. |
|
36 | computations attached to the code which generated them. | |
37 |
|
37 | |||
38 | * Displaying the result of computation using rich media representations, such |
|
38 | * Displaying the result of computation using rich media representations, such | |
39 | as HTML, LaTeX, PNG, SVG, etc. For example, publication-quality figures |
|
39 | as HTML, LaTeX, PNG, SVG, etc. For example, publication-quality figures | |
40 | rendered by the matplotlib_ library, can be included inline. |
|
40 | rendered by the matplotlib_ library, can be included inline. | |
41 |
|
41 | |||
42 | * In-browser editing for rich text using the Markdown_ markup language, which |
|
42 | * In-browser editing for rich text using the Markdown_ markup language, which | |
43 | can provide commentary for the code, is not limited to plain text. |
|
43 | can provide commentary for the code, is not limited to plain text. | |
44 |
|
44 | |||
45 | * The ability to easily include mathematical notation within markdown cells |
|
45 | * The ability to easily include mathematical notation within markdown cells | |
46 | using LaTeX, and rendered natively by MathJax_. |
|
46 | using LaTeX, and rendered natively by MathJax_. | |
47 |
|
47 | |||
48 |
|
48 | |||
49 |
|
49 | |||
50 | .. _MathJax: http://www.mathjax.org/ |
|
50 | .. _MathJax: http://www.mathjax.org/ | |
51 |
|
51 | |||
52 |
|
52 | |||
53 | Notebook documents |
|
53 | Notebook documents | |
54 | ~~~~~~~~~~~~~~~~~~ |
|
54 | ~~~~~~~~~~~~~~~~~~ | |
55 | Notebook documents contains the inputs and outputs of a interactive session as |
|
55 | Notebook documents contains the inputs and outputs of a interactive session as | |
56 | well as additional text that accompanies the code but is not meant for |
|
56 | well as additional text that accompanies the code but is not meant for | |
57 | execution. In this way, notebook files can serve as a complete computational |
|
57 | execution. In this way, notebook files can serve as a complete computational | |
58 | record of a session, interleaving executable code with explanatory text, |
|
58 | record of a session, interleaving executable code with explanatory text, | |
59 | mathematics, and rich representations of resulting objects. These documents |
|
59 | mathematics, and rich representations of resulting objects. These documents | |
60 | are internally JSON_ files and are saved with the ``.ipynb`` extension. Since |
|
60 | are internally JSON_ files and are saved with the ``.ipynb`` extension. Since | |
61 | JSON is a plain text format, they can be version-controlled and shared with |
|
61 | JSON is a plain text format, they can be version-controlled and shared with | |
62 | colleagues. |
|
62 | colleagues. | |
63 |
|
63 | |||
64 | .. _JSON: http://en.wikipedia.org/wiki/JSON |
|
64 | .. _JSON: http://en.wikipedia.org/wiki/JSON | |
65 |
|
65 | |||
66 | Notebooks may be exported to a range of static formats, including HTML (for |
|
66 | Notebooks may be exported to a range of static formats, including HTML (for | |
67 | example, for blog posts), reStructeredText, LaTeX, PDF, and slide shows, via |
|
67 | example, for blog posts), reStructeredText, LaTeX, PDF, and slide shows, via | |
68 | the new :ref:`nbconvert <nbconvert>` command. |
|
68 | the new :ref:`nbconvert <nbconvert>` command. | |
69 |
|
69 | |||
70 | Furthermore, any ``.ipynb`` notebook document available from a public |
|
70 | Furthermore, any ``.ipynb`` notebook document available from a public | |
71 | URL can be shared via the `IPython Notebook Viewer <nbviewer>`_ (nbviewer_). |
|
71 | URL can be shared via the `IPython Notebook Viewer <nbviewer>`_ (nbviewer_). | |
72 | This service loads the notebook document from the URL and renders it as a |
|
72 | This service loads the notebook document from the URL and renders it as a | |
73 | static web page. The results may thus be shared with a colleague, or as a |
|
73 | static web page. The results may thus be shared with a colleague, or as a | |
74 | public blog post, without other users needing to install IPython themselves. |
|
74 | public blog post, without other users needing to install IPython themselves. | |
75 | In effect, nbviewer_ is simply :ref:`nbconvert <nbconvert>` as a web service, |
|
75 | In effect, nbviewer_ is simply :ref:`nbconvert <nbconvert>` as a web service, | |
76 | so you can do your own static conversions with nbconvert, without relying on |
|
76 | so you can do your own static conversions with nbconvert, without relying on | |
77 | nbviewer. |
|
77 | nbviewer. | |
78 |
|
78 | |||
79 |
|
79 | |||
80 |
|
80 | |||
81 | .. seealso:: |
|
81 | .. seealso:: | |
82 |
|
82 | |||
83 | :ref:`Details on the notebook JSON file format <notebook_format>` |
|
83 | :ref:`Details on the notebook JSON file format <notebook_format>` | |
84 |
|
84 | |||
85 |
|
85 | |||
86 | Starting the notebook server |
|
86 | Starting the notebook server | |
87 | ---------------------------- |
|
87 | ---------------------------- | |
88 |
|
88 | |||
89 | You can start running a notebook server from the command line using the |
|
89 | You can start running a notebook server from the command line using the | |
90 | following command:: |
|
90 | following command:: | |
91 |
|
91 | |||
92 | ipython notebook |
|
92 | ipython notebook | |
93 |
|
93 | |||
94 | This will print some information about the notebook server in your console, |
|
94 | This will print some information about the notebook server in your console, | |
95 | and open a web browser to the URL of the web application (by default, |
|
95 | and open a web browser to the URL of the web application (by default, | |
96 | ``http://127.0.0.1:8888``). |
|
96 | ``http://127.0.0.1:8888``). | |
97 |
|
97 | |||
98 | The landing page of the IPython notebook web application, the **dashboard**, |
|
98 | The landing page of the IPython notebook web application, the **dashboard**, | |
99 | shows the notebooks currently available in the notebook directory (by default, |
|
99 | shows the notebooks currently available in the notebook directory (by default, | |
100 | the directory from which the notebook server was started). |
|
100 | the directory from which the notebook server was started). | |
101 |
|
101 | |||
102 | You can create new notebooks from the dashboard with the ``New Notebook`` |
|
102 | You can create new notebooks from the dashboard with the ``New Notebook`` | |
103 | button, or open existing ones by clicking on their name. You can also drag |
|
103 | button, or open existing ones by clicking on their name. You can also drag | |
104 | and drop ``.ipynb`` notebooks and standard ``.py`` Python source code files |
|
104 | and drop ``.ipynb`` notebooks and standard ``.py`` Python source code files | |
105 | into the notebook list area. |
|
105 | into the notebook list area. | |
106 |
|
106 | |||
107 | When starting a notebook server from the command line, you can also open a |
|
107 | When starting a notebook server from the command line, you can also open a | |
108 | particular notebook directly, bypassing the dashboard, with ``ipython notebook |
|
108 | particular notebook directly, bypassing the dashboard, with ``ipython notebook | |
109 | my_notebook.ipynb``. The ``.ipynb`` extension is assumed if no extension is |
|
109 | my_notebook.ipynb``. The ``.ipynb`` extension is assumed if no extension is | |
110 | given. |
|
110 | given. | |
111 |
|
111 | |||
112 | When you are inside an open notebook, the `File | Open...` menu option will |
|
112 | When you are inside an open notebook, the `File | Open...` menu option will | |
113 | open the dashboard in a new browser tab, to allow you to open another notebook |
|
113 | open the dashboard in a new browser tab, to allow you to open another notebook | |
114 | from the notebook directory or to create a new notebook. |
|
114 | from the notebook directory or to create a new notebook. | |
115 |
|
115 | |||
116 |
|
116 | |||
117 | .. note:: |
|
117 | .. note:: | |
118 |
|
118 | |||
119 | You can start more than one notebook server at the same time, if you want |
|
119 | You can start more than one notebook server at the same time, if you want | |
120 | to work on notebooks in different directories. By default the first |
|
120 | to work on notebooks in different directories. By default the first | |
121 | notebook server starts on port 8888, and later notebook servers search for |
|
121 | notebook server starts on port 8888, and later notebook servers search for | |
122 | ports near that one. You can also manually specify the port with the |
|
122 | ports near that one. You can also manually specify the port with the | |
123 | ``--port`` option. |
|
123 | ``--port`` option. | |
124 |
|
124 | |||
125 | Creating a new notebook document |
|
125 | Creating a new notebook document | |
126 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
126 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
127 |
|
127 | |||
128 | A new notebook may be created at any time, either from the dashboard, or using |
|
128 | A new notebook may be created at any time, either from the dashboard, or using | |
129 | the `File | New` menu option from within an active notebook. The new notebook |
|
129 | the `File | New` menu option from within an active notebook. The new notebook | |
130 | is created within the same directory and will open in a new browser tab. It |
|
130 | is created within the same directory and will open in a new browser tab. It | |
131 | will also be reflected as a new entry in the notebook list on the dashboard. |
|
131 | will also be reflected as a new entry in the notebook list on the dashboard. | |
132 |
|
132 | |||
133 |
|
133 | |||
134 | Opening notebooks |
|
134 | Opening notebooks | |
135 | ~~~~~~~~~~~~~~~~~ |
|
135 | ~~~~~~~~~~~~~~~~~ | |
136 | An open notebook has **exactly one** interactive session connected to an |
|
136 | An open notebook has **exactly one** interactive session connected to an | |
137 | :ref:`IPython kernel <ipythonzmq>`, which will execute code sent by the user |
|
137 | :ref:`IPython kernel <ipythonzmq>`, which will execute code sent by the user | |
138 | and communicate back results. This kernel remains active if the web browser |
|
138 | and communicate back results. This kernel remains active if the web browser | |
139 | window is closed, and reopening the same notebook from the dashboard will |
|
139 | window is closed, and reopening the same notebook from the dashboard will | |
140 | reconnect the web application to the same kernel. In the dashboard, notebooks |
|
140 | reconnect the web application to the same kernel. In the dashboard, notebooks | |
141 | with an active kernel have a ``Shutdown`` button next to them, whereas |
|
141 | with an active kernel have a ``Shutdown`` button next to them, whereas | |
142 | notebooks without an active kernel have a ``Delete`` button in its place. |
|
142 | notebooks without an active kernel have a ``Delete`` button in its place. | |
143 |
|
143 | |||
144 | Other clients may connect to the same underlying IPython kernel. |
|
144 | Other clients may connect to the same underlying IPython kernel. | |
145 | The notebook server always prints to the terminal the full details of |
|
145 | The notebook server always prints to the terminal the full details of | |
146 | how to connect to each kernel, with messages such as the following:: |
|
146 | how to connect to each kernel, with messages such as the following:: | |
147 |
|
147 | |||
148 | [NotebookApp] Kernel started: 87f7d2c0-13e3-43df-8bb8-1bd37aaf3373 |
|
148 | [NotebookApp] Kernel started: 87f7d2c0-13e3-43df-8bb8-1bd37aaf3373 | |
149 |
|
149 | |||
150 | This long string is the kernel's ID which is sufficient for getting the |
|
150 | This long string is the kernel's ID which is sufficient for getting the | |
151 | information necessary to connect to the kernel. You can also request this |
|
151 | information necessary to connect to the kernel. You can also request this | |
152 | connection data by running the ``%connect_info`` :ref:`magic |
|
152 | connection data by running the ``%connect_info`` :ref:`magic | |
153 | <magics_explained>`. This will print the same ID information as well as the |
|
153 | <magics_explained>`. This will print the same ID information as well as the | |
154 | content of the JSON data structure it contains. |
|
154 | content of the JSON data structure it contains. | |
155 |
|
155 | |||
156 | You can then, for example, manually start a Qt console connected to the *same* |
|
156 | You can then, for example, manually start a Qt console connected to the *same* | |
157 | kernel from the command line, by passing a portion of the ID:: |
|
157 | kernel from the command line, by passing a portion of the ID:: | |
158 |
|
158 | |||
159 | $ ipython qtconsole --existing 87f7d2c0 |
|
159 | $ ipython qtconsole --existing 87f7d2c0 | |
160 |
|
160 | |||
161 | Without an ID, ``--existing`` will connect to the most recently |
|
161 | Without an ID, ``--existing`` will connect to the most recently | |
162 | started kernel. This can also be done by running the ``%qtconsole`` |
|
162 | started kernel. This can also be done by running the ``%qtconsole`` | |
163 | :ref:`magic <magics_explained>` in the notebook. |
|
163 | :ref:`magic <magics_explained>` in the notebook. | |
164 |
|
164 | |||
165 | .. seealso:: |
|
165 | .. seealso:: | |
166 |
|
166 | |||
167 | :ref:`ipythonzmq` |
|
167 | :ref:`ipythonzmq` | |
168 |
|
168 | |||
169 | Notebook user interface |
|
169 | Notebook user interface | |
170 | ----------------------- |
|
170 | ----------------------- | |
171 |
|
171 | |||
172 | When you create a new notebook document, you will be presented with the |
|
172 | When you create a new notebook document, you will be presented with the | |
173 | **notebook name**, a **menu bar**, a **toolbar** and an empty **code |
|
173 | **notebook name**, a **menu bar**, a **toolbar** and an empty **code | |
174 | cell**. |
|
174 | cell**. | |
175 |
|
175 | |||
176 | **notebook name**: The name of the notebook document is displayed at the top |
|
176 | **notebook name**: The name of the notebook document is displayed at the top | |
177 | of the page, next to the ``IP[y]: Notebook`` logo. This name reflects the name |
|
177 | of the page, next to the ``IP[y]: Notebook`` logo. This name reflects the name | |
178 | of the ``.ipynb`` notebook document file. Clicking on the notebook name |
|
178 | of the ``.ipynb`` notebook document file. Clicking on the notebook name | |
179 | brings up a dialog which allows you to rename it. Thus, renaming a notebook |
|
179 | brings up a dialog which allows you to rename it. Thus, renaming a notebook | |
180 | from "Untitled0" to "My first notebook" in the browser, renames the |
|
180 | from "Untitled0" to "My first notebook" in the browser, renames the | |
181 | ``Untitled0.ipynb`` file to ``My first notebook.ipynb``. |
|
181 | ``Untitled0.ipynb`` file to ``My first notebook.ipynb``. | |
182 |
|
182 | |||
183 | **menu bar**: The menu bar presents different options that may be used to |
|
183 | **menu bar**: The menu bar presents different options that may be used to | |
184 | manipulate the way the notebook functions. |
|
184 | manipulate the way the notebook functions. | |
185 |
|
185 | |||
186 | **toolbar**: The tool bar gives a quick way of performing the most-used |
|
186 | **toolbar**: The tool bar gives a quick way of performing the most-used | |
187 | operations within the notebook, by clicking on an icon. |
|
187 | operations within the notebook, by clicking on an icon. | |
188 |
|
188 | |||
189 | **code cell**: the default type of cell, read on for an explanation of cells |
|
189 | **code cell**: the default type of cell, read on for an explanation of cells | |
190 |
|
190 | |||
191 |
|
191 | |||
192 | Structure of a notebook document |
|
192 | Structure of a notebook document | |
193 | -------------------------------- |
|
193 | -------------------------------- | |
194 |
|
194 | |||
195 | The notebook consists of a sequence of cells. A cell is a multi-line |
|
195 | The notebook consists of a sequence of cells. A cell is a multi-line | |
196 | text input field, and its contents can be executed by using |
|
196 | text input field, and its contents can be executed by using | |
197 | :kbd:`Shift-Enter`, or by clicking either the "Play" button the toolbar, or |
|
197 | :kbd:`Shift-Enter`, or by clicking either the "Play" button the toolbar, or | |
198 | `Cell | Run` in the menu bar. The execution behavior of a cell is determined |
|
198 | `Cell | Run` in the menu bar. The execution behavior of a cell is determined | |
199 | the cell's type. There are four types of cells: **code cells**, **markdown |
|
199 | the cell's type. There are four types of cells: **code cells**, **markdown | |
200 | cells**, **raw cells** and **heading cells**. Every cell starts off |
|
200 | cells**, **raw cells** and **heading cells**. Every cell starts off | |
201 | being a **code cell**, but its type can be changed by using a dropdown on the |
|
201 | being a **code cell**, but its type can be changed by using a dropdown on the | |
202 | toolbar (which will be "Code", initially), or via :ref:`keyboard shortcuts |
|
202 | toolbar (which will be "Code", initially), or via :ref:`keyboard shortcuts | |
203 | <keyboard-shortcuts>`. |
|
203 | <keyboard-shortcuts>`. | |
204 |
|
204 | |||
205 | For more information on the different things you can do in a notebook, |
|
205 | For more information on the different things you can do in a notebook, | |
206 | see the `collection of examples |
|
206 | see the `collection of examples | |
207 | <https://github.com/ipython/ipython/tree/master/examples/notebooks#readme>`_. |
|
207 | <https://github.com/ipython/ipython/tree/master/examples/notebooks#readme>`_. | |
208 |
|
208 | |||
209 | Code cells |
|
209 | Code cells | |
210 | ~~~~~~~~~~ |
|
210 | ~~~~~~~~~~ | |
211 | A *code cell* allows you to edit and write new code, with full syntax |
|
211 | A *code cell* allows you to edit and write new code, with full syntax | |
212 | highlighting and tab completion. By default, the language associated to a code |
|
212 | highlighting and tab completion. By default, the language associated to a code | |
213 | cell is Python, but other languages, such as ``Julia`` and ``R``, can be |
|
213 | cell is Python, but other languages, such as ``Julia`` and ``R``, can be | |
214 | handled using :ref:`cell magic commands <magics_explained>`. |
|
214 | handled using :ref:`cell magic commands <magics_explained>`. | |
215 |
|
215 | |||
216 | When a code cell is executed, code that it contains is sent to the kernel |
|
216 | When a code cell is executed, code that it contains is sent to the kernel | |
217 | associated with the notebook. The results that are returned from this |
|
217 | associated with the notebook. The results that are returned from this | |
218 | computation are then displayed in the notebook as the cell's *output*. The |
|
218 | computation are then displayed in the notebook as the cell's *output*. The | |
219 | output is not limited to text, with many other possible forms of output are |
|
219 | output is not limited to text, with many other possible forms of output are | |
220 | also possible, including ``matplotlib`` figures and HTML tables (as used, for |
|
220 | also possible, including ``matplotlib`` figures and HTML tables (as used, for | |
221 | example, in the ``pandas`` data analysis package). This is known as IPython's |
|
221 | example, in the ``pandas`` data analysis package). This is known as IPython's | |
222 | *rich display* capability. |
|
222 | *rich display* capability. | |
223 |
|
223 | |||
224 | .. seealso:: |
|
224 | .. seealso:: | |
225 |
|
225 | |||
226 | `Basic Output`_ example notebook |
|
226 | `Basic Output`_ example notebook | |
227 |
|
227 | |||
228 | `Rich Display System`_ example notebook |
|
228 | `Rich Display System`_ example notebook | |
229 |
|
229 | |||
230 | Markdown cells |
|
230 | Markdown cells | |
231 | ~~~~~~~~~~~~~~ |
|
231 | ~~~~~~~~~~~~~~ | |
232 | You can document the computational process in a literate way, alternating |
|
232 | You can document the computational process in a literate way, alternating | |
233 | descriptive text with code, using *rich text*. In IPython this is accomplished |
|
233 | descriptive text with code, using *rich text*. In IPython this is accomplished | |
234 | by marking up text with the Markdown language. The corresponding cells are |
|
234 | by marking up text with the Markdown language. The corresponding cells are | |
235 | called *Markdown cells*. The Markdown language provides a simple way to |
|
235 | called *Markdown cells*. The Markdown language provides a simple way to | |
236 | perform this text markup, that is, to specify which parts of the text should |
|
236 | perform this text markup, that is, to specify which parts of the text should | |
237 | be emphasized (italics), bold, form lists, etc. |
|
237 | be emphasized (italics), bold, form lists, etc. | |
238 |
|
238 | |||
239 |
|
239 | |||
240 | When a Markdown cell is executed, the Markdown code is converted into |
|
240 | When a Markdown cell is executed, the Markdown code is converted into | |
241 | the corresponding formatted rich text. Markdown allows arbitrary HTML code for |
|
241 | the corresponding formatted rich text. Markdown allows arbitrary HTML code for | |
242 | formatting. |
|
242 | formatting. | |
243 |
|
243 | |||
244 | Within Markdown cells, you can also include *mathematics* in a straightforward |
|
244 | Within Markdown cells, you can also include *mathematics* in a straightforward | |
245 | way, using standard LaTeX notation: ``$...$`` for inline mathematics and |
|
245 | way, using standard LaTeX notation: ``$...$`` for inline mathematics and | |
246 | ``$$...$$`` for displayed mathematics. When the Markdown cell is executed, |
|
246 | ``$$...$$`` for displayed mathematics. When the Markdown cell is executed, | |
247 | the LaTeX portions are automatically rendered in the HTML output as equations |
|
247 | the LaTeX portions are automatically rendered in the HTML output as equations | |
248 | with high quality typography. This is made possible by MathJax_, which |
|
248 | with high quality typography. This is made possible by MathJax_, which | |
249 | supports a `large subset <mathjax_tex>`_ of LaTeX functionality |
|
249 | supports a `large subset <mathjax_tex>`_ of LaTeX functionality | |
250 |
|
250 | |||
251 | .. _mathjax_tex: http://docs.mathjax.org/en/latest/tex.html |
|
251 | .. _mathjax_tex: http://docs.mathjax.org/en/latest/tex.html | |
252 |
|
252 | |||
253 | Standard mathematics environments defined by LaTeX and AMS-LaTeX (the |
|
253 | Standard mathematics environments defined by LaTeX and AMS-LaTeX (the | |
254 | `amsmath` package) also work, such as |
|
254 | `amsmath` package) also work, such as | |
255 | ``\begin{equation}...\end{equation}``, and ``\begin{align}...\end{align}``. |
|
255 | ``\begin{equation}...\end{equation}``, and ``\begin{align}...\end{align}``. | |
256 | New LaTeX macros may be defined using standard methods, |
|
256 | New LaTeX macros may be defined using standard methods, | |
257 | such as ``\newcommand``, by placing them anywhere *between math delimiters* in |
|
257 | such as ``\newcommand``, by placing them anywhere *between math delimiters* in | |
258 | a Markdown cell. These definitions are then available throughout the rest of |
|
258 | a Markdown cell. These definitions are then available throughout the rest of | |
259 | the IPython session. |
|
259 | the IPython session. | |
260 |
|
260 | |||
261 | .. seealso:: |
|
261 | .. seealso:: | |
262 |
|
262 | |||
263 | `Markdown Cells`_ example notebook |
|
263 | `Markdown Cells`_ example notebook | |
264 |
|
264 | |||
265 | Raw cells |
|
265 | Raw cells | |
266 | ~~~~~~~~~ |
|
266 | ~~~~~~~~~ | |
267 |
|
267 | |||
268 | *Raw* cells provide a place in which you can write *output* directly. |
|
268 | *Raw* cells provide a place in which you can write *output* directly. | |
269 | Raw cells are not evaluated by the notebook. |
|
269 | Raw cells are not evaluated by the notebook. | |
270 | When passed through :ref:`nbconvert <nbconvert>`, raw cells arrive in the |
|
270 | When passed through :ref:`nbconvert <nbconvert>`, raw cells arrive in the | |
271 | destination format unmodified. For example, this allows you to type full LaTeX |
|
271 | destination format unmodified. For example, this allows you to type full LaTeX | |
272 | into a raw cell, which will only be rendered by LaTeX after conversion by |
|
272 | into a raw cell, which will only be rendered by LaTeX after conversion by | |
273 | nbconvert. |
|
273 | nbconvert. | |
274 |
|
274 | |||
275 | Heading cells |
|
275 | Heading cells | |
276 | ~~~~~~~~~~~~~ |
|
276 | ~~~~~~~~~~~~~ | |
277 |
|
277 | |||
278 | You can provide a conceptual structure for your computational document as a |
|
278 | You can provide a conceptual structure for your computational document as a | |
279 | whole using different levels of headings; there are 6 levels available, from |
|
279 | whole using different levels of headings; there are 6 levels available, from | |
280 | level 1 (top level) down to level 6 (paragraph). These can be used later for |
|
280 | level 1 (top level) down to level 6 (paragraph). These can be used later for | |
281 | constructing tables of contents, etc. As with Markdown cells, a heading |
|
281 | constructing tables of contents, etc. As with Markdown cells, a heading | |
282 | cell is replaced by a rich text rendering of the heading when the cell is |
|
282 | cell is replaced by a rich text rendering of the heading when the cell is | |
283 | executed. |
|
283 | executed. | |
284 |
|
284 | |||
285 |
|
285 | |||
286 | Basic workflow |
|
286 | Basic workflow | |
287 | -------------- |
|
287 | -------------- | |
288 |
|
288 | |||
289 | The normal workflow in a notebook is, then, quite similar to a standard |
|
289 | The normal workflow in a notebook is, then, quite similar to a standard | |
290 | IPython session, with the difference that you can edit cells in-place multiple |
|
290 | IPython session, with the difference that you can edit cells in-place multiple | |
291 | times until you obtain the desired results, rather than having to |
|
291 | times until you obtain the desired results, rather than having to | |
292 | rerun separate scripts with the ``%run`` magic command. |
|
292 | rerun separate scripts with the ``%run`` magic command. | |
293 |
|
293 | |||
294 |
|
294 | |||
295 | Typically, you will work on a computational problem in pieces, organizing |
|
295 | Typically, you will work on a computational problem in pieces, organizing | |
296 | related ideas into cells and moving forward once previous parts work |
|
296 | related ideas into cells and moving forward once previous parts work | |
297 | correctly. This is much more convenient for interactive exploration than |
|
297 | correctly. This is much more convenient for interactive exploration than | |
298 | breaking up a computation into scripts that must be executed together, as was |
|
298 | breaking up a computation into scripts that must be executed together, as was | |
299 | previously necessary, especially if parts of them take a long time to run. |
|
299 | previously necessary, especially if parts of them take a long time to run. | |
300 |
|
300 | |||
301 | At certain moments, it may be necessary to interrupt a calculation which is |
|
301 | At certain moments, it may be necessary to interrupt a calculation which is | |
302 | taking too long to complete. This may be done with the `Kernel | Interrupt` |
|
302 | taking too long to complete. This may be done with the `Kernel | Interrupt` | |
303 | menu option, or the :kbd:`Ctrl-m i` keyboard shortcut. |
|
303 | menu option, or the :kbd:`Ctrl-m i` keyboard shortcut. | |
304 | Similarly, it may be necessary or desirable to restart the whole computational |
|
304 | Similarly, it may be necessary or desirable to restart the whole computational | |
305 | process, with the `Kernel | Restart` menu option or :kbd:`Ctrl-m .` |
|
305 | process, with the `Kernel | Restart` menu option or :kbd:`Ctrl-m .` | |
306 | shortcut. |
|
306 | shortcut. | |
307 |
|
307 | |||
308 | A notebook may be downloaded in either a ``.ipynb`` or ``.py`` file from the |
|
308 | A notebook may be downloaded in either a ``.ipynb`` or ``.py`` file from the | |
309 | menu option `File | Download as`. Choosing the ``.py`` option downloads a |
|
309 | menu option `File | Download as`. Choosing the ``.py`` option downloads a | |
310 | Python ``.py`` script, in which all rich output has been removed and the |
|
310 | Python ``.py`` script, in which all rich output has been removed and the | |
311 | content of markdown cells have been inserted as comments. |
|
311 | content of markdown cells have been inserted as comments. | |
312 |
|
312 | |||
313 | .. seealso:: |
|
313 | .. seealso:: | |
314 |
|
314 | |||
315 | `Running Code in the IPython Notebook`_ example notebook |
|
315 | `Running Code in the IPython Notebook`_ example notebook | |
316 |
|
316 | |||
317 | `Basic Output`_ example notebook |
|
317 | `Basic Output`_ example notebook | |
318 |
|
318 | |||
319 | :ref:`a warning about doing "roundtrip" conversions <note_about_roundtrip>`. |
|
319 | :ref:`a warning about doing "roundtrip" conversions <note_about_roundtrip>`. | |
320 |
|
320 | |||
321 | .. _keyboard-shortcuts: |
|
321 | .. _keyboard-shortcuts: | |
322 |
|
322 | |||
323 | Keyboard shortcuts |
|
323 | Keyboard shortcuts | |
324 | ~~~~~~~~~~~~~~~~~~ |
|
324 | ~~~~~~~~~~~~~~~~~~ | |
325 | All actions in the notebook can be performed with the mouse, but keyboard |
|
325 | All actions in the notebook can be performed with the mouse, but keyboard | |
326 | shortcuts are also available for the most common ones. The essential shortcuts |
|
326 | shortcuts are also available for the most common ones. The essential shortcuts | |
327 | to remember are the following: |
|
327 | to remember are the following: | |
328 |
|
328 | |||
329 | * :kbd:`Shift-Enter`: run cell |
|
329 | * :kbd:`Shift-Enter`: run cell | |
330 | Execute the current cell, show output (if any), and jump to the next cell |
|
330 | Execute the current cell, show output (if any), and jump to the next cell | |
331 | below. If :kbd:`Shift-Enter` is invoked on the last cell, a new code |
|
331 | below. If :kbd:`Shift-Enter` is invoked on the last cell, a new code | |
332 | cell will also be created. Note that in the notebook, typing :kbd:`Enter` |
|
332 | cell will also be created. Note that in the notebook, typing :kbd:`Enter` | |
333 | on its own *never* forces execution, but rather just inserts a new line in |
|
333 | on its own *never* forces execution, but rather just inserts a new line in | |
334 | the current cell. :kbd:`Shift-Enter` is equivalent to clicking the |
|
334 | the current cell. :kbd:`Shift-Enter` is equivalent to clicking the | |
335 | ``Cell | Run`` menu item. |
|
335 | ``Cell | Run`` menu item. | |
336 |
|
336 | |||
337 | * :kbd:`Ctrl-Enter`: run cell in-place |
|
337 | * :kbd:`Ctrl-Enter`: run cell in-place | |
338 | Execute the current cell as if it were in "terminal mode", where any |
|
338 | Execute the current cell as if it were in "terminal mode", where any | |
339 | output is shown, but the cursor *remains* in the current cell. The cell's |
|
339 | output is shown, but the cursor *remains* in the current cell. The cell's | |
340 | entire contents are selected after execution, so you can just start typing |
|
340 | entire contents are selected after execution, so you can just start typing | |
341 | and only the new input will be in the cell. This is convenient for doing |
|
341 | and only the new input will be in the cell. This is convenient for doing | |
342 | quick experiments in place, or for querying things like filesystem |
|
342 | quick experiments in place, or for querying things like filesystem | |
343 | content, without needing to create additional cells that you may not want |
|
343 | content, without needing to create additional cells that you may not want | |
344 | to be saved in the notebook. |
|
344 | to be saved in the notebook. | |
345 |
|
345 | |||
346 | * :kbd:`Alt-Enter`: run cell, insert below |
|
346 | * :kbd:`Alt-Enter`: run cell, insert below | |
347 | Executes the current cell, shows the output, and inserts a *new* |
|
347 | Executes the current cell, shows the output, and inserts a *new* | |
348 | cell between the current cell and the cell below (if one exists). This |
|
348 | cell between the current cell and the cell below (if one exists). This | |
349 | is thus a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`. |
|
349 | is thus a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`. | |
350 | (:kbd:`Ctrl-m a` adds a new cell above the current one.) |
|
350 | (:kbd:`Ctrl-m a` adds a new cell above the current one.) | |
351 |
|
351 | |||
352 | * :kbd:`Ctrl-m`: |
|
352 | * :kbd:`Ctrl-m`: | |
353 | This is the prefix for *all* other shortcuts, which consist of :kbd:`Ctrl-m` |
|
353 | This is the prefix for *all* other shortcuts, which consist of :kbd:`Ctrl-m` | |
354 | followed by a single letter or character. For example, if you type |
|
354 | followed by a single letter or character. For example, if you type | |
355 | :kbd:`Ctrl-m h` (that is, the sole letter :kbd:`h` after :kbd:`Ctrl-m`), |
|
355 | :kbd:`Ctrl-m h` (that is, the sole letter :kbd:`h` after :kbd:`Ctrl-m`), | |
356 | IPython will show you all the available keyboard shortcuts. |
|
356 | IPython will show you all the available keyboard shortcuts. | |
357 |
|
357 | |||
358 |
|
358 | |||
359 | .. |
|
359 | .. | |
360 | TODO: these live in IPython/html/static/notebook/js/quickhelp.js |
|
360 | TODO: these live in IPython/html/static/notebook/js/quickhelp.js | |
361 | They were last updated for IPython 1.0 release, so update them again for |
|
361 | They were last updated for IPython 1.0 release, so update them again for | |
362 | future releases. |
|
362 | future releases. | |
363 |
|
363 | |||
364 | Here is the complete set of keyboard shortcuts available: |
|
364 | Here is the complete set of keyboard shortcuts available: | |
365 |
|
365 | |||
366 | ============ ========================== |
|
366 | ============ ========================== | |
367 | **Shortcut** **Action** |
|
367 | **Shortcut** **Action** | |
368 | ------------ -------------------------- |
|
368 | ------------ -------------------------- | |
369 | Shift-Enter run cell |
|
369 | Shift-Enter run cell | |
370 | Ctrl-Enter run cell in-place |
|
370 | Ctrl-Enter run cell in-place | |
371 | Alt-Enter run cell, insert below |
|
371 | Alt-Enter run cell, insert below | |
372 | Ctrl-m x cut cell |
|
372 | Ctrl-m x cut cell | |
373 | Ctrl-m c copy cell |
|
373 | Ctrl-m c copy cell | |
374 | Ctrl-m v paste cell |
|
374 | Ctrl-m v paste cell | |
375 | Ctrl-m d delete cell |
|
375 | Ctrl-m d delete cell | |
376 | Ctrl-m z undo last cell deletion |
|
376 | Ctrl-m z undo last cell deletion | |
377 | Ctrl-m - split cell |
|
377 | Ctrl-m - split cell | |
378 | Ctrl-m a insert cell above |
|
378 | Ctrl-m a insert cell above | |
379 | Ctrl-m b insert cell below |
|
379 | Ctrl-m b insert cell below | |
380 | Ctrl-m o toggle output |
|
380 | Ctrl-m o toggle output | |
381 | Ctrl-m O toggle output scroll |
|
381 | Ctrl-m O toggle output scroll | |
382 | Ctrl-m l toggle line numbers |
|
382 | Ctrl-m l toggle line numbers | |
383 | Ctrl-m s save notebook |
|
383 | Ctrl-m s save notebook | |
384 | Ctrl-m j move cell down |
|
384 | Ctrl-m j move cell down | |
385 | Ctrl-m k move cell up |
|
385 | Ctrl-m k move cell up | |
386 | Ctrl-m y code cell |
|
386 | Ctrl-m y code cell | |
387 | Ctrl-m m markdown cell |
|
387 | Ctrl-m m markdown cell | |
388 | Ctrl-m t raw cell |
|
388 | Ctrl-m t raw cell | |
389 | Ctrl-m 1-6 heading 1-6 cell |
|
389 | Ctrl-m 1-6 heading 1-6 cell | |
390 | Ctrl-m p select previous |
|
390 | Ctrl-m p select previous | |
391 | Ctrl-m n select next |
|
391 | Ctrl-m n select next | |
392 | Ctrl-m i interrupt kernel |
|
392 | Ctrl-m i interrupt kernel | |
393 | Ctrl-m . restart kernel |
|
393 | Ctrl-m . restart kernel | |
394 | Ctrl-m h show keyboard shortcuts |
|
394 | Ctrl-m h show keyboard shortcuts | |
395 | ============ ========================== |
|
395 | ============ ========================== | |
396 |
|
396 | |||
397 |
|
397 | |||
398 |
|
398 | |||
399 |
|
399 | |||
400 |
|
400 | |||
401 | Plotting |
|
401 | Plotting | |
402 | -------- |
|
402 | -------- | |
403 | One major feature of the notebook is the ability to display plots that are the |
|
403 | One major feature of the notebook is the ability to display plots that are the | |
404 | output of running code cells. IPython is designed to work seamlessly with the |
|
404 | output of running code cells. IPython is designed to work seamlessly with the | |
405 | matplotlib_ plotting library to provide this functionality. |
|
405 | matplotlib_ plotting library to provide this functionality. | |
406 |
|
406 | |||
407 | To set this up, before any plotting is performed you must execute the |
|
407 | To set this up, before any plotting is performed you must execute the | |
408 | ``%matplotlib`` :ref:`magic command <magics_explained>`. This performs the |
|
408 | ``%matplotlib`` :ref:`magic command <magics_explained>`. This performs the | |
409 | necessary behind-the-scenes setup for IPython to work correctly hand in hand |
|
409 | necessary behind-the-scenes setup for IPython to work correctly hand in hand | |
410 | with ``matplotlib``; it does *not*, however, actually execute any Python |
|
410 | with ``matplotlib``; it does *not*, however, actually execute any Python | |
411 | ``import`` commands, that is, no names are added to the namespace. |
|
411 | ``import`` commands, that is, no names are added to the namespace. | |
412 |
|
412 | |||
413 | If the ``%matplotlib`` magic is called without an argument, the |
|
413 | If the ``%matplotlib`` magic is called without an argument, the | |
414 | output of a plotting command is displayed using the default ``matplotlib`` |
|
414 | output of a plotting command is displayed using the default ``matplotlib`` | |
415 | backend in a separate window. Alternatively, the backend can be explicitly |
|
415 | backend in a separate window. Alternatively, the backend can be explicitly | |
416 | requested using, for example:: |
|
416 | requested using, for example:: | |
417 |
|
417 | |||
418 | %matplotlib gtk |
|
418 | %matplotlib gtk | |
419 |
|
419 | |||
420 | A particularly interesting backend, provided by IPython, is the ``inline`` |
|
420 | A particularly interesting backend, provided by IPython, is the ``inline`` | |
421 | backend. This is available only for the IPython Notebook and the |
|
421 | backend. This is available only for the IPython Notebook and the | |
422 | :ref:`IPython QtConsole <qtconsole>`. It can be invoked as follows:: |
|
422 | :ref:`IPython QtConsole <qtconsole>`. It can be invoked as follows:: | |
423 |
|
423 | |||
424 | %matplotlib inline |
|
424 | %matplotlib inline | |
425 |
|
425 | |||
426 | With this backend, the output of plotting commands is displayed *inline* |
|
426 | With this backend, the output of plotting commands is displayed *inline* | |
427 | within the notebook, directly below the code cell that produced it. The |
|
427 | within the notebook, directly below the code cell that produced it. The | |
428 | resulting plots will then also be stored in the notebook document. |
|
428 | resulting plots will then also be stored in the notebook document. | |
429 |
|
429 | |||
430 | .. seealso:: |
|
430 | .. seealso:: | |
431 |
|
431 | |||
432 | `Plotting with Matplotlib`_ example notebook |
|
432 | `Plotting with Matplotlib`_ example notebook | |
433 |
|
433 | |||
434 |
|
434 | |||
435 | Configuring the IPython Notebook |
|
435 | Configuring the IPython Notebook | |
436 | -------------------------------- |
|
436 | -------------------------------- | |
437 | The notebook server can be run with a variety of command line arguments. |
|
437 | The notebook server can be run with a variety of command line arguments. | |
438 | To see a list of available options enter:: |
|
438 | To see a list of available options enter:: | |
439 |
|
439 | |||
440 | $ ipython notebook --help |
|
440 | $ ipython notebook --help | |
441 |
|
441 | |||
442 | Defaults for these options can also be set by creating a file named |
|
442 | Defaults for these options can also be set by creating a file named | |
443 | ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile |
|
443 | ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile | |
444 | folder is a subfolder of your IPython directory; to find out where it is |
|
444 | folder is a subfolder of your IPython directory; to find out where it is | |
445 | located, run:: |
|
445 | located, run:: | |
446 |
|
446 | |||
447 | $ ipython locate |
|
447 | $ ipython locate | |
448 |
|
448 | |||
449 | To create a new set of default configuration files, with lots of information |
|
449 | To create a new set of default configuration files, with lots of information | |
450 | on available options, use:: |
|
450 | on available options, use:: | |
451 |
|
451 | |||
452 | $ ipython profile create |
|
452 | $ ipython profile create | |
453 |
|
453 | |||
454 | .. seealso:: |
|
454 | .. seealso:: | |
455 |
|
455 | |||
456 | :ref:`config_overview`, in particular :ref:`Profiles`. |
|
456 | :ref:`config_overview`, in particular :ref:`Profiles`. | |
457 |
|
457 | |||
458 | :ref:`notebook_security` |
|
458 | :ref:`notebook_security` | |
459 |
|
459 | |||
460 | :ref:`notebook_public_server` |
|
460 | :ref:`notebook_public_server` | |
461 |
|
461 | |||
462 |
|
462 | |||
463 | Importing ``.py`` files |
|
463 | Importing ``.py`` files | |
464 | ----------------------- |
|
464 | ----------------------- | |
465 |
|
465 | |||
466 | ``.py`` files will be imported as a notebook with |
|
466 | ``.py`` files will be imported as a notebook with | |
467 | the same basename, but an ``.ipynb`` extension, located in the notebook |
|
467 | the same basename, but an ``.ipynb`` extension, located in the notebook | |
468 | directory. The notebook created will have just one cell, which will contain |
|
468 | directory. The notebook created will have just one cell, which will contain | |
469 | all the code in the ``.py`` file. You can later manually partition this into |
|
469 | all the code in the ``.py`` file. You can later manually partition this into | |
470 | individual cells using the ``Edit | Split Cell`` menu option, or the |
|
470 | individual cells using the ``Edit | Split Cell`` menu option, or the | |
471 | :kbd:`Ctrl-m -` keyboard shortcut. |
|
471 | :kbd:`Ctrl-m -` keyboard shortcut. | |
472 |
|
472 | |||
473 |
Note that ``.py`` scripts obtained from a notebook document using nbconvert |
|
473 | Note that ``.py`` scripts obtained from a notebook document using :doc:`nbconvert <nbconvert>` | |
474 | maintain the structure of the notebook in comments. Reimporting such a |
|
474 | maintain the structure of the notebook in comments. Reimporting such a | |
475 | script back into a notebook will preserve this structure. |
|
475 | script back into a notebook will preserve this structure. | |
476 |
|
476 | |||
477 | .. _note_about_roundtrip: |
|
477 | .. _note_about_roundtrip: | |
478 |
|
478 | |||
479 | .. warning:: |
|
479 | .. warning:: | |
480 |
|
480 | |||
481 | While in simple cases you can "roundtrip" a notebook to Python, edit the |
|
481 | While in simple cases you can "roundtrip" a notebook to Python, edit the | |
482 | Python file, and then import it back without loss of main content, this is |
|
482 | Python file, and then import it back without loss of main content, this is | |
483 | in general *not guaranteed to work*. First, there is extra metadata |
|
483 | in general *not guaranteed to work*. First, there is extra metadata | |
484 | saved in the notebook that may not be saved to the ``.py`` format. And as |
|
484 | saved in the notebook that may not be saved to the ``.py`` format. And as | |
485 | the notebook format evolves in complexity, there will be attributes of the |
|
485 | the notebook format evolves in complexity, there will be attributes of the | |
486 | notebook that will not survive a roundtrip through the Python form. You |
|
486 | notebook that will not survive a roundtrip through the Python form. You | |
487 | should think of the Python format as a way to output a script version of a |
|
487 | should think of the Python format as a way to output a script version of a | |
488 | notebook and the import capabilities as a way to load existing code to get |
|
488 | notebook and the import capabilities as a way to load existing code to get | |
489 | a notebook started. But the Python version is *not* an alternate notebook |
|
489 | a notebook started. But the Python version is *not* an alternate notebook | |
490 | format. |
|
490 | format. | |
491 |
|
491 | |||
492 | .. seealso:: |
|
492 | .. seealso:: | |
493 | :ref:`notebook_format` |
|
493 | :ref:`notebook_format` | |
494 |
|
494 | |||
495 | .. include:: ../links.txt |
|
495 | .. include:: ../links.txt |
@@ -1,163 +1,163 b'' | |||||
1 |
.. _working_remotely |
|
1 | .. _working_remotely: | |
2 |
|
2 | |||
3 | Running a notebook server |
|
3 | Running a notebook server | |
4 | ========================= |
|
4 | ========================= | |
5 |
|
5 | |||
6 |
|
6 | |||
7 | The :ref:`IPython notebook <htmlnotebook>` web-application is based on a |
|
7 | The :ref:`IPython notebook <htmlnotebook>` web-application is based on a | |
8 | server-client structure. This server uses a :ref:`two-process kernel |
|
8 | server-client structure. This server uses a :ref:`two-process kernel | |
9 | architecture <ipythonzmq>` based on ZeroMQ_, as well as Tornado_ for serving |
|
9 | architecture <ipythonzmq>` based on ZeroMQ_, as well as Tornado_ for serving | |
10 | HTTP requests. By default, a notebook server runs on http://127.0.0.1:8888/ |
|
10 | HTTP requests. By default, a notebook server runs on http://127.0.0.1:8888/ | |
11 | and is accessible only from `localhost`. This document describes how you can |
|
11 | and is accessible only from `localhost`. This document describes how you can | |
12 | :ref:`secure a notebook server <notebook_security>` and how to :ref:`run it on |
|
12 | :ref:`secure a notebook server <notebook_security>` and how to :ref:`run it on | |
13 | a public interface <notebook_public_server>`. |
|
13 | a public interface <notebook_public_server>`. | |
14 |
|
14 | |||
15 | .. _ZeroMQ: http://zeromq.org |
|
15 | .. _ZeroMQ: http://zeromq.org | |
16 |
|
16 | |||
17 | .. _Tornado: http://www.tornadoweb.org |
|
17 | .. _Tornado: http://www.tornadoweb.org | |
18 |
|
18 | |||
19 |
|
19 | |||
20 | .. _notebook_security: |
|
20 | .. _notebook_security: | |
21 |
|
21 | |||
22 | Notebook security |
|
22 | Notebook security | |
23 | ----------------- |
|
23 | ----------------- | |
24 |
|
24 | |||
25 | You can protect your notebook server with a simple single password by |
|
25 | You can protect your notebook server with a simple single password by | |
26 | setting the :attr:`NotebookApp.password` configurable. You can prepare a |
|
26 | setting the :attr:`NotebookApp.password` configurable. You can prepare a | |
27 | hashed password using the function :func:`IPython.lib.security.passwd`: |
|
27 | hashed password using the function :func:`IPython.lib.security.passwd`: | |
28 |
|
28 | |||
29 | .. sourcecode:: ipython |
|
29 | .. sourcecode:: ipython | |
30 |
|
30 | |||
31 | In [1]: from IPython.lib import passwd |
|
31 | In [1]: from IPython.lib import passwd | |
32 | In [2]: passwd() |
|
32 | In [2]: passwd() | |
33 | Enter password: |
|
33 | Enter password: | |
34 | Verify password: |
|
34 | Verify password: | |
35 | Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed' |
|
35 | Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed' | |
36 |
|
36 | |||
37 | .. note:: |
|
37 | .. note:: | |
38 |
|
38 | |||
39 | :func:`~IPython.lib.security.passwd` can also take the password as a string |
|
39 | :func:`~IPython.lib.security.passwd` can also take the password as a string | |
40 | argument. **Do not** pass it as an argument inside an IPython session, as it |
|
40 | argument. **Do not** pass it as an argument inside an IPython session, as it | |
41 | will be saved in your input history. |
|
41 | will be saved in your input history. | |
42 |
|
42 | |||
43 | You can then add this to your :file:`ipython_notebook_config.py`, e.g.:: |
|
43 | You can then add this to your :file:`ipython_notebook_config.py`, e.g.:: | |
44 |
|
44 | |||
45 | # Password to use for web authentication |
|
45 | # Password to use for web authentication | |
46 | c = get_config() |
|
46 | c = get_config() | |
47 | c.NotebookApp.password = |
|
47 | c.NotebookApp.password = | |
48 | u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed' |
|
48 | u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed' | |
49 |
|
49 | |||
50 | When using a password, it is a good idea to also use SSL, so that your |
|
50 | When using a password, it is a good idea to also use SSL, so that your | |
51 | password is not sent unencrypted by your browser. You can start the notebook |
|
51 | password is not sent unencrypted by your browser. You can start the notebook | |
52 | to communicate via a secure protocol mode using a self-signed certificate with |
|
52 | to communicate via a secure protocol mode using a self-signed certificate with | |
53 | the command:: |
|
53 | the command:: | |
54 |
|
54 | |||
55 | $ ipython notebook --certfile=mycert.pem |
|
55 | $ ipython notebook --certfile=mycert.pem | |
56 |
|
56 | |||
57 | .. note:: |
|
57 | .. note:: | |
58 |
|
58 | |||
59 | A self-signed certificate can be generated with ``openssl``. For example, |
|
59 | A self-signed certificate can be generated with ``openssl``. For example, | |
60 | the following command will create a certificate valid for 365 days with |
|
60 | the following command will create a certificate valid for 365 days with | |
61 | both the key and certificate data written to the same file:: |
|
61 | both the key and certificate data written to the same file:: | |
62 |
|
62 | |||
63 | $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem |
|
63 | $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem | |
64 |
|
64 | |||
65 | Your browser will warn you of a dangerous certificate because it is |
|
65 | Your browser will warn you of a dangerous certificate because it is | |
66 | self-signed. If you want to have a fully compliant certificate that will not |
|
66 | self-signed. If you want to have a fully compliant certificate that will not | |
67 | raise warnings, it is possible (but rather involved) to obtain one, |
|
67 | raise warnings, it is possible (but rather involved) to obtain one, | |
68 | as explained in detail in `this tutorial`__. |
|
68 | as explained in detail in `this tutorial`__. | |
69 |
|
69 | |||
70 | .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars |
|
70 | .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars | |
71 |
|
71 | |||
72 | Keep in mind that when you enable SSL support, you will need to access the |
|
72 | Keep in mind that when you enable SSL support, you will need to access the | |
73 | notebook server over ``https://``, not over plain ``http://``. The startup |
|
73 | notebook server over ``https://``, not over plain ``http://``. The startup | |
74 | message from the server prints this, but it is easy to overlook and think the |
|
74 | message from the server prints this, but it is easy to overlook and think the | |
75 | server is for some reason non-responsive. |
|
75 | server is for some reason non-responsive. | |
76 |
|
76 | |||
77 |
|
77 | |||
78 | .. _notebook_public_server: |
|
78 | .. _notebook_public_server: | |
79 |
|
79 | |||
80 | Running a public notebook server |
|
80 | Running a public notebook server | |
81 | -------------------------------- |
|
81 | -------------------------------- | |
82 |
|
82 | |||
83 | If you want to access your notebook server remotely via a web browser, |
|
83 | If you want to access your notebook server remotely via a web browser, | |
84 | you can do the following. |
|
84 | you can do the following. | |
85 |
|
85 | |||
86 | Start by creating a certificate file and a hashed password, as explained |
|
86 | Start by creating a certificate file and a hashed password, as explained | |
87 | above. Then create a custom profile for the notebook, with the following |
|
87 | above. Then create a custom profile for the notebook, with the following | |
88 | command line, type:: |
|
88 | command line, type:: | |
89 |
|
89 | |||
90 | $ ipython profile create nbserver |
|
90 | $ ipython profile create nbserver | |
91 |
|
91 | |||
92 | In the profile directory just created, edit the file |
|
92 | In the profile directory just created, edit the file | |
93 | ``ipython_notebook_config.py``. By default, the file has all fields |
|
93 | ``ipython_notebook_config.py``. By default, the file has all fields | |
94 | commented; the minimum set you need to uncomment and edit is the following:: |
|
94 | commented; the minimum set you need to uncomment and edit is the following:: | |
95 |
|
95 | |||
96 | c = get_config() |
|
96 | c = get_config() | |
97 |
|
97 | |||
98 | # Kernel config |
|
98 | # Kernel config | |
99 | c.IPKernelApp.pylab = 'inline' # if you want plotting support always |
|
99 | c.IPKernelApp.pylab = 'inline' # if you want plotting support always | |
100 |
|
100 | |||
101 | # Notebook config |
|
101 | # Notebook config | |
102 | c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem' |
|
102 | c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem' | |
103 | c.NotebookApp.ip = '*' |
|
103 | c.NotebookApp.ip = '*' | |
104 | c.NotebookApp.open_browser = False |
|
104 | c.NotebookApp.open_browser = False | |
105 | c.NotebookApp.password = u'sha1:bcd259ccf...[your hashed password here]' |
|
105 | c.NotebookApp.password = u'sha1:bcd259ccf...[your hashed password here]' | |
106 | # It is a good idea to put it on a known, fixed port |
|
106 | # It is a good idea to put it on a known, fixed port | |
107 | c.NotebookApp.port = 9999 |
|
107 | c.NotebookApp.port = 9999 | |
108 |
|
108 | |||
109 | You can then start the notebook and access it later by pointing your browser |
|
109 | You can then start the notebook and access it later by pointing your browser | |
110 | to ``https://your.host.com:9999`` with ``ipython notebook |
|
110 | to ``https://your.host.com:9999`` with ``ipython notebook | |
111 | --profile=nbserver``. |
|
111 | --profile=nbserver``. | |
112 |
|
112 | |||
113 | Running with a different URL prefix |
|
113 | Running with a different URL prefix | |
114 | ----------------------------------- |
|
114 | ----------------------------------- | |
115 |
|
115 | |||
116 | The notebook dashboard (the landing page with an overview |
|
116 | The notebook dashboard (the landing page with an overview | |
117 | of the notebooks in your working directory) typically lives at the URL |
|
117 | of the notebooks in your working directory) typically lives at the URL | |
118 | ``http://localhost:8888/``. If you prefer that it lives, together with the |
|
118 | ``http://localhost:8888/``. If you prefer that it lives, together with the | |
119 | rest of the notebook, under a sub-directory, |
|
119 | rest of the notebook, under a sub-directory, | |
120 | e.g. ``http://localhost:8888/ipython/``, you can do so with |
|
120 | e.g. ``http://localhost:8888/ipython/``, you can do so with | |
121 | configuration options like the following (see above for instructions about |
|
121 | configuration options like the following (see above for instructions about | |
122 | modifying ``ipython_notebook_config.py``):: |
|
122 | modifying ``ipython_notebook_config.py``):: | |
123 |
|
123 | |||
124 | c.NotebookApp.base_project_url = '/ipython/' |
|
124 | c.NotebookApp.base_project_url = '/ipython/' | |
125 | c.NotebookApp.base_kernel_url = '/ipython/' |
|
125 | c.NotebookApp.base_kernel_url = '/ipython/' | |
126 | c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'} |
|
126 | c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'} | |
127 |
|
127 | |||
128 | Using a different notebook store |
|
128 | Using a different notebook store | |
129 | -------------------------------- |
|
129 | -------------------------------- | |
130 |
|
130 | |||
131 | By default, the notebook server stores the notebook documents that it saves as |
|
131 | By default, the notebook server stores the notebook documents that it saves as | |
132 | files in the working directory of the notebook server, also known as the |
|
132 | files in the working directory of the notebook server, also known as the | |
133 | ``notebook_dir``. This logic is implemented in the |
|
133 | ``notebook_dir``. This logic is implemented in the | |
134 | :class:`FileNotebookManager` class. However, the server can be configured to |
|
134 | :class:`FileNotebookManager` class. However, the server can be configured to | |
135 | use a different notebook manager class, which can |
|
135 | use a different notebook manager class, which can | |
136 | store the notebooks in a different format. |
|
136 | store the notebooks in a different format. | |
137 |
|
137 | |||
138 | Currently, we ship a :class:`AzureNotebookManager` class that stores notebooks |
|
138 | Currently, we ship a :class:`AzureNotebookManager` class that stores notebooks | |
139 | in Azure blob storage. This can be used by adding the following lines to your |
|
139 | in Azure blob storage. This can be used by adding the following lines to your | |
140 | ``ipython_notebook_config.py`` file:: |
|
140 | ``ipython_notebook_config.py`` file:: | |
141 |
|
141 | |||
142 | c.NotebookApp.notebook_manager_class = |
|
142 | c.NotebookApp.notebook_manager_class = | |
143 | 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager' |
|
143 | 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager' | |
144 | c.AzureNotebookManager.account_name = u'paste_your_account_name_here' |
|
144 | c.AzureNotebookManager.account_name = u'paste_your_account_name_here' | |
145 | c.AzureNotebookManager.account_key = u'paste_your_account_key_here' |
|
145 | c.AzureNotebookManager.account_key = u'paste_your_account_key_here' | |
146 | c.AzureNotebookManager.container = u'notebooks' |
|
146 | c.AzureNotebookManager.container = u'notebooks' | |
147 |
|
147 | |||
148 | In addition to providing your Azure Blob Storage account name and key, you |
|
148 | In addition to providing your Azure Blob Storage account name and key, you | |
149 | will have to provide a container name; you can use multiple containers to |
|
149 | will have to provide a container name; you can use multiple containers to | |
150 | organize your notebooks. |
|
150 | organize your notebooks. | |
151 |
|
151 | |||
152 |
|
152 | |||
153 | Known issues |
|
153 | Known issues | |
154 | ------------ |
|
154 | ------------ | |
155 |
|
155 | |||
156 | When behind a proxy, especially if your system or browser is set to autodetect |
|
156 | When behind a proxy, especially if your system or browser is set to autodetect | |
157 | the proxy, the notebook web application might fail to connect to the server's |
|
157 | the proxy, the notebook web application might fail to connect to the server's | |
158 | websockets, and present you with a warning at startup. In this case, you need |
|
158 | websockets, and present you with a warning at startup. In this case, you need | |
159 | to configure your system not to use the proxy for the server's address. |
|
159 | to configure your system not to use the proxy for the server's address. | |
160 |
|
160 | |||
161 | For example, in Firefox, go to the Preferences panel, Advanced section, |
|
161 | For example, in Firefox, go to the Preferences panel, Advanced section, | |
162 | Network tab, click 'Settings...', and add the address of the notebook server |
|
162 | Network tab, click 'Settings...', and add the address of the notebook server | |
163 | to the 'No proxy for' field. |
|
163 | to the 'No proxy for' field. |
@@ -1,610 +1,610 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 | We now have a version of IPython, using the new two-process :ref:`ZeroMQ Kernel |
|
7 | 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 |
|
8 | <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 |
|
9 | 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 |
|
10 | possible in a GUI, such as inline figures, proper multiline editing with syntax | |
11 | highlighting, graphical calltips, and much more. |
|
11 | highlighting, graphical calltips, and much more. | |
12 |
|
12 | |||
13 |
.. figure:: |
|
13 | .. figure:: ../_images/qtconsole.png | |
14 | :width: 400px |
|
14 | :width: 400px | |
15 | :alt: IPython Qt console with embedded plots |
|
15 | :alt: IPython Qt console with embedded plots | |
16 | :align: center |
|
16 | :align: center | |
17 | :target: ../_images/qtconsole.png |
|
17 | :target: ../_images/qtconsole.png | |
18 |
|
18 | |||
19 | The Qt console for IPython, using inline matplotlib plots. |
|
19 | The Qt console for IPython, using inline matplotlib plots. | |
20 |
|
20 | |||
21 | To get acquainted with the Qt console, type `%guiref` to see a quick |
|
21 | To get acquainted with the Qt console, type `%guiref` to see a quick | |
22 | introduction of its main features. |
|
22 | introduction of its main features. | |
23 |
|
23 | |||
24 | The Qt frontend has hand-coded emacs-style bindings for text navigation. This |
|
24 | The Qt frontend has hand-coded emacs-style bindings for text navigation. This | |
25 | is not yet configurable. |
|
25 | is not yet configurable. | |
26 |
|
26 | |||
27 | .. tip:: |
|
27 | .. tip:: | |
28 |
|
28 | |||
29 | Since the Qt console tries hard to behave like a terminal, by default it |
|
29 | 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 |
|
30 | 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 |
|
31 | 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 |
|
32 | 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 |
|
33 | point in a multiline block, you can force its execution (without having to | |
34 | go to the bottom) with :kbd:`Shift-Enter`. |
|
34 | go to the bottom) with :kbd:`Shift-Enter`. | |
35 |
|
35 | |||
36 | ``%load`` |
|
36 | ``%load`` | |
37 | ========= |
|
37 | ========= | |
38 |
|
38 | |||
39 | The new ``%load`` magic (previously ``%loadpy``) takes any script, and pastes |
|
39 | 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 |
|
40 | 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 |
|
41 | 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 |
|
42 | url, and it will download the script from the web. This is particularly useful | |
43 | for playing with examples from documentation, such as matplotlib. |
|
43 | for playing with examples from documentation, such as matplotlib. | |
44 |
|
44 | |||
45 | .. sourcecode:: ipython |
|
45 | .. sourcecode:: ipython | |
46 |
|
46 | |||
47 | In [6]: %load http://matplotlib.org/plot_directive/mpl_examples/mplot3d/contour3d_demo.py |
|
47 | In [6]: %load http://matplotlib.org/plot_directive/mpl_examples/mplot3d/contour3d_demo.py | |
48 |
|
48 | |||
49 | In [7]: from mpl_toolkits.mplot3d import axes3d |
|
49 | In [7]: from mpl_toolkits.mplot3d import axes3d | |
50 | ...: import matplotlib.pyplot as plt |
|
50 | ...: import matplotlib.pyplot as plt | |
51 | ...: |
|
51 | ...: | |
52 | ...: fig = plt.figure() |
|
52 | ...: fig = plt.figure() | |
53 | ...: ax = fig.add_subplot(111, projection='3d') |
|
53 | ...: ax = fig.add_subplot(111, projection='3d') | |
54 | ...: X, Y, Z = axes3d.get_test_data(0.05) |
|
54 | ...: X, Y, Z = axes3d.get_test_data(0.05) | |
55 | ...: cset = ax.contour(X, Y, Z) |
|
55 | ...: cset = ax.contour(X, Y, Z) | |
56 | ...: ax.clabel(cset, fontsize=9, inline=1) |
|
56 | ...: ax.clabel(cset, fontsize=9, inline=1) | |
57 | ...: |
|
57 | ...: | |
58 | ...: plt.show() |
|
58 | ...: plt.show() | |
59 |
|
59 | |||
60 | Inline Matplotlib |
|
60 | Inline Matplotlib | |
61 | ================= |
|
61 | ================= | |
62 |
|
62 | |||
63 | One of the most exciting features of the QtConsole is embedded matplotlib |
|
63 | One of the most exciting features of the QtConsole is embedded matplotlib | |
64 | figures. You can use any standard matplotlib GUI backend |
|
64 | figures. You can use any standard matplotlib GUI backend | |
65 | to draw the figures, and since there is now a two-process model, there is no |
|
65 | to draw the figures, and since there is now a two-process model, there is no | |
66 | longer a conflict between user input and the drawing eventloop. |
|
66 | longer a conflict between user input and the drawing eventloop. | |
67 |
|
67 | |||
68 | .. image:: figs/besselj.png |
|
68 | .. image:: figs/besselj.png | |
69 | :width: 519px |
|
69 | :width: 519px | |
70 |
|
70 | |||
71 | .. _display: |
|
71 | .. _display: | |
72 |
|
72 | |||
73 | :func:`display` |
|
73 | :func:`display` | |
74 | *************** |
|
74 | *************** | |
75 |
|
75 | |||
76 | IPython provides a function :func:`display` for displaying rich representations |
|
76 | IPython provides a function :func:`display` for displaying rich representations | |
77 | of objects if they are available. The IPython display |
|
77 | of objects if they are available. The IPython display | |
78 | system provides a mechanism for specifying PNG or SVG (and more) |
|
78 | system provides a mechanism for specifying PNG or SVG (and more) | |
79 | representations of objects for GUI frontends. |
|
79 | representations of objects for GUI frontends. | |
80 | When you enable matplotlib integration via the ``%matplotlib`` magic, IPython registers |
|
80 | When you enable matplotlib integration via the ``%matplotlib`` magic, IPython registers | |
81 | convenient PNG and SVG renderers for matplotlib figures, so you can embed them |
|
81 | convenient PNG and SVG renderers for matplotlib figures, so you can embed them | |
82 | in your document by calling :func:`display` on one or more of them. This is |
|
82 | in your document by calling :func:`display` on one or more of them. This is | |
83 | especially useful for saving_ your work. |
|
83 | especially useful for saving_ your work. | |
84 |
|
84 | |||
85 | .. sourcecode:: ipython |
|
85 | .. sourcecode:: ipython | |
86 |
|
86 | |||
87 | In [4]: from IPython.display import display |
|
87 | In [4]: from IPython.display import display | |
88 |
|
88 | |||
89 | In [5]: plt.plot(range(5)) # plots in the matplotlib window |
|
89 | In [5]: plt.plot(range(5)) # plots in the matplotlib window | |
90 |
|
90 | |||
91 | In [6]: display(plt.gcf()) # embeds the current figure in the qtconsole |
|
91 | In [6]: display(plt.gcf()) # embeds the current figure in the qtconsole | |
92 |
|
92 | |||
93 | In [7]: display(*getfigs()) # embeds all active figures in the qtconsole |
|
93 | In [7]: display(*getfigs()) # embeds all active figures in the qtconsole | |
94 |
|
94 | |||
95 | If you have a reference to a matplotlib figure object, you can always display |
|
95 | If you have a reference to a matplotlib figure object, you can always display | |
96 | that specific figure: |
|
96 | that specific figure: | |
97 |
|
97 | |||
98 | .. sourcecode:: ipython |
|
98 | .. sourcecode:: ipython | |
99 |
|
99 | |||
100 | In [1]: f = plt.figure() |
|
100 | In [1]: f = plt.figure() | |
101 |
|
101 | |||
102 | In [2]: plt.plot(np.rand(100)) |
|
102 | In [2]: plt.plot(np.rand(100)) | |
103 | Out[2]: [<matplotlib.lines.Line2D at 0x7fc6ac03dd90>] |
|
103 | Out[2]: [<matplotlib.lines.Line2D at 0x7fc6ac03dd90>] | |
104 |
|
104 | |||
105 | In [3]: display(f) |
|
105 | In [3]: display(f) | |
106 |
|
106 | |||
107 | # Plot is shown here |
|
107 | # Plot is shown here | |
108 |
|
108 | |||
109 | In [4]: plt.title('A title') |
|
109 | In [4]: plt.title('A title') | |
110 | Out[4]: <matplotlib.text.Text at 0x7fc6ac023450> |
|
110 | Out[4]: <matplotlib.text.Text at 0x7fc6ac023450> | |
111 |
|
111 | |||
112 | In [5]: display(f) |
|
112 | In [5]: display(f) | |
113 |
|
113 | |||
114 | # Updated plot with title is shown here. |
|
114 | # Updated plot with title is shown here. | |
115 |
|
115 | |||
116 | .. _inline: |
|
116 | .. _inline: | |
117 |
|
117 | |||
118 | ``--matplotlib inline`` |
|
118 | ``--matplotlib inline`` | |
119 | *********************** |
|
119 | *********************** | |
120 |
|
120 | |||
121 | If you want to have all of your figures embedded in your session, instead of |
|
121 | If you want to have all of your figures embedded in your session, instead of | |
122 | calling :func:`display`, you can specify ``--matplotlib inline`` when you start the |
|
122 | calling :func:`display`, you can specify ``--matplotlib inline`` when you start the | |
123 | console, and each time you make a plot, it will show up in your document, as if |
|
123 | console, and each time you make a plot, it will show up in your document, as if | |
124 | you had called :func:`display(fig)`. |
|
124 | you had called :func:`display(fig)`. | |
125 |
|
125 | |||
126 | The inline backend can use either SVG or PNG figures (PNG being the default). |
|
126 | The inline backend can use either SVG or PNG figures (PNG being the default). | |
127 | It also supports the special key ``'retina'``, which is 2x PNG for high-DPI displays. |
|
127 | It also supports the special key ``'retina'``, which is 2x PNG for high-DPI displays. | |
128 | To switch between them, set the ``InlineBackend.figure_format`` configurable |
|
128 | To switch between them, set the ``InlineBackend.figure_format`` configurable | |
129 | in a config file, or via the ``%config`` magic: |
|
129 | in a config file, or via the ``%config`` magic: | |
130 |
|
130 | |||
131 | .. sourcecode:: ipython |
|
131 | .. sourcecode:: ipython | |
132 |
|
132 | |||
133 | In [10]: %config InlineBackend.figure_format = 'svg' |
|
133 | In [10]: %config InlineBackend.figure_format = 'svg' | |
134 |
|
134 | |||
135 | .. note:: |
|
135 | .. note:: | |
136 |
|
136 | |||
137 | Changing the inline figure format also affects calls to :func:`display` above, |
|
137 | Changing the inline figure format also affects calls to :func:`display` above, | |
138 | even if you are not using the inline backend for all figures. |
|
138 | even if you are not using the inline backend for all figures. | |
139 |
|
139 | |||
140 | By default, IPython closes all figures at the completion of each execution. This means you |
|
140 | By default, IPython closes all figures at the completion of each execution. This means you | |
141 | don't have to manually close figures, which is less convenient when figures aren't attached |
|
141 | don't have to manually close figures, which is less convenient when figures aren't attached | |
142 | to windows with an obvious close button. It also means that the first matplotlib call in |
|
142 | to windows with an obvious close button. It also means that the first matplotlib call in | |
143 | each cell will always create a new figure: |
|
143 | each cell will always create a new figure: | |
144 |
|
144 | |||
145 | .. sourcecode:: ipython |
|
145 | .. sourcecode:: ipython | |
146 |
|
146 | |||
147 | In [11]: plt.plot(range(100)) |
|
147 | In [11]: plt.plot(range(100)) | |
148 | <single-line plot> |
|
148 | <single-line plot> | |
149 |
|
149 | |||
150 | In [12]: plt.plot([1,3,2]) |
|
150 | In [12]: plt.plot([1,3,2]) | |
151 | <another single-line plot> |
|
151 | <another single-line plot> | |
152 |
|
152 | |||
153 |
|
153 | |||
154 | However, it does prevent the list of active figures surviving from one input cell to the |
|
154 | However, it does prevent the list of active figures surviving from one input cell to the | |
155 | next, so if you want to continue working with a figure, you must hold on to a reference to |
|
155 | next, so if you want to continue working with a figure, you must hold on to a reference to | |
156 | it: |
|
156 | it: | |
157 |
|
157 | |||
158 | .. sourcecode:: ipython |
|
158 | .. sourcecode:: ipython | |
159 |
|
159 | |||
160 | In [11]: fig = gcf() |
|
160 | In [11]: fig = gcf() | |
161 | ....: fig.plot(rand(100)) |
|
161 | ....: fig.plot(rand(100)) | |
162 | <plot> |
|
162 | <plot> | |
163 | In [12]: fig.title('Random Title') |
|
163 | In [12]: fig.title('Random Title') | |
164 | <redraw plot with title> |
|
164 | <redraw plot with title> | |
165 |
|
165 | |||
166 | This behavior is controlled by the :attr:`InlineBackend.close_figures` configurable, and |
|
166 | This behavior is controlled by the :attr:`InlineBackend.close_figures` configurable, and | |
167 | if you set it to False, via %config or config file, then IPython will *not* close figures, |
|
167 | if you set it to False, via %config or config file, then IPython will *not* close figures, | |
168 | and tools like :func:`gcf`, :func:`gca`, :func:`getfigs` will behave the same as they |
|
168 | and tools like :func:`gcf`, :func:`gca`, :func:`getfigs` will behave the same as they | |
169 | do with other backends. You will, however, have to manually close figures: |
|
169 | do with other backends. You will, however, have to manually close figures: | |
170 |
|
170 | |||
171 | .. sourcecode:: ipython |
|
171 | .. sourcecode:: ipython | |
172 |
|
172 | |||
173 | # close all active figures: |
|
173 | # close all active figures: | |
174 | In [13]: [ fig.close() for fig in getfigs() ] |
|
174 | In [13]: [ fig.close() for fig in getfigs() ] | |
175 |
|
175 | |||
176 |
|
176 | |||
177 |
|
177 | |||
178 | .. _saving: |
|
178 | .. _saving: | |
179 |
|
179 | |||
180 | Saving and Printing |
|
180 | Saving and Printing | |
181 | =================== |
|
181 | =================== | |
182 |
|
182 | |||
183 | IPythonQt has the ability to save your current session, as either HTML or |
|
183 | IPythonQt has the ability to save your current session, as either HTML or | |
184 | XHTML. If you have been using :func:`display` or inline_ matplotlib, your figures |
|
184 | XHTML. If you have been using :func:`display` or inline_ matplotlib, your figures | |
185 | will be PNG in HTML, or inlined as SVG in XHTML. PNG images have the option to |
|
185 | will be PNG in HTML, or inlined as SVG in XHTML. PNG images have the option to | |
186 | be either in an external folder, as in many browsers' "Webpage, Complete" |
|
186 | be either in an external folder, as in many browsers' "Webpage, Complete" | |
187 | option, or inlined as well, for a larger, but more portable file. |
|
187 | option, or inlined as well, for a larger, but more portable file. | |
188 |
|
188 | |||
189 | .. note:: |
|
189 | .. note:: | |
190 |
|
190 | |||
191 | Export to SVG+XHTML requires that you are using SVG figures, which is *not* |
|
191 | Export to SVG+XHTML requires that you are using SVG figures, which is *not* | |
192 | the default. To switch the inline figure format to use SVG during an active |
|
192 | the default. To switch the inline figure format to use SVG during an active | |
193 | session, do: |
|
193 | session, do: | |
194 |
|
194 | |||
195 | .. sourcecode:: ipython |
|
195 | .. sourcecode:: ipython | |
196 |
|
196 | |||
197 | In [10]: %config InlineBackend.figure_format = 'svg' |
|
197 | In [10]: %config InlineBackend.figure_format = 'svg' | |
198 |
|
198 | |||
199 | Or, you can add the same line (c.Inline... instead of %config Inline...) to |
|
199 | Or, you can add the same line (c.Inline... instead of %config Inline...) to | |
200 | your config files. |
|
200 | your config files. | |
201 |
|
201 | |||
202 | This will only affect figures plotted after making this call |
|
202 | This will only affect figures plotted after making this call | |
203 |
|
203 | |||
204 |
|
204 | |||
205 | The widget also exposes the ability to print directly, via the default print |
|
205 | The widget also exposes the ability to print directly, via the default print | |
206 | shortcut or context menu. |
|
206 | shortcut or context menu. | |
207 |
|
207 | |||
208 |
|
208 | |||
209 | .. Note:: |
|
209 | .. Note:: | |
210 |
|
210 | |||
211 | Saving is only available to richtext Qt widgets, which are used by default, |
|
211 | Saving is only available to richtext Qt widgets, which are used by default, | |
212 | but if you pass the ``--plain`` flag, saving will not be available to you. |
|
212 | but if you pass the ``--plain`` flag, saving will not be available to you. | |
213 |
|
213 | |||
214 |
|
214 | |||
215 | See these examples of :download:`png/html<figs/jn.html>` and |
|
215 | See these examples of :download:`png/html<figs/jn.html>` and | |
216 | :download:`svg/xhtml <figs/jn.xhtml>` output. Note that syntax highlighting |
|
216 | :download:`svg/xhtml <figs/jn.xhtml>` output. Note that syntax highlighting | |
217 | does not survive export. This is a known issue, and is being investigated. |
|
217 | does not survive export. This is a known issue, and is being investigated. | |
218 |
|
218 | |||
219 |
|
219 | |||
220 | Colors and Highlighting |
|
220 | Colors and Highlighting | |
221 | ======================= |
|
221 | ======================= | |
222 |
|
222 | |||
223 | Terminal IPython has always had some coloring, but never syntax |
|
223 | Terminal IPython has always had some coloring, but never syntax | |
224 | highlighting. There are a few simple color choices, specified by the ``colors`` |
|
224 | highlighting. There are a few simple color choices, specified by the ``colors`` | |
225 | flag or ``%colors`` magic: |
|
225 | flag or ``%colors`` magic: | |
226 |
|
226 | |||
227 | * LightBG for light backgrounds |
|
227 | * LightBG for light backgrounds | |
228 | * Linux for dark backgrounds |
|
228 | * Linux for dark backgrounds | |
229 | * NoColor for a simple colorless terminal |
|
229 | * NoColor for a simple colorless terminal | |
230 |
|
230 | |||
231 | The Qt widget has full support for the ``colors`` flag used in the terminal shell. |
|
231 | The Qt widget has full support for the ``colors`` flag used in the terminal shell. | |
232 |
|
232 | |||
233 | The Qt widget, however, has full syntax highlighting as you type, handled by |
|
233 | The Qt widget, however, has full syntax highlighting as you type, handled by | |
234 | the `pygments`_ library. The ``style`` argument exposes access to any style by |
|
234 | the `pygments`_ library. The ``style`` argument exposes access to any style by | |
235 | name that can be found by pygments, and there are several already |
|
235 | name that can be found by pygments, and there are several already | |
236 | installed. The ``colors`` argument, if unspecified, will be guessed based on |
|
236 | installed. The ``colors`` argument, if unspecified, will be guessed based on | |
237 | the chosen style. Similarly, there are default styles associated with each |
|
237 | the chosen style. Similarly, there are default styles associated with each | |
238 | ``colors`` option. |
|
238 | ``colors`` option. | |
239 |
|
239 | |||
240 |
|
240 | |||
241 | Screenshot of ``ipython qtconsole --colors=linux``, which uses the 'monokai' |
|
241 | Screenshot of ``ipython qtconsole --colors=linux``, which uses the 'monokai' | |
242 | theme by default: |
|
242 | theme by default: | |
243 |
|
243 | |||
244 | .. image:: figs/colors_dark.png |
|
244 | .. image:: figs/colors_dark.png | |
245 | :width: 627px |
|
245 | :width: 627px | |
246 |
|
246 | |||
247 | .. Note:: |
|
247 | .. Note:: | |
248 |
|
248 | |||
249 | Calling ``ipython qtconsole -h`` will show all the style names that |
|
249 | Calling ``ipython qtconsole -h`` will show all the style names that | |
250 | pygments can find on your system. |
|
250 | pygments can find on your system. | |
251 |
|
251 | |||
252 | You can also pass the filename of a custom CSS stylesheet, if you want to do |
|
252 | You can also pass the filename of a custom CSS stylesheet, if you want to do | |
253 | your own coloring, via the ``stylesheet`` argument. The default LightBG |
|
253 | your own coloring, via the ``stylesheet`` argument. The default LightBG | |
254 | stylesheet: |
|
254 | stylesheet: | |
255 |
|
255 | |||
256 | .. sourcecode:: css |
|
256 | .. sourcecode:: css | |
257 |
|
257 | |||
258 | QPlainTextEdit, QTextEdit { background-color: white; |
|
258 | QPlainTextEdit, QTextEdit { background-color: white; | |
259 | color: black ; |
|
259 | color: black ; | |
260 | selection-background-color: #ccc} |
|
260 | selection-background-color: #ccc} | |
261 | .error { color: red; } |
|
261 | .error { color: red; } | |
262 | .in-prompt { color: navy; } |
|
262 | .in-prompt { color: navy; } | |
263 | .in-prompt-number { font-weight: bold; } |
|
263 | .in-prompt-number { font-weight: bold; } | |
264 | .out-prompt { color: darkred; } |
|
264 | .out-prompt { color: darkred; } | |
265 | .out-prompt-number { font-weight: bold; } |
|
265 | .out-prompt-number { font-weight: bold; } | |
266 | /* .inverted is used to highlight selected completion */ |
|
266 | /* .inverted is used to highlight selected completion */ | |
267 | .inverted { background-color: black ; color: white; } |
|
267 | .inverted { background-color: black ; color: white; } | |
268 |
|
268 | |||
269 | Fonts |
|
269 | Fonts | |
270 | ===== |
|
270 | ===== | |
271 |
|
271 | |||
272 | The QtConsole has configurable via the ConsoleWidget. To change these, set the |
|
272 | The QtConsole has configurable via the ConsoleWidget. To change these, set the | |
273 | ``font_family`` or ``font_size`` traits of the ConsoleWidget. For instance, to |
|
273 | ``font_family`` or ``font_size`` traits of the ConsoleWidget. For instance, to | |
274 | use 9pt Anonymous Pro:: |
|
274 | use 9pt Anonymous Pro:: | |
275 |
|
275 | |||
276 | $> ipython qtconsole --ConsoleWidget.font_family="Anonymous Pro" --ConsoleWidget.font_size=9 |
|
276 | $> ipython qtconsole --ConsoleWidget.font_family="Anonymous Pro" --ConsoleWidget.font_size=9 | |
277 |
|
277 | |||
278 | Process Management |
|
278 | Process Management | |
279 | ================== |
|
279 | ================== | |
280 |
|
280 | |||
281 | With the two-process ZMQ model, the frontend does not block input during |
|
281 | With the two-process ZMQ model, the frontend does not block input during | |
282 | execution. This means that actions can be taken by the frontend while the |
|
282 | execution. This means that actions can be taken by the frontend while the | |
283 | Kernel is executing, or even after it crashes. The most basic such command is |
|
283 | Kernel is executing, or even after it crashes. The most basic such command is | |
284 | via 'Ctrl-.', which restarts the kernel. This can be done in the middle of a |
|
284 | via 'Ctrl-.', which restarts the kernel. This can be done in the middle of a | |
285 | blocking execution. The frontend can also know, via a heartbeat mechanism, that |
|
285 | blocking execution. The frontend can also know, via a heartbeat mechanism, that | |
286 | the kernel has died. This means that the frontend can safely restart the |
|
286 | the kernel has died. This means that the frontend can safely restart the | |
287 | kernel. |
|
287 | kernel. | |
288 |
|
288 | |||
289 | .. _multiple_consoles: |
|
289 | .. _multiple_consoles: | |
290 |
|
290 | |||
291 | Multiple Consoles |
|
291 | Multiple Consoles | |
292 | ***************** |
|
292 | ***************** | |
293 |
|
293 | |||
294 | Since the Kernel listens on the network, multiple frontends can connect to it. |
|
294 | Since the Kernel listens on the network, multiple frontends can connect to it. | |
295 | These do not have to all be qt frontends - any IPython frontend can connect and |
|
295 | These do not have to all be qt frontends - any IPython frontend can connect and | |
296 | run code. When you start ipython qtconsole, there will be an output line, |
|
296 | run code. When you start ipython qtconsole, there will be an output line, | |
297 | like:: |
|
297 | like:: | |
298 |
|
298 | |||
299 | [IPKernelApp] To connect another client to this kernel, use: |
|
299 | [IPKernelApp] To connect another client to this kernel, use: | |
300 | [IPKernelApp] --existing kernel-12345.json |
|
300 | [IPKernelApp] --existing kernel-12345.json | |
301 |
|
301 | |||
302 | Other frontends can connect to your kernel, and share in the execution. This is |
|
302 | Other frontends can connect to your kernel, and share in the execution. This is | |
303 | great for collaboration. The ``--existing`` flag means connect to a kernel |
|
303 | great for collaboration. The ``--existing`` flag means connect to a kernel | |
304 | that already exists. Starting other consoles |
|
304 | that already exists. Starting other consoles | |
305 | with that flag will not try to start their own kernel, but rather connect to |
|
305 | with that flag will not try to start their own kernel, but rather connect to | |
306 | yours. :file:`kernel-12345.json` is a small JSON file with the ip, port, and |
|
306 | yours. :file:`kernel-12345.json` is a small JSON file with the ip, port, and | |
307 | authentication information necessary to connect to your kernel. By default, this file |
|
307 | authentication information necessary to connect to your kernel. By default, this file | |
308 | will be in your default profile's security directory. If it is somewhere else, |
|
308 | will be in your default profile's security directory. If it is somewhere else, | |
309 | the output line will print the full path of the connection file, rather than |
|
309 | the output line will print the full path of the connection file, rather than | |
310 | just its filename. |
|
310 | just its filename. | |
311 |
|
311 | |||
312 | If you need to find the connection info to send, and don't know where your connection file |
|
312 | If you need to find the connection info to send, and don't know where your connection file | |
313 | lives, there are a couple of ways to get it. If you are already running an IPython console |
|
313 | lives, there are a couple of ways to get it. If you are already running an IPython console | |
314 | connected to the kernel, you can use the ``%connect_info`` magic to display the information |
|
314 | connected to the kernel, you can use the ``%connect_info`` magic to display the information | |
315 | necessary to connect another frontend to the kernel. |
|
315 | necessary to connect another frontend to the kernel. | |
316 |
|
316 | |||
317 | .. sourcecode:: ipython |
|
317 | .. sourcecode:: ipython | |
318 |
|
318 | |||
319 | In [2]: %connect_info |
|
319 | In [2]: %connect_info | |
320 | { |
|
320 | { | |
321 | "stdin_port":50255, |
|
321 | "stdin_port":50255, | |
322 | "ip":"127.0.0.1", |
|
322 | "ip":"127.0.0.1", | |
323 | "hb_port":50256, |
|
323 | "hb_port":50256, | |
324 | "key":"70be6f0f-1564-4218-8cda-31be40a4d6aa", |
|
324 | "key":"70be6f0f-1564-4218-8cda-31be40a4d6aa", | |
325 | "shell_port":50253, |
|
325 | "shell_port":50253, | |
326 | "iopub_port":50254 |
|
326 | "iopub_port":50254 | |
327 | } |
|
327 | } | |
328 |
|
328 | |||
329 | Paste the above JSON into a file, and connect with: |
|
329 | Paste the above JSON into a file, and connect with: | |
330 | $> ipython <app> --existing <file> |
|
330 | $> ipython <app> --existing <file> | |
331 | or, if you are local, you can connect with just: |
|
331 | or, if you are local, you can connect with just: | |
332 | $> ipython <app> --existing kernel-12345.json |
|
332 | $> ipython <app> --existing kernel-12345.json | |
333 | or even just: |
|
333 | or even just: | |
334 | $> ipython <app> --existing |
|
334 | $> ipython <app> --existing | |
335 | if this is the most recent IPython session you have started. |
|
335 | if this is the most recent IPython session you have started. | |
336 |
|
336 | |||
337 | Otherwise, you can find a connection file by name (and optionally profile) with |
|
337 | Otherwise, you can find a connection file by name (and optionally profile) with | |
338 | :func:`IPython.lib.kernel.find_connection_file`: |
|
338 | :func:`IPython.lib.kernel.find_connection_file`: | |
339 |
|
339 | |||
340 | .. sourcecode:: bash |
|
340 | .. sourcecode:: bash | |
341 |
|
341 | |||
342 | $> python -c "from IPython.lib.kernel import find_connection_file;\ |
|
342 | $> python -c "from IPython.lib.kernel import find_connection_file;\ | |
343 | print find_connection_file('kernel-12345.json')" |
|
343 | print find_connection_file('kernel-12345.json')" | |
344 | /home/you/.ipython/profile_default/security/kernel-12345.json |
|
344 | /home/you/.ipython/profile_default/security/kernel-12345.json | |
345 |
|
345 | |||
346 | And if you are using a particular IPython profile: |
|
346 | And if you are using a particular IPython profile: | |
347 |
|
347 | |||
348 | .. sourcecode:: bash |
|
348 | .. sourcecode:: bash | |
349 |
|
349 | |||
350 | $> python -c "from IPython.lib.kernel import find_connection_file;\ |
|
350 | $> python -c "from IPython.lib.kernel import find_connection_file;\ | |
351 | print find_connection_file('kernel-12345.json', profile='foo')" |
|
351 | print find_connection_file('kernel-12345.json', profile='foo')" | |
352 | /home/you/.ipython/profile_foo/security/kernel-12345.json |
|
352 | /home/you/.ipython/profile_foo/security/kernel-12345.json | |
353 |
|
353 | |||
354 | You can even launch a standalone kernel, and connect and disconnect Qt Consoles |
|
354 | You can even launch a standalone kernel, and connect and disconnect Qt Consoles | |
355 | from various machines. This lets you keep the same running IPython session |
|
355 | from various machines. This lets you keep the same running IPython session | |
356 | on your work machine (with matplotlib plots and everything), logging in from home, |
|
356 | on your work machine (with matplotlib plots and everything), logging in from home, | |
357 | cafΓ©s, etc.:: |
|
357 | cafΓ©s, etc.:: | |
358 |
|
358 | |||
359 | $> ipython kernel |
|
359 | $> ipython kernel | |
360 | [IPKernelApp] To connect another client to this kernel, use: |
|
360 | [IPKernelApp] To connect another client to this kernel, use: | |
361 | [IPKernelApp] --existing kernel-12345.json |
|
361 | [IPKernelApp] --existing kernel-12345.json | |
362 |
|
362 | |||
363 | This is actually exactly the same as the subprocess launched by the qtconsole, so |
|
363 | This is actually exactly the same as the subprocess launched by the qtconsole, so | |
364 | all the information about connecting to a standalone kernel is identical to that |
|
364 | all the information about connecting to a standalone kernel is identical to that | |
365 | of connecting to the kernel attached to a running console. |
|
365 | of connecting to the kernel attached to a running console. | |
366 |
|
366 | |||
367 | .. _kernel_security: |
|
367 | .. _kernel_security: | |
368 |
|
368 | |||
369 | Security |
|
369 | Security | |
370 | -------- |
|
370 | -------- | |
371 |
|
371 | |||
372 | .. warning:: |
|
372 | .. warning:: | |
373 |
|
373 | |||
374 | Since the ZMQ code currently has no encryption, listening on an |
|
374 | Since the ZMQ code currently has no encryption, listening on an | |
375 | external-facing IP is dangerous. You are giving any computer that can see |
|
375 | external-facing IP is dangerous. You are giving any computer that can see | |
376 | you on the network the ability to connect to your kernel, and view your traffic. |
|
376 | you on the network the ability to connect to your kernel, and view your traffic. | |
377 | Read the rest of this section before listening on external ports |
|
377 | Read the rest of this section before listening on external ports | |
378 | or running an IPython kernel on a shared machine. |
|
378 | or running an IPython kernel on a shared machine. | |
379 |
|
379 | |||
380 | By default (for security reasons), the kernel only listens on localhost, so you |
|
380 | By default (for security reasons), the kernel only listens on localhost, so you | |
381 | can only connect multiple frontends to the kernel from your local machine. You |
|
381 | can only connect multiple frontends to the kernel from your local machine. You | |
382 | can specify to listen on an external interface by specifying the ``ip`` |
|
382 | can specify to listen on an external interface by specifying the ``ip`` | |
383 | argument:: |
|
383 | argument:: | |
384 |
|
384 | |||
385 | $> ipython qtconsole --ip=192.168.1.123 |
|
385 | $> ipython qtconsole --ip=192.168.1.123 | |
386 |
|
386 | |||
387 | If you specify the ip as 0.0.0.0 or '*', that means all interfaces, so any |
|
387 | If you specify the ip as 0.0.0.0 or '*', that means all interfaces, so any | |
388 | computer that can see yours on the network can connect to the kernel. |
|
388 | computer that can see yours on the network can connect to the kernel. | |
389 |
|
389 | |||
390 | Messages are not encrypted, so users with access to the ports your kernel is using will be |
|
390 | Messages are not encrypted, so users with access to the ports your kernel is using will be | |
391 | able to see any output of the kernel. They will **NOT** be able to issue shell commands as |
|
391 | able to see any output of the kernel. They will **NOT** be able to issue shell commands as | |
392 | you due to message signatures, which are enabled by default as of IPython 0.12. |
|
392 | you due to message signatures, which are enabled by default as of IPython 0.12. | |
393 |
|
393 | |||
394 | .. warning:: |
|
394 | .. warning:: | |
395 |
|
395 | |||
396 | If you disable message signatures, then any user with access to the ports your |
|
396 | If you disable message signatures, then any user with access to the ports your | |
397 | kernel is listening on can issue arbitrary code as you. **DO NOT** disable message |
|
397 | kernel is listening on can issue arbitrary code as you. **DO NOT** disable message | |
398 | signatures unless you have a lot of trust in your environment. |
|
398 | signatures unless you have a lot of trust in your environment. | |
399 |
|
399 | |||
400 | The one security feature IPython does provide is protection from unauthorized execution. |
|
400 | The one security feature IPython does provide is protection from unauthorized execution. | |
401 | IPython's messaging system will sign messages with HMAC digests using a shared-key. The key |
|
401 | IPython's messaging system will sign messages with HMAC digests using a shared-key. The key | |
402 | is never sent over the network, it is only used to generate a unique hash for each message, |
|
402 | is never sent over the network, it is only used to generate a unique hash for each message, | |
403 | based on its content. When IPython receives a message, it will check that the digest |
|
403 | based on its content. When IPython receives a message, it will check that the digest | |
404 | matches, and discard the message. You can use any file that only you have access to to |
|
404 | matches, and discard the message. You can use any file that only you have access to to | |
405 | generate this key, but the default is just to generate a new UUID. You can generate a random |
|
405 | generate this key, but the default is just to generate a new UUID. You can generate a random | |
406 | private key with:: |
|
406 | private key with:: | |
407 |
|
407 | |||
408 | # generate 1024b of random data, and store in a file only you can read: |
|
408 | # generate 1024b of random data, and store in a file only you can read: | |
409 | # (assumes IPYTHONDIR is defined, otherwise use your IPython directory) |
|
409 | # (assumes IPYTHONDIR is defined, otherwise use your IPython directory) | |
410 | $> python -c "import os; print os.urandom(128).encode('base64')" > $IPYTHONDIR/sessionkey |
|
410 | $> python -c "import os; print os.urandom(128).encode('base64')" > $IPYTHONDIR/sessionkey | |
411 | $> chmod 600 $IPYTHONDIR/sessionkey |
|
411 | $> chmod 600 $IPYTHONDIR/sessionkey | |
412 |
|
412 | |||
413 | The *contents* of this file will be stored in the JSON connection file, so that file |
|
413 | The *contents* of this file will be stored in the JSON connection file, so that file | |
414 | contains everything you need to connect to and use a kernel. |
|
414 | contains everything you need to connect to and use a kernel. | |
415 |
|
415 | |||
416 | To use this generated key, simply specify the ``Session.keyfile`` configurable |
|
416 | To use this generated key, simply specify the ``Session.keyfile`` configurable | |
417 | in :file:`ipython_config.py` or at the command-line, as in:: |
|
417 | in :file:`ipython_config.py` or at the command-line, as in:: | |
418 |
|
418 | |||
419 | # instruct IPython to sign messages with that key, instead of a new UUID |
|
419 | # instruct IPython to sign messages with that key, instead of a new UUID | |
420 | $> ipython qtconsole --Session.keyfile=$IPYTHONDIR/sessionkey |
|
420 | $> ipython qtconsole --Session.keyfile=$IPYTHONDIR/sessionkey | |
421 |
|
421 | |||
422 | .. _ssh_tunnels: |
|
422 | .. _ssh_tunnels: | |
423 |
|
423 | |||
424 | SSH Tunnels |
|
424 | SSH Tunnels | |
425 | ----------- |
|
425 | ----------- | |
426 |
|
426 | |||
427 | Sometimes you want to connect to machines across the internet, or just across |
|
427 | Sometimes you want to connect to machines across the internet, or just across | |
428 | a LAN that either doesn't permit open ports or you don't trust the other |
|
428 | a LAN that either doesn't permit open ports or you don't trust the other | |
429 | machines on the network. To do this, you can use SSH tunnels. SSH tunnels |
|
429 | machines on the network. To do this, you can use SSH tunnels. SSH tunnels | |
430 | are a way to securely forward ports on your local machine to ports on another |
|
430 | are a way to securely forward ports on your local machine to ports on another | |
431 | machine, to which you have SSH access. |
|
431 | machine, to which you have SSH access. | |
432 |
|
432 | |||
433 | In simple cases, IPython's tools can forward ports over ssh by simply adding the |
|
433 | In simple cases, IPython's tools can forward ports over ssh by simply adding the | |
434 | ``--ssh=remote`` argument to the usual ``--existing...`` set of flags for connecting |
|
434 | ``--ssh=remote`` argument to the usual ``--existing...`` set of flags for connecting | |
435 | to a running kernel, after copying the JSON connection file (or its contents) to |
|
435 | to a running kernel, after copying the JSON connection file (or its contents) to | |
436 | the second computer. |
|
436 | the second computer. | |
437 |
|
437 | |||
438 | .. warning:: |
|
438 | .. warning:: | |
439 |
|
439 | |||
440 | Using SSH tunnels does *not* increase localhost security. In fact, when |
|
440 | Using SSH tunnels does *not* increase localhost security. In fact, when | |
441 | tunneling from one machine to another *both* machines have open |
|
441 | tunneling from one machine to another *both* machines have open | |
442 | ports on localhost available for connections to the kernel. |
|
442 | ports on localhost available for connections to the kernel. | |
443 |
|
443 | |||
444 | There are two primary models for using SSH tunnels with IPython. The first |
|
444 | There are two primary models for using SSH tunnels with IPython. The first | |
445 | is to have the Kernel listen only on localhost, and connect to it from |
|
445 | is to have the Kernel listen only on localhost, and connect to it from | |
446 | another machine on the same LAN. |
|
446 | another machine on the same LAN. | |
447 |
|
447 | |||
448 | First, let's start a kernel on machine **worker**, listening only |
|
448 | First, let's start a kernel on machine **worker**, listening only | |
449 | on loopback:: |
|
449 | on loopback:: | |
450 |
|
450 | |||
451 | user@worker $> ipython kernel |
|
451 | user@worker $> ipython kernel | |
452 | [IPKernelApp] To connect another client to this kernel, use: |
|
452 | [IPKernelApp] To connect another client to this kernel, use: | |
453 | [IPKernelApp] --existing kernel-12345.json |
|
453 | [IPKernelApp] --existing kernel-12345.json | |
454 |
|
454 | |||
455 | In this case, the IP that you would connect |
|
455 | In this case, the IP that you would connect | |
456 | to would still be 127.0.0.1, but you want to specify the additional ``--ssh`` argument |
|
456 | to would still be 127.0.0.1, but you want to specify the additional ``--ssh`` argument | |
457 | with the hostname of the kernel (in this example, it's 'worker'):: |
|
457 | with the hostname of the kernel (in this example, it's 'worker'):: | |
458 |
|
458 | |||
459 | user@client $> ipython qtconsole --ssh=worker --existing /path/to/kernel-12345.json |
|
459 | user@client $> ipython qtconsole --ssh=worker --existing /path/to/kernel-12345.json | |
460 |
|
460 | |||
461 | Which will write a new connection file with the forwarded ports, so you can reuse them:: |
|
461 | Which will write a new connection file with the forwarded ports, so you can reuse them:: | |
462 |
|
462 | |||
463 | [IPythonQtConsoleApp] To connect another client via this tunnel, use: |
|
463 | [IPythonQtConsoleApp] To connect another client via this tunnel, use: | |
464 | [IPythonQtConsoleApp] --existing kernel-12345-ssh.json |
|
464 | [IPythonQtConsoleApp] --existing kernel-12345-ssh.json | |
465 |
|
465 | |||
466 | Note again that this opens ports on the *client* machine that point to your kernel. |
|
466 | Note again that this opens ports on the *client* machine that point to your kernel. | |
467 |
|
467 | |||
468 | .. note:: |
|
468 | .. note:: | |
469 |
|
469 | |||
470 | the ssh argument is simply passed to openssh, so it can be fully specified ``user@host:port`` |
|
470 | the ssh argument is simply passed to openssh, so it can be fully specified ``user@host:port`` | |
471 | but it will also respect your aliases, etc. in :file:`.ssh/config` if you have any. |
|
471 | but it will also respect your aliases, etc. in :file:`.ssh/config` if you have any. | |
472 |
|
472 | |||
473 | The second pattern is for connecting to a machine behind a firewall across the internet |
|
473 | The second pattern is for connecting to a machine behind a firewall across the internet | |
474 | (or otherwise wide network). This time, we have a machine **login** that you have ssh access |
|
474 | (or otherwise wide network). This time, we have a machine **login** that you have ssh access | |
475 | to, which can see **kernel**, but **client** is on another network. The important difference |
|
475 | to, which can see **kernel**, but **client** is on another network. The important difference | |
476 | now is that **client** can see **login**, but *not* **worker**. So we need to forward ports from |
|
476 | now is that **client** can see **login**, but *not* **worker**. So we need to forward ports from | |
477 | client to worker *via* login. This means that the kernel must be started listening |
|
477 | client to worker *via* login. This means that the kernel must be started listening | |
478 | on external interfaces, so that its ports are visible to `login`:: |
|
478 | on external interfaces, so that its ports are visible to `login`:: | |
479 |
|
479 | |||
480 | user@worker $> ipython kernel --ip=0.0.0.0 |
|
480 | user@worker $> ipython kernel --ip=0.0.0.0 | |
481 | [IPKernelApp] To connect another client to this kernel, use: |
|
481 | [IPKernelApp] To connect another client to this kernel, use: | |
482 | [IPKernelApp] --existing kernel-12345.json |
|
482 | [IPKernelApp] --existing kernel-12345.json | |
483 |
|
483 | |||
484 | Which we can connect to from the client with:: |
|
484 | Which we can connect to from the client with:: | |
485 |
|
485 | |||
486 | user@client $> ipython qtconsole --ssh=login --ip=192.168.1.123 --existing /path/to/kernel-12345.json |
|
486 | user@client $> ipython qtconsole --ssh=login --ip=192.168.1.123 --existing /path/to/kernel-12345.json | |
487 |
|
487 | |||
488 | .. note:: |
|
488 | .. note:: | |
489 |
|
489 | |||
490 | The IP here is the address of worker as seen from *login*, and need only be specified if |
|
490 | The IP here is the address of worker as seen from *login*, and need only be specified if | |
491 | the kernel used the ambiguous 0.0.0.0 (all interfaces) address. If it had used |
|
491 | the kernel used the ambiguous 0.0.0.0 (all interfaces) address. If it had used | |
492 | 192.168.1.123 to start with, it would not be needed. |
|
492 | 192.168.1.123 to start with, it would not be needed. | |
493 |
|
493 | |||
494 |
|
494 | |||
495 | Manual SSH tunnels |
|
495 | Manual SSH tunnels | |
496 | ------------------ |
|
496 | ------------------ | |
497 |
|
497 | |||
498 | It's possible that IPython's ssh helper functions won't work for you, for various |
|
498 | It's possible that IPython's ssh helper functions won't work for you, for various | |
499 | reasons. You can still connect to remote machines, as long as you set up the tunnels |
|
499 | reasons. You can still connect to remote machines, as long as you set up the tunnels | |
500 | yourself. The basic format of forwarding a local port to a remote one is:: |
|
500 | yourself. The basic format of forwarding a local port to a remote one is:: | |
501 |
|
501 | |||
502 | [client] $> ssh <server> <localport>:<remoteip>:<remoteport> -f -N |
|
502 | [client] $> ssh <server> <localport>:<remoteip>:<remoteport> -f -N | |
503 |
|
503 | |||
504 | This will forward local connections to **localport** on client to **remoteip:remoteport** |
|
504 | This will forward local connections to **localport** on client to **remoteip:remoteport** | |
505 | *via* **server**. Note that remoteip is interpreted relative to *server*, not the client. |
|
505 | *via* **server**. Note that remoteip is interpreted relative to *server*, not the client. | |
506 | So if you have direct ssh access to the machine to which you want to forward connections, |
|
506 | So if you have direct ssh access to the machine to which you want to forward connections, | |
507 | then the server *is* the remote machine, and remoteip should be server's IP as seen from the |
|
507 | then the server *is* the remote machine, and remoteip should be server's IP as seen from the | |
508 | server itself, i.e. 127.0.0.1. Thus, to forward local port 12345 to remote port 54321 on |
|
508 | server itself, i.e. 127.0.0.1. Thus, to forward local port 12345 to remote port 54321 on | |
509 | a machine you can see, do:: |
|
509 | a machine you can see, do:: | |
510 |
|
510 | |||
511 | [client] $> ssh machine 12345:127.0.0.1:54321 -f -N |
|
511 | [client] $> ssh machine 12345:127.0.0.1:54321 -f -N | |
512 |
|
512 | |||
513 | But if your target is actually on a LAN at 192.168.1.123, behind another machine called **login**, |
|
513 | But if your target is actually on a LAN at 192.168.1.123, behind another machine called **login**, | |
514 | then you would do:: |
|
514 | then you would do:: | |
515 |
|
515 | |||
516 | [client] $> ssh login 12345:192.168.1.16:54321 -f -N |
|
516 | [client] $> ssh login 12345:192.168.1.16:54321 -f -N | |
517 |
|
517 | |||
518 | The ``-f -N`` on the end are flags that tell ssh to run in the background, |
|
518 | The ``-f -N`` on the end are flags that tell ssh to run in the background, | |
519 | and don't actually run any commands beyond creating the tunnel. |
|
519 | and don't actually run any commands beyond creating the tunnel. | |
520 |
|
520 | |||
521 | .. seealso:: |
|
521 | .. seealso:: | |
522 |
|
522 | |||
523 | A short discussion of ssh tunnels: http://www.revsys.com/writings/quicktips/ssh-tunnel.html |
|
523 | A short discussion of ssh tunnels: http://www.revsys.com/writings/quicktips/ssh-tunnel.html | |
524 |
|
524 | |||
525 |
|
525 | |||
526 |
|
526 | |||
527 | Stopping Kernels and Consoles |
|
527 | Stopping Kernels and Consoles | |
528 | ***************************** |
|
528 | ***************************** | |
529 |
|
529 | |||
530 | Since there can be many consoles per kernel, the shutdown mechanism and dialog |
|
530 | Since there can be many consoles per kernel, the shutdown mechanism and dialog | |
531 | are probably more complicated than you are used to. Since you don't always want |
|
531 | are probably more complicated than you are used to. Since you don't always want | |
532 | to shutdown a kernel when you close a window, you are given the option to just |
|
532 | to shutdown a kernel when you close a window, you are given the option to just | |
533 | close the console window or also close the Kernel and *all other windows*. Note |
|
533 | close the console window or also close the Kernel and *all other windows*. Note | |
534 | that this only refers to all other *local* windows, as remote Consoles are not |
|
534 | that this only refers to all other *local* windows, as remote Consoles are not | |
535 | allowed to shutdown the kernel, and shutdowns do not close Remote consoles (to |
|
535 | allowed to shutdown the kernel, and shutdowns do not close Remote consoles (to | |
536 | allow for saving, etc.). |
|
536 | allow for saving, etc.). | |
537 |
|
537 | |||
538 | Rules: |
|
538 | Rules: | |
539 |
|
539 | |||
540 | * Restarting the kernel automatically clears all *local* Consoles, and prompts remote |
|
540 | * Restarting the kernel automatically clears all *local* Consoles, and prompts remote | |
541 | Consoles about the reset. |
|
541 | Consoles about the reset. | |
542 | * Shutdown closes all *local* Consoles, and notifies remotes that |
|
542 | * Shutdown closes all *local* Consoles, and notifies remotes that | |
543 | the Kernel has been shutdown. |
|
543 | the Kernel has been shutdown. | |
544 | * Remote Consoles may not restart or shutdown the kernel. |
|
544 | * Remote Consoles may not restart or shutdown the kernel. | |
545 |
|
545 | |||
546 | Qt and the QtConsole |
|
546 | Qt and the QtConsole | |
547 | ==================== |
|
547 | ==================== | |
548 |
|
548 | |||
549 | An important part of working with the QtConsole when you are writing your own |
|
549 | An important part of working with the QtConsole when you are writing your own | |
550 | Qt code is to remember that user code (in the kernel) is *not* in the same |
|
550 | Qt code is to remember that user code (in the kernel) is *not* in the same | |
551 | process as the frontend. This means that there is not necessarily any Qt code |
|
551 | process as the frontend. This means that there is not necessarily any Qt code | |
552 | running in the kernel, and under most normal circumstances there isn't. If, |
|
552 | running in the kernel, and under most normal circumstances there isn't. If, | |
553 | however, you specify ``--matplotlib qt`` at the command-line, then there *will* be a |
|
553 | however, you specify ``--matplotlib qt`` at the command-line, then there *will* be a | |
554 | :class:`QCoreApplication` instance running in the kernel process along with |
|
554 | :class:`QCoreApplication` instance running in the kernel process along with | |
555 | user-code. To get a reference to this application, do: |
|
555 | user-code. To get a reference to this application, do: | |
556 |
|
556 | |||
557 | .. sourcecode:: python |
|
557 | .. sourcecode:: python | |
558 |
|
558 | |||
559 | from PyQt4 import QtCore |
|
559 | from PyQt4 import QtCore | |
560 | app = QtCore.QCoreApplication.instance() |
|
560 | app = QtCore.QCoreApplication.instance() | |
561 | # app will be None if there is no such instance |
|
561 | # app will be None if there is no such instance | |
562 |
|
562 | |||
563 | A common problem listed in the PyQt4 Gotchas_ is the fact that Python's garbage |
|
563 | A common problem listed in the PyQt4 Gotchas_ is the fact that Python's garbage | |
564 | collection will destroy Qt objects (Windows, etc.) once there is no longer a |
|
564 | collection will destroy Qt objects (Windows, etc.) once there is no longer a | |
565 | Python reference to them, so you have to hold on to them. For instance, in: |
|
565 | Python reference to them, so you have to hold on to them. For instance, in: | |
566 |
|
566 | |||
567 | .. sourcecode:: python |
|
567 | .. sourcecode:: python | |
568 |
|
568 | |||
569 | def make_window(): |
|
569 | def make_window(): | |
570 | win = QtGui.QMainWindow() |
|
570 | win = QtGui.QMainWindow() | |
571 |
|
571 | |||
572 | def make_and_return_window(): |
|
572 | def make_and_return_window(): | |
573 | win = QtGui.QMainWindow() |
|
573 | win = QtGui.QMainWindow() | |
574 | return win |
|
574 | return win | |
575 |
|
575 | |||
576 | :func:`make_window` will never draw a window, because garbage collection will |
|
576 | :func:`make_window` will never draw a window, because garbage collection will | |
577 | destroy it before it is drawn, whereas :func:`make_and_return_window` lets the |
|
577 | destroy it before it is drawn, whereas :func:`make_and_return_window` lets the | |
578 | caller decide when the window object should be destroyed. If, as a developer, |
|
578 | caller decide when the window object should be destroyed. If, as a developer, | |
579 | you know that you always want your objects to last as long as the process, you |
|
579 | you know that you always want your objects to last as long as the process, you | |
580 | can attach them to the QApplication instance itself: |
|
580 | can attach them to the QApplication instance itself: | |
581 |
|
581 | |||
582 | .. sourcecode:: python |
|
582 | .. sourcecode:: python | |
583 |
|
583 | |||
584 | # do this just once: |
|
584 | # do this just once: | |
585 | app = QtCore.QCoreApplication.instance() |
|
585 | app = QtCore.QCoreApplication.instance() | |
586 | app.references = set() |
|
586 | app.references = set() | |
587 | # then when you create Windows, add them to the set |
|
587 | # then when you create Windows, add them to the set | |
588 | def make_window(): |
|
588 | def make_window(): | |
589 | win = QtGui.QMainWindow() |
|
589 | win = QtGui.QMainWindow() | |
590 | app.references.add(win) |
|
590 | app.references.add(win) | |
591 |
|
591 | |||
592 | Now the QApplication itself holds a reference to ``win``, so it will never be |
|
592 | Now the QApplication itself holds a reference to ``win``, so it will never be | |
593 | garbage collected until the application itself is destroyed. |
|
593 | garbage collected until the application itself is destroyed. | |
594 |
|
594 | |||
595 | .. _Gotchas: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/gotchas.html#garbage-collection |
|
595 | .. _Gotchas: http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/gotchas.html#garbage-collection | |
596 |
|
596 | |||
597 | Regressions |
|
597 | Regressions | |
598 | =========== |
|
598 | =========== | |
599 |
|
599 | |||
600 | There are some features, where the qt console lags behind the Terminal |
|
600 | There are some features, where the qt console lags behind the Terminal | |
601 | frontend: |
|
601 | frontend: | |
602 |
|
602 | |||
603 | * !cmd input: Due to our use of pexpect, we cannot pass input to subprocesses |
|
603 | * !cmd input: Due to our use of pexpect, we cannot pass input to subprocesses | |
604 | launched using the '!' escape, so you should never call a command that |
|
604 | launched using the '!' escape, so you should never call a command that | |
605 | requires interactive input. For such cases, use the terminal IPython. This |
|
605 | requires interactive input. For such cases, use the terminal IPython. This | |
606 | will not be fixed, as abandoning pexpect would significantly degrade the |
|
606 | will not be fixed, as abandoning pexpect would significantly degrade the | |
607 | console experience. |
|
607 | console experience. | |
608 |
|
608 | |||
609 | .. _PyQt: http://www.riverbankcomputing.co.uk/software/pyqt/download |
|
609 | .. _PyQt: http://www.riverbankcomputing.co.uk/software/pyqt/download | |
610 | .. _pygments: http://pygments.org/ |
|
610 | .. _pygments: http://pygments.org/ |
@@ -1,306 +1,307 b'' | |||||
1 | .. _parallel_overview: |
|
1 | .. _parallel_overview: | |
2 |
|
2 | |||
3 | ============================ |
|
3 | ============================ | |
4 | Overview and getting started |
|
4 | Overview and getting started | |
5 | ============================ |
|
5 | ============================ | |
6 |
|
6 | |||
7 |
|
7 | |||
8 | Examples |
|
8 | Examples | |
9 | ======== |
|
9 | ======== | |
10 |
|
10 | |||
11 | We have various example scripts and notebooks for using IPython.parallel in our |
|
11 | We have various example scripts and notebooks for using IPython.parallel in our | |
12 | :file:`examples/parallel` directory, or they can be found `on GitHub`__. |
|
12 | :file:`examples/parallel` directory, or they can be found `on GitHub`__. | |
13 | Some of these are covered in more detail in the :ref:`examples |
|
13 | Some of these are covered in more detail in the :ref:`examples | |
14 | <parallel_examples>` section. |
|
14 | <parallel_examples>` section. | |
15 |
|
15 | |||
16 | .. __: https://github.com/ipython/ipython/tree/master/examples/parallel |
|
16 | .. __: https://github.com/ipython/ipython/tree/master/examples/parallel | |
17 |
|
17 | |||
18 | Introduction |
|
18 | Introduction | |
19 | ============ |
|
19 | ============ | |
20 |
|
20 | |||
21 | This section gives an overview of IPython's sophisticated and powerful |
|
21 | This section gives an overview of IPython's sophisticated and powerful | |
22 | architecture for parallel and distributed computing. This architecture |
|
22 | architecture for parallel and distributed computing. This architecture | |
23 | abstracts out parallelism in a very general way, which enables IPython to |
|
23 | abstracts out parallelism in a very general way, which enables IPython to | |
24 | support many different styles of parallelism including: |
|
24 | support many different styles of parallelism including: | |
25 |
|
25 | |||
26 | * Single program, multiple data (SPMD) parallelism. |
|
26 | * Single program, multiple data (SPMD) parallelism. | |
27 | * Multiple program, multiple data (MPMD) parallelism. |
|
27 | * Multiple program, multiple data (MPMD) parallelism. | |
28 | * Message passing using MPI. |
|
28 | * Message passing using MPI. | |
29 | * Task farming. |
|
29 | * Task farming. | |
30 | * Data parallel. |
|
30 | * Data parallel. | |
31 | * Combinations of these approaches. |
|
31 | * Combinations of these approaches. | |
32 | * Custom user defined approaches. |
|
32 | * Custom user defined approaches. | |
33 |
|
33 | |||
34 | Most importantly, IPython enables all types of parallel applications to |
|
34 | Most importantly, IPython enables all types of parallel applications to | |
35 | be developed, executed, debugged and monitored *interactively*. Hence, |
|
35 | be developed, executed, debugged and monitored *interactively*. Hence, | |
36 | the ``I`` in IPython. The following are some example usage cases for IPython: |
|
36 | the ``I`` in IPython. The following are some example usage cases for IPython: | |
37 |
|
37 | |||
38 | * Quickly parallelize algorithms that are embarrassingly parallel |
|
38 | * Quickly parallelize algorithms that are embarrassingly parallel | |
39 | using a number of simple approaches. Many simple things can be |
|
39 | using a number of simple approaches. Many simple things can be | |
40 | parallelized interactively in one or two lines of code. |
|
40 | parallelized interactively in one or two lines of code. | |
41 |
|
41 | |||
42 | * Steer traditional MPI applications on a supercomputer from an |
|
42 | * Steer traditional MPI applications on a supercomputer from an | |
43 | IPython session on your laptop. |
|
43 | IPython session on your laptop. | |
44 |
|
44 | |||
45 | * Analyze and visualize large datasets (that could be remote and/or |
|
45 | * Analyze and visualize large datasets (that could be remote and/or | |
46 | distributed) interactively using IPython and tools like |
|
46 | distributed) interactively using IPython and tools like | |
47 | matplotlib/TVTK. |
|
47 | matplotlib/TVTK. | |
48 |
|
48 | |||
49 | * Develop, test and debug new parallel algorithms |
|
49 | * Develop, test and debug new parallel algorithms | |
50 | (that may use MPI) interactively. |
|
50 | (that may use MPI) interactively. | |
51 |
|
51 | |||
52 | * Tie together multiple MPI jobs running on different systems into |
|
52 | * Tie together multiple MPI jobs running on different systems into | |
53 | one giant distributed and parallel system. |
|
53 | one giant distributed and parallel system. | |
54 |
|
54 | |||
55 | * Start a parallel job on your cluster and then have a remote |
|
55 | * Start a parallel job on your cluster and then have a remote | |
56 | collaborator connect to it and pull back data into their |
|
56 | collaborator connect to it and pull back data into their | |
57 | local IPython session for plotting and analysis. |
|
57 | local IPython session for plotting and analysis. | |
58 |
|
58 | |||
59 | * Run a set of tasks on a set of CPUs using dynamic load balancing. |
|
59 | * Run a set of tasks on a set of CPUs using dynamic load balancing. | |
60 |
|
60 | |||
61 | .. tip:: |
|
61 | .. tip:: | |
62 |
|
62 | |||
63 | At the SciPy 2011 conference in Austin, Min Ragan-Kelley presented a |
|
63 | At the SciPy 2011 conference in Austin, Min Ragan-Kelley presented a | |
64 | complete 4-hour tutorial on the use of these features, and all the materials |
|
64 | complete 4-hour tutorial on the use of these features, and all the materials | |
65 | for the tutorial are now `available online`__. That tutorial provides an |
|
65 | for the tutorial are now `available online`__. That tutorial provides an | |
66 | excellent, hands-on oriented complement to the reference documentation |
|
66 | excellent, hands-on oriented complement to the reference documentation | |
67 | presented here. |
|
67 | presented here. | |
68 |
|
68 | |||
69 | .. __: http://minrk.github.com/scipy-tutorial-2011 |
|
69 | .. __: http://minrk.github.com/scipy-tutorial-2011 | |
70 |
|
70 | |||
71 | Architecture overview |
|
71 | Architecture overview | |
72 | ===================== |
|
72 | ===================== | |
73 |
|
73 | |||
74 | .. figure:: figs/wideView.png |
|
74 | .. figure:: figs/wideView.png | |
75 | :width: 300px |
|
75 | :width: 300px | |
76 |
|
76 | |||
77 |
|
77 | |||
78 | The IPython architecture consists of four components: |
|
78 | The IPython architecture consists of four components: | |
79 |
|
79 | |||
80 | * The IPython engine. |
|
80 | * The IPython engine. | |
81 | * The IPython hub. |
|
81 | * The IPython hub. | |
82 | * The IPython schedulers. |
|
82 | * The IPython schedulers. | |
83 | * The controller client. |
|
83 | * The controller client. | |
84 |
|
84 | |||
85 | These components live in the :mod:`IPython.parallel` package and are |
|
85 | These components live in the :mod:`IPython.parallel` package and are | |
86 | installed with IPython. They do, however, have additional dependencies |
|
86 | installed with IPython. They do, however, have additional dependencies | |
87 | that must be installed. For more information, see our |
|
87 | that must be installed. For more information, see our | |
88 | :ref:`installation documentation <install_index>`. |
|
88 | :ref:`installation documentation <install_index>`. | |
89 |
|
89 | |||
90 | .. TODO: include zmq in install_index |
|
90 | .. TODO: include zmq in install_index | |
91 |
|
91 | |||
92 | IPython engine |
|
92 | IPython engine | |
93 | --------------- |
|
93 | --------------- | |
94 |
|
94 | |||
95 | The IPython engine is a Python instance that takes Python commands over a |
|
95 | The IPython engine is a Python instance that takes Python commands over a | |
96 | network connection. Eventually, the IPython engine will be a full IPython |
|
96 | network connection. Eventually, the IPython engine will be a full IPython | |
97 | interpreter, but for now, it is a regular Python interpreter. The engine |
|
97 | interpreter, but for now, it is a regular Python interpreter. The engine | |
98 | can also handle incoming and outgoing Python objects sent over a network |
|
98 | can also handle incoming and outgoing Python objects sent over a network | |
99 | connection. When multiple engines are started, parallel and distributed |
|
99 | connection. When multiple engines are started, parallel and distributed | |
100 | computing becomes possible. An important feature of an IPython engine is |
|
100 | computing becomes possible. An important feature of an IPython engine is | |
101 | that it blocks while user code is being executed. Read on for how the |
|
101 | that it blocks while user code is being executed. Read on for how the | |
102 | IPython controller solves this problem to expose a clean asynchronous API |
|
102 | IPython controller solves this problem to expose a clean asynchronous API | |
103 | to the user. |
|
103 | to the user. | |
104 |
|
104 | |||
105 | IPython controller |
|
105 | IPython controller | |
106 | ------------------ |
|
106 | ------------------ | |
107 |
|
107 | |||
108 | The IPython controller processes provide an interface for working with a set of engines. |
|
108 | The IPython controller processes provide an interface for working with a set of engines. | |
109 | At a general level, the controller is a collection of processes to which IPython engines |
|
109 | At a general level, the controller is a collection of processes to which IPython engines | |
110 | and clients can connect. The controller is composed of a :class:`Hub` and a collection of |
|
110 | and clients can connect. The controller is composed of a :class:`Hub` and a collection of | |
111 | :class:`Schedulers`. These Schedulers are typically run in separate processes but on the |
|
111 | :class:`Schedulers`. These Schedulers are typically run in separate processes but on the | |
112 | same machine as the Hub, but can be run anywhere from local threads or on remote machines. |
|
112 | same machine as the Hub, but can be run anywhere from local threads or on remote machines. | |
113 |
|
113 | |||
114 | The controller also provides a single point of contact for users who wish to |
|
114 | The controller also provides a single point of contact for users who wish to | |
115 | utilize the engines connected to the controller. There are different ways of |
|
115 | utilize the engines connected to the controller. There are different ways of | |
116 | working with a controller. In IPython, all of these models are implemented via |
|
116 | working with a controller. In IPython, all of these models are implemented via | |
117 | the :meth:`.View.apply` method, after |
|
117 | the :meth:`.View.apply` method, after | |
118 | constructing :class:`.View` objects to represent subsets of engines. The two |
|
118 | constructing :class:`.View` objects to represent subsets of engines. The two | |
119 | primary models for interacting with engines are: |
|
119 | primary models for interacting with engines are: | |
120 |
|
120 | |||
121 | * A **Direct** interface, where engines are addressed explicitly. |
|
121 | * A **Direct** interface, where engines are addressed explicitly. | |
122 | * A **LoadBalanced** interface, where the Scheduler is trusted with assigning work to |
|
122 | * A **LoadBalanced** interface, where the Scheduler is trusted with assigning work to | |
123 | appropriate engines. |
|
123 | appropriate engines. | |
124 |
|
124 | |||
125 | Advanced users can readily extend the View models to enable other |
|
125 | Advanced users can readily extend the View models to enable other | |
126 | styles of parallelism. |
|
126 | styles of parallelism. | |
127 |
|
127 | |||
128 | .. note:: |
|
128 | .. note:: | |
129 |
|
129 | |||
130 | A single controller and set of engines can be used with multiple models |
|
130 | A single controller and set of engines can be used with multiple models | |
131 | simultaneously. This opens the door for lots of interesting things. |
|
131 | simultaneously. This opens the door for lots of interesting things. | |
132 |
|
132 | |||
133 |
|
133 | |||
134 | The Hub |
|
134 | The Hub | |
135 | ******* |
|
135 | ******* | |
136 |
|
136 | |||
137 | The center of an IPython cluster is the Hub. This is the process that keeps |
|
137 | The center of an IPython cluster is the Hub. This is the process that keeps | |
138 | track of engine connections, schedulers, clients, as well as all task requests and |
|
138 | track of engine connections, schedulers, clients, as well as all task requests and | |
139 | results. The primary role of the Hub is to facilitate queries of the cluster state, and |
|
139 | results. The primary role of the Hub is to facilitate queries of the cluster state, and | |
140 | minimize the necessary information required to establish the many connections involved in |
|
140 | minimize the necessary information required to establish the many connections involved in | |
141 | connecting new clients and engines. |
|
141 | connecting new clients and engines. | |
142 |
|
142 | |||
143 |
|
143 | |||
144 | Schedulers |
|
144 | Schedulers | |
145 | ********** |
|
145 | ********** | |
146 |
|
146 | |||
147 | All actions that can be performed on the engine go through a Scheduler. While the engines |
|
147 | All actions that can be performed on the engine go through a Scheduler. While the engines | |
148 | themselves block when user code is run, the schedulers hide that from the user to provide |
|
148 | themselves block when user code is run, the schedulers hide that from the user to provide | |
149 | a fully asynchronous interface to a set of engines. |
|
149 | a fully asynchronous interface to a set of engines. | |
150 |
|
150 | |||
151 |
|
151 | |||
152 | IPython client and views |
|
152 | IPython client and views | |
153 | ------------------------ |
|
153 | ------------------------ | |
154 |
|
154 | |||
155 | There is one primary object, the :class:`~.parallel.Client`, for connecting to a cluster. |
|
155 | There is one primary object, the :class:`~.parallel.Client`, for connecting to a cluster. | |
156 | For each execution model, there is a corresponding :class:`~.parallel.View`. These views |
|
156 | For each execution model, there is a corresponding :class:`~.parallel.View`. These views | |
157 | allow users to interact with a set of engines through the interface. Here are the two default |
|
157 | allow users to interact with a set of engines through the interface. Here are the two default | |
158 | views: |
|
158 | views: | |
159 |
|
159 | |||
160 | * The :class:`DirectView` class for explicit addressing. |
|
160 | * The :class:`DirectView` class for explicit addressing. | |
161 | * The :class:`LoadBalancedView` class for destination-agnostic scheduling. |
|
161 | * The :class:`LoadBalancedView` class for destination-agnostic scheduling. | |
162 |
|
162 | |||
163 | Security |
|
163 | Security | |
164 | -------- |
|
164 | -------- | |
165 |
|
165 | |||
166 | IPython uses ZeroMQ for networking, which has provided many advantages, but |
|
166 | IPython uses ZeroMQ for networking, which has provided many advantages, but | |
167 | one of the setbacks is its utter lack of security [ZeroMQ]_. By default, no IPython |
|
167 | one of the setbacks is its utter lack of security [ZeroMQ]_. By default, no IPython | |
168 | connections are encrypted, but open ports only listen on localhost. The only |
|
168 | connections are encrypted, but open ports only listen on localhost. The only | |
169 | source of security for IPython is via ssh-tunnel. IPython supports both shell |
|
169 | source of security for IPython is via ssh-tunnel. IPython supports both shell | |
170 | (`openssh`) and `paramiko` based tunnels for connections. There is a key necessary |
|
170 | (`openssh`) and `paramiko` based tunnels for connections. There is a key necessary | |
171 | to submit requests, but due to the lack of encryption, it does not provide |
|
171 | to submit requests, but due to the lack of encryption, it does not provide | |
172 | significant security if loopback traffic is compromised. |
|
172 | significant security if loopback traffic is compromised. | |
173 |
|
173 | |||
174 | In our architecture, the controller is the only process that listens on |
|
174 | In our architecture, the controller is the only process that listens on | |
175 | network ports, and is thus the main point of vulnerability. The standard model |
|
175 | network ports, and is thus the main point of vulnerability. The standard model | |
176 | for secure connections is to designate that the controller listen on |
|
176 | for secure connections is to designate that the controller listen on | |
177 | localhost, and use ssh-tunnels to connect clients and/or |
|
177 | localhost, and use ssh-tunnels to connect clients and/or | |
178 | engines. |
|
178 | engines. | |
179 |
|
179 | |||
180 | To connect and authenticate to the controller an engine or client needs |
|
180 | To connect and authenticate to the controller an engine or client needs | |
181 | some information that the controller has stored in a JSON file. |
|
181 | some information that the controller has stored in a JSON file. | |
182 | Thus, the JSON files need to be copied to a location where |
|
182 | Thus, the JSON files need to be copied to a location where | |
183 | the clients and engines can find them. Typically, this is the |
|
183 | the clients and engines can find them. Typically, this is the | |
184 | :file:`~/.ipython/profile_default/security` directory on the host where the |
|
184 | :file:`~/.ipython/profile_default/security` directory on the host where the | |
185 | client/engine is running (which could be a different host than the controller). |
|
185 | client/engine is running (which could be a different host than the controller). | |
186 | Once the JSON files are copied over, everything should work fine. |
|
186 | Once the JSON files are copied over, everything should work fine. | |
187 |
|
187 | |||
188 | Currently, there are two JSON files that the controller creates: |
|
188 | Currently, there are two JSON files that the controller creates: | |
189 |
|
189 | |||
190 | ipcontroller-engine.json |
|
190 | ipcontroller-engine.json | |
191 | This JSON file has the information necessary for an engine to connect |
|
191 | This JSON file has the information necessary for an engine to connect | |
192 | to a controller. |
|
192 | to a controller. | |
193 |
|
193 | |||
194 | ipcontroller-client.json |
|
194 | ipcontroller-client.json | |
195 | The client's connection information. This may not differ from the engine's, |
|
195 | The client's connection information. This may not differ from the engine's, | |
196 | but since the controller may listen on different ports for clients and |
|
196 | but since the controller may listen on different ports for clients and | |
197 | engines, it is stored separately. |
|
197 | engines, it is stored separately. | |
198 |
|
198 | |||
199 | ipcontroller-client.json will look something like this, under default localhost |
|
199 | ipcontroller-client.json will look something like this, under default localhost | |
200 | circumstances: |
|
200 | circumstances: | |
201 |
|
201 | |||
202 | .. sourcecode:: python |
|
202 | .. sourcecode:: python | |
203 |
|
203 | |||
204 | { |
|
204 | { | |
205 | "url":"tcp:\/\/127.0.0.1:54424", |
|
205 | "url":"tcp:\/\/127.0.0.1:54424", | |
206 | "exec_key":"a361fe89-92fc-4762-9767-e2f0a05e3130", |
|
206 | "exec_key":"a361fe89-92fc-4762-9767-e2f0a05e3130", | |
207 | "ssh":"", |
|
207 | "ssh":"", | |
208 | "location":"10.19.1.135" |
|
208 | "location":"10.19.1.135" | |
209 | } |
|
209 | } | |
210 |
|
210 | |||
211 | If, however, you are running the controller on a work node on a cluster, you will likely |
|
211 | If, however, you are running the controller on a work node on a cluster, you will likely | |
212 | need to use ssh tunnels to connect clients from your laptop to it. You will also |
|
212 | need to use ssh tunnels to connect clients from your laptop to it. You will also | |
213 | probably need to instruct the controller to listen for engines coming from other work nodes |
|
213 | probably need to instruct the controller to listen for engines coming from other work nodes | |
214 | on the cluster. An example of ipcontroller-client.json, as created by:: |
|
214 | on the cluster. An example of ipcontroller-client.json, as created by:: | |
215 |
|
215 | |||
216 | $> ipcontroller --ip=* --ssh=login.mycluster.com |
|
216 | $> ipcontroller --ip=* --ssh=login.mycluster.com | |
217 |
|
217 | |||
218 |
|
218 | |||
219 | .. sourcecode:: python |
|
219 | .. sourcecode:: python | |
220 |
|
220 | |||
221 | { |
|
221 | { | |
222 | "url":"tcp:\/\/*:54424", |
|
222 | "url":"tcp:\/\/*:54424", | |
223 | "exec_key":"a361fe89-92fc-4762-9767-e2f0a05e3130", |
|
223 | "exec_key":"a361fe89-92fc-4762-9767-e2f0a05e3130", | |
224 | "ssh":"login.mycluster.com", |
|
224 | "ssh":"login.mycluster.com", | |
225 | "location":"10.0.0.2" |
|
225 | "location":"10.0.0.2" | |
226 | } |
|
226 | } | |
|
227 | ||||
227 | More details of how these JSON files are used are given below. |
|
228 | More details of how these JSON files are used are given below. | |
228 |
|
229 | |||
229 | A detailed description of the security model and its implementation in IPython |
|
230 | A detailed description of the security model and its implementation in IPython | |
230 | can be found :ref:`here <parallelsecurity>`. |
|
231 | can be found :ref:`here <parallelsecurity>`. | |
231 |
|
232 | |||
232 | .. warning:: |
|
233 | .. warning:: | |
233 |
|
234 | |||
234 | Even at its most secure, the Controller listens on ports on localhost, and |
|
235 | Even at its most secure, the Controller listens on ports on localhost, and | |
235 | every time you make a tunnel, you open a localhost port on the connecting |
|
236 | every time you make a tunnel, you open a localhost port on the connecting | |
236 | machine that points to the Controller. If localhost on the Controller's |
|
237 | machine that points to the Controller. If localhost on the Controller's | |
237 | machine, or the machine of any client or engine, is untrusted, then your |
|
238 | machine, or the machine of any client or engine, is untrusted, then your | |
238 | Controller is insecure. There is no way around this with ZeroMQ. |
|
239 | Controller is insecure. There is no way around this with ZeroMQ. | |
239 |
|
240 | |||
240 |
|
241 | |||
241 |
|
242 | |||
242 | Getting Started |
|
243 | Getting Started | |
243 | =============== |
|
244 | =============== | |
244 |
|
245 | |||
245 | To use IPython for parallel computing, you need to start one instance of the |
|
246 | To use IPython for parallel computing, you need to start one instance of the | |
246 | controller and one or more instances of the engine. Initially, it is best to |
|
247 | controller and one or more instances of the engine. Initially, it is best to | |
247 | simply start a controller and engines on a single host using the |
|
248 | simply start a controller and engines on a single host using the | |
248 | :command:`ipcluster` command. To start a controller and 4 engines on your |
|
249 | :command:`ipcluster` command. To start a controller and 4 engines on your | |
249 | localhost, just do:: |
|
250 | localhost, just do:: | |
250 |
|
251 | |||
251 | $ ipcluster start -n 4 |
|
252 | $ ipcluster start -n 4 | |
252 |
|
253 | |||
253 | More details about starting the IPython controller and engines can be found |
|
254 | More details about starting the IPython controller and engines can be found | |
254 | :ref:`here <parallel_process>` |
|
255 | :ref:`here <parallel_process>` | |
255 |
|
256 | |||
256 | Once you have started the IPython controller and one or more engines, you |
|
257 | Once you have started the IPython controller and one or more engines, you | |
257 | are ready to use the engines to do something useful. To make sure |
|
258 | are ready to use the engines to do something useful. To make sure | |
258 | everything is working correctly, try the following commands: |
|
259 | everything is working correctly, try the following commands: | |
259 |
|
260 | |||
260 | .. sourcecode:: ipython |
|
261 | .. sourcecode:: ipython | |
261 |
|
262 | |||
262 | In [1]: from IPython.parallel import Client |
|
263 | In [1]: from IPython.parallel import Client | |
263 |
|
264 | |||
264 | In [2]: c = Client() |
|
265 | In [2]: c = Client() | |
265 |
|
266 | |||
266 | In [4]: c.ids |
|
267 | In [4]: c.ids | |
267 | Out[4]: set([0, 1, 2, 3]) |
|
268 | Out[4]: set([0, 1, 2, 3]) | |
268 |
|
269 | |||
269 | In [5]: c[:].apply_sync(lambda : "Hello, World") |
|
270 | In [5]: c[:].apply_sync(lambda : "Hello, World") | |
270 | Out[5]: [ 'Hello, World', 'Hello, World', 'Hello, World', 'Hello, World' ] |
|
271 | Out[5]: [ 'Hello, World', 'Hello, World', 'Hello, World', 'Hello, World' ] | |
271 |
|
272 | |||
272 |
|
273 | |||
273 | When a client is created with no arguments, the client tries to find the corresponding JSON file |
|
274 | When a client is created with no arguments, the client tries to find the corresponding JSON file | |
274 | in the local `~/.ipython/profile_default/security` directory. Or if you specified a profile, |
|
275 | in the local `~/.ipython/profile_default/security` directory. Or if you specified a profile, | |
275 | you can use that with the Client. This should cover most cases: |
|
276 | you can use that with the Client. This should cover most cases: | |
276 |
|
277 | |||
277 | .. sourcecode:: ipython |
|
278 | .. sourcecode:: ipython | |
278 |
|
279 | |||
279 | In [2]: c = Client(profile='myprofile') |
|
280 | In [2]: c = Client(profile='myprofile') | |
280 |
|
281 | |||
281 | If you have put the JSON file in a different location or it has a different name, create the |
|
282 | If you have put the JSON file in a different location or it has a different name, create the | |
282 | client like this: |
|
283 | client like this: | |
283 |
|
284 | |||
284 | .. sourcecode:: ipython |
|
285 | .. sourcecode:: ipython | |
285 |
|
286 | |||
286 | In [2]: c = Client('/path/to/my/ipcontroller-client.json') |
|
287 | In [2]: c = Client('/path/to/my/ipcontroller-client.json') | |
287 |
|
288 | |||
288 | Remember, a client needs to be able to see the Hub's ports to connect. So if they are on a |
|
289 | Remember, a client needs to be able to see the Hub's ports to connect. So if they are on a | |
289 | different machine, you may need to use an ssh server to tunnel access to that machine, |
|
290 | different machine, you may need to use an ssh server to tunnel access to that machine, | |
290 | then you would connect to it with: |
|
291 | then you would connect to it with: | |
291 |
|
292 | |||
292 | .. sourcecode:: ipython |
|
293 | .. sourcecode:: ipython | |
293 |
|
294 | |||
294 | In [2]: c = Client('/path/to/my/ipcontroller-client.json', sshserver='me@myhub.example.com') |
|
295 | In [2]: c = Client('/path/to/my/ipcontroller-client.json', sshserver='me@myhub.example.com') | |
295 |
|
296 | |||
296 | Where 'myhub.example.com' is the url or IP address of the machine on |
|
297 | Where 'myhub.example.com' is the url or IP address of the machine on | |
297 | which the Hub process is running (or another machine that has direct access to the Hub's ports). |
|
298 | which the Hub process is running (or another machine that has direct access to the Hub's ports). | |
298 |
|
299 | |||
299 | The SSH server may already be specified in ipcontroller-client.json, if the controller was |
|
300 | The SSH server may already be specified in ipcontroller-client.json, if the controller was | |
300 | instructed at its launch time. |
|
301 | instructed at its launch time. | |
301 |
|
302 | |||
302 | You are now ready to learn more about the :ref:`Direct |
|
303 | You are now ready to learn more about the :ref:`Direct | |
303 | <parallel_multiengine>` and :ref:`LoadBalanced <parallel_task>` interfaces to the |
|
304 | <parallel_multiengine>` and :ref:`LoadBalanced <parallel_task>` interfaces to the | |
304 | controller. |
|
305 | controller. | |
305 |
|
306 | |||
306 | .. [ZeroMQ] ZeroMQ. http://www.zeromq.org |
|
307 | .. [ZeroMQ] ZeroMQ. http://www.zeromq.org |
General Comments 0
You need to be logged in to leave comments.
Login now