##// END OF EJS Templates
Hard word wrap at 80 chars
David P. Sanders -
Show More
@@ -3,9 +3,11 b''
3 3 The IPython Notebook
4 4 ====================
5 5
6 The IPython Notebook is part of the IPython package, which aims to provide a powerful, interactive approach to scientific computation.
7 The IPython Notebook extends the previous text-console-based approach, and the later Qt console, in a qualitatively new diretion, providing a web-based application suitable for capturing the whole scientific computation process.
8
6 The IPython Notebook is part of the IPython package, which aims to provide a
7 powerful, interactive approach to scientific computation.
8 The IPython Notebook extends the previous text-console-based approach, and the
9 later Qt console, in a qualitatively new diretion, providing a web-based
10 application suitable for capturing the whole scientific computation process.
9 11
10 12 .. seealso::
11 13
@@ -19,16 +21,34 b' The IPython Notebook combines two components:'
19 21
20 22 * **The IPython Notebook web application**:
21 23
22 The IPython Notebook web app is a browser-based tool for interactive authoring of literate computations, in which explanatory text, mathematics,computations and rich media output may be combined. Input and output are stored in persistent cells that may be edited in-place.
24 The IPython Notebook web app is a browser-based tool for interactive
25 authoring of literate computations, in which explanatory text, mathematics,
26 computations and rich media output may be combined. Input and output are
27 stored in persistent cells that may be edited in-place.
23 28
24 29 * **Notebook documents**:
25 30
26 *Notebook documents*, or *notebooks*, are plain text documents which record all inputs and outputs of the computations, interspersed with text, mathematics and HTML 5 representations of objects, in a literate style.
27
28 Since the similarity in names can lead to some confusion, in the documentation we will use capitalization of the word "notebook" to distinguish the *N*otebook app and *n*otebook documents, thinking of the Notebook app as being a proper noun. We will also always refer to the "Notebook app" when we are referring to the browser-based interface, and usually to "notebook documents", instead of "notebooks", for added precision.
29
30 We refer to the current state of the computational process taking place in the Notebook app, i.e. the (numbered) sequence of input and output cells, as the
31 *notebook space*. Notebook documents provide an *exact*, *one-to-one* record of all the content in the notebook space, as a plain text file in JSON format. The Notebook app automatically saves, at certain intervals, the contents of the notebook space to a notebook document stored on disk, with the same name as the title of the notebook space, and the file extension ``.ipynb``. For this reason, there is no confusion about using the same word "notebook" for both the notebook space and the corresonding notebook document, since they are really one and the same concept (we could say that they are "isomorphic").
31 *Notebook documents*, or *notebooks*, are plain text documents which record
32 all inputs and outputs of the computations, interspersed with text,
33 mathematics and HTML 5 representations of objects, in a literate style.
34
35 Since the similarity in names can lead to some confusion, in the documentation
36 we will use capitalization of the word "notebook" to distinguish the
37 *N*otebook app and *n*otebook documents, thinking of the Notebook app as being
38 a proper noun. We will also always refer to the "Notebook app" when we are
39 referring to the browser-based interface, and usually to "notebook documents",
40 instead of "notebooks", for added precision.
41
42 We refer to the current state of the computational process taking place in the
43 Notebook app, i.e. the (numbered) sequence of input and output cells, as the
44 *notebook space*. Notebook documents provide an *exact*, *one-to-one* record
45 of all the content in the notebook space, as a plain text file in JSON format.
46 The Notebook app automatically saves, at certain intervals, the contents of
47 the notebook space to a notebook document stored on disk, with the same name
48 as the title of the notebook space, and the file extension ``.ipynb``. For
49 this reason, there is no confusion about using the same word "notebook" for
50 both the notebook space and the corresonding notebook document, since they are
51 really one and the same concept (we could say that they are "isomorphic").
32 52
33 53
34 54 Main features of the IPython Notebook web app
@@ -36,11 +56,16 b' Main features of the IPython Notebook web app'
36 56
37 57 The main features of the IPython Notebook app include:
38 58
39 * In-browser editing for code, with automatic syntax highlighting and indentation and tab completion/introspection.
40 * Literate combination of code with rich text using the Markdown markup language.
41 * Mathematics is easily included within the Markdown using LaTeX notation, and rendered natively by MathJax.
42 * Displays rich data representations (e.g. HTML / LaTeX / SVG) as the result of computations.
43 * Publication-quality figures in a range of formats (SVG / PNG), rendered by the ``matplotlib`` library, may be included inline and exported.
59 * In-browser editing for code, with automatic syntax highlighting and
60 indentation and tab completion/introspection.
61 * Literate combination of code with rich text using the Markdown markup
62 language.
63 * Mathematics is easily included within the Markdown using LaTeX notation, and
64 rendered natively by MathJax.
65 * Displays rich data representations (e.g. HTML / LaTeX / SVG) as the result
66 of computations.
67 * Publication-quality figures in a range of formats (SVG / PNG), rendered by
68 the ``matplotlib`` library, may be included inline and exported.
44 69
45 70
46 71 Notebook documents
@@ -50,7 +75,8 b' Notebook document files are just standard text files with the extension'
50 75 ``.ipynb``, stored in the working directory on your computer. This file can be easily put under version control and shared with colleagues.
51 76
52 77 Despite the fact that the notebook documents are plain text files, they use
53 the JSON format in order to store a *complete*, *reproducible*, *one-to-one* copy of the state of the computational state as it is inside the Notebook app.
78 the JSON format in order to store a *complete*, *reproducible*, *one-to-one*
79 copy of the state of the computational state as it is inside the Notebook app.
54 80 All computations carried out, and the corresponding results obtained, can be
55 81 combined in a literate way, mixing them with descriptive text, mathematics,
56 82 and HTML 5 representations of objects.
@@ -59,7 +85,8 b' Notebooks may easily be exported to a range of static formats, including'
59 85 HTML (for example, for blog posts), PDF and slide shows.
60 86 Furthermore, any publicly available notebook may be shared via the
61 87 `IPython Notebook Viewer <http://nbviewer.ipython.org>`_ service, which will
62 provide it as a static web page. The results may thus be shared without having to install anything.
88 provide it as a static web page. The results may thus be shared without having
89 to install anything.
63 90
64 91 See the :ref:`installation documentation <install_index>` for directions on
65 92 how to install the notebook and its dependencies.
@@ -68,8 +95,8 b' how to install the notebook and its dependencies.'
68 95
69 96 You can start more than one notebook server at the same time, if you want to
70 97 work on notebooks in different directories. By default the first notebook
71 server starts on port 8888, and later notebook servers search for ports near
72 that one. You can also manually specify the port with the ``--port``
98 server starts on port 8888, and later notebook servers search for ports
99 near that one. You can also manually specify the port with the ``--port``
73 100 option.
74 101
75 102
@@ -80,82 +107,125 b' You can start running the Notebook web app using the following command::'
80 107
81 108 $ ipython notebook
82 109
83 The landing page of the notebook server application, the *dashboard*, shows the notebooks currently available in the *working directory* (the directory from which the notebook was started).
110 The landing page of the notebook server application, the *dashboard*, shows
111 the notebooks currently available in the *working directory* (the directory
112 from which the notebook was started).
84 113 You can create new notebooks from the dashboard with the ``New Notebook``
85 114 button, or open existing ones by clicking on their name.
86 You can also drag and drop ``.ipynb`` notebooks and standard ``.py`` Python source code files into the notebook list area.
115 You can also drag and drop ``.ipynb`` notebooks and standard ``.py`` Python
116 source code files into the notebook list area.
87 117
88 ``.py`` files will be imported into the IPython Notebook as a notebook with the same name, but an ``.ipynb`` extension, located in the working directory. The notebook created will have just one cell, which will contain all the
89 code in the ``.py`` file. You can later manually partition this into individual cells using the ``Edit | Split Cell`` menu option, or the :kbd:`Ctrl-m -` keyboard shortcut.
118 ``.py`` files will be imported into the IPython Notebook as a notebook with
119 the same name, but an ``.ipynb`` extension, located in the working directory.
120 The notebook created will have just one cell, which will contain all the
121 code in the ``.py`` file. You can later manually partition this into
122 individual cells using the ``Edit | Split Cell`` menu option, or the
123 :kbd:`Ctrl-m -` keyboard shortcut.
90 124
91 .. Alternatively, prior to importing the ``.py``, you can manually add ``# <nbformat>2</nbformat>`` at the start of the file, and then add separators for text and code cells, to get a cleaner import with the file already broken into individual cells.
125 .. Alternatively, prior to importing the ``.py``, you can manually add ``# <
126 nbformat>2</nbformat>`` at the start of the file, and then add separators for
127 text and code cells, to get a cleaner import with the file already broken into
128 individual cells.
92 129
93
94 When you open or create a new notebook, your browser tab will reflect the name of that notebook, prefixed with "IPy".
95 The URL is currently not meant to be human-readable and is not persistent across invocations of the notebook server; however, this will change in a future version of IPython.
130 When you open or create a new notebook, your browser tab will reflect the name
131 of that notebook, prefixed with "IPy".
132 The URL is currently not meant to be human-readable and is not persistent
133 across invocations of the notebook server; however, this will change in a
134 future version of IPython.
96 135
97 136
98 137 The IPython Notebook web app is based on a server-client structure.
99 This server uses a two-process kernel architecture based on ZeroMQ, as well as Tornado for serving HTTP requests. Other clients may connect to the same underlying IPython kernel; see below.
100
101
138 This server uses a two-process kernel architecture based on ZeroMQ, as well as
139 Tornado for serving HTTP requests. Other clients may connect to the same
140 underlying IPython kernel; see below.
102 141
103 142
104 143 Notebook user interface
105 144 -----------------------
106 145
107 When you open a new notebook document in the Notebook, you will be presented with the title associated to the notebook space/document, a *menu bar*, a *toolbar* and an empty *input cell*.
146 When you open a new notebook document in the Notebook, you will be presented
147 with the title associated to the notebook space/document, a *menu bar*, a
148 *toolbar* and an empty *input cell*.
108 149
109 150 Notebook title
110 151 ~~~~~~~~~~~~~~
111 The title of the notebook document that is currently being edited is displayed at the top of the page, next to the ``IP[y]: Notebook`` logo. This title may be edited directly by clicking on it. The title is reflected in the name of the ``.ipynb`` notebook document file that is saved.
152 The title of the notebook document that is currently being edited is displayed
153 at the top of the page, next to the ``IP[y]: Notebook`` logo. This title may
154 be edited directly by clicking on it. The title is reflected in the name of
155 the ``.ipynb`` notebook document file that is saved.
112 156
113 157 Menu bar
114 158 ~~~~~~~~
115 The menu bar presents different options that may be used to manipulate the way the Notebook functions.
159 The menu bar presents different options that may be used to manipulate the way
160 the Notebook functions.
116 161
117 162 Toolbar
118 163 ~~~~~~~
119 The tool bar gives a quick way of accessing the most-used operations within the Notebook, by clicking on an icon.
164 The tool bar gives a quick way of accessing the most-used operations within
165 the Notebook, by clicking on an icon.
120 166
121 167
122 168 Input cells
123 169 -----------
124 170 Input cells are at the core of the functionality of the IPython Notebook.
125 They are regions in the document in which you can enter different types of text and commands. To *execute* or *run* the *current cell*, i.e. the cell under the cursor, you can use the :kbd:`Shift-Enter` key combination.
126 This tells the Notebook app to perform the relevant operation for each type of cell (see below), and then to display the resulting output.
171 They are regions in the document in which you can enter different types of
172 text and commands. To *execute* or *run* the *current cell*, i.e. the cell
173 under the cursor, you can use the :kbd:`Shift-Enter` key combination.
174 This tells the Notebook app to perform the relevant operation for each type of
175 cell (see below), and then to display the resulting output.
127 176
128 The notebook consists of a sequence of input cells, labelled ``In[n]``, which may be executed in a non-linear way, and outputs ``Out[n]``, where ``n`` is a number which denotes the order in which the cells were executed over the history of the computational process. The contents of all of these cells are accessible as Python variables with the same names, forming a complete record of the history of the computation.
177 The notebook consists of a sequence of input cells, labelled ``In[n]``, which
178 may be executed in a non-linear way, and outputs ``Out[n]``, where ``n`` is a
179 number which denotes the order in which the cells were executed over the
180 history of the computational process. The contents of all of these cells are
181 accessible as Python variables with the same names, forming a complete record
182 of the history of the computation.
129 183
130 184
131 185 Basic workflow
132 186 --------------
133 The normal workflow in a notebook is, then, quite similar to a standard IPython session, with the difference that you can edit cells in-place multiple
187 The normal workflow in a notebook is, then, quite similar to a standard
188 IPython session, with the difference that you can edit cells in-place multiple
134 189 times until you obtain the desired results, rather than having to
135 rerun separate scripts with the ``%run`` magic command. (Magic commands do, however, also work in the notebook; see below).
136
137 Typically, you will work on a computational problem in pieces, organizing related ideas into cells and moving forward once previous parts work correctly. This is much more convenient for interactive exploration than breaking up a computation into scripts that must be executed together, as was previously necessary, especially if parts of them take a long time to run
138
139 The only significant limitation that the Notebook currently has, compared to the Qt console, is that it cannot run any code that expects input from the kernel (such as scripts that call :func:`raw_input`). Very importantly, this means that the ``%debug`` magic does *not* currently work in the notebook!
140
141 This limitation will be overcome in the future, but in the meantime, there is a simple solution for debugging: you can attach a Qt console to your existing notebook kernel, and run ``%debug`` from the Qt console.
142 If your notebook is running on a local computer (i.e. if you are accessing it via your localhost address at ``127.0.0.1``), then you can just type
143 ``%qtconsole`` in the notebook and a Qt console will open up, connected to that same kernel.
144
145 At certain moments, it may be necessary to interrupt a particularly long calculation, or even to kill the entire computational process. This may be achieved by interrupting or restarting the kernel, respectively.
190 rerun separate scripts with the ``%run`` magic command. (Magic commands do,
191 however, also work in the notebook; see below).
192
193 Typically, you will work on a computational problem in pieces, organizing
194 related ideas into cells and moving forward once previous parts work
195 correctly. This is much more convenient for interactive exploration than
196 breaking up a computation into scripts that must be executed together, as was
197 previously necessary, especially if parts of them take a long time to run
198
199 The only significant limitation that the Notebook currently has, compared to
200 the Qt console, is that it cannot run any code that expects input from the
201 kernel (such as scripts that call :func:`raw_input`). Very importantly, this
202 means that the ``%debug`` magic does *not* currently work in the notebook!
203
204 This limitation will be overcome in the future, but in the meantime, there is
205 a simple solution for debugging: you can attach a Qt console to your existing
206 notebook kernel, and run ``%debug`` from the Qt console.
207 If your notebook is running on a local computer (i.e. if you are accessing it
208 via your localhost address at ``127.0.0.1``), then you can just type
209 ``%qtconsole`` in the notebook and a Qt console will open up, connected to
210 that same kernel.
211
212 At certain moments, it may be necessary to interrupt a particularly long
213 calculation, or even to kill the entire computational process. This may be
214 achieved by interrupting or restarting the kernel, respectively.
146 215 After a kernel restart, it as if the notebook were loaded afresh.
147 216
148
149 A notebook may be downloaded in either ``.ipynb`` or raw ``.py`` form from the menu option ``File | Download as``.
150 Choosing the ``.py`` option downloads a Python ``.py`` script, in which all output has been removed and the content of Markdown cells
151 in comment areas. See ref:`below <notebook_format>` for more details on the
152 notebook format.
217 A notebook may be downloaded in either ``.ipynb`` or raw ``.py`` form from the
218 menu option ``File | Download as``. Choosing the ``.py`` option downloads a
219 Python ``.py`` script, in which all output has been removed and the content of
220 Markdown cells in comment areas. See ref:`below <notebook_format>` for more
221 details on the notebook format.
153 222
154 223
155 224 .. warning::
156 225
157 226 While in simple cases you can "roundtrip" a notebook to Python, edit the
158 Python file, and then import it back without loss of main content, this is in general *not guaranteed to work*. First, there is extra metadata
227 Python file, and then import it back without loss of main content, this is
228 in general *not guaranteed to work*. First, there is extra metadata
159 229 saved in the notebook that may not be saved to the ``.py`` format. And as
160 230 the notebook format evolves in complexity, there will be attributes of the
161 231 notebook that will not survive a roundtrip through the Python form. You
@@ -167,28 +237,43 b' notebook format.'
167 237
168 238 Keyboard shortcuts
169 239 ------------------
170 All actions in the notebook can be achieved with the mouse, but
171 keyboard shortcuts are also available for the most common ones, so that productive use of the notebook can be achieved with minimal mouse usage. The main shortcuts to remember are the following:
240 All actions in the notebook can be achieved with the mouse, but keyboard
241 shortcuts are also available for the most common ones, so that productive use
242 of the notebook can be achieved with minimal mouse usage. The main shortcuts
243 to remember are the following:
172 244
173 245 * :kbd:`Shift-Enter`:
174 Execute the current cell, show output (if any), and jump to the next cell below. If :kbd:`Shift-Enter` is invoked on the last input cell, a new code cell will also be created. Note that in the notebook, typing :kbd:`Enter` on its own *never* forces execution, but rather just inserts a new line in the current input cell. In the Notebook it is thus always necessary to use :kbd:`Shift-Enter` to execute the cell (or use the ``Cell | Run`` menu item).
246
247 Execute the current cell, show output (if any), and jump to the next cell
248 below. If :kbd:`Shift-Enter` is invoked on the last input cell, a new code
249 cell will also be created. Note that in the notebook, typing :kbd:`Enter`
250 on its own *never* forces execution, but rather just inserts a new line in
251 the current input cell. In the Notebook it is thus always necessary to use
252 :kbd:`Shift-Enter` to execute the cell (or use the ``Cell | Run`` menu
253 item).
175 254
176 255 * :kbd:`Ctrl-Enter`:
177 Execute the current cell as if it were in "terminal mode", where any output is shown, but the cursor *remains* in the current cell. This is convenient for doing quick experiments in place, or for querying things like filesystem content, without needing to create additional cells that you may not want to be saved in the notebook.
256 Execute the current cell as if it were in "terminal mode", where any
257 output is shown, but the cursor *remains* in the current cell. This is
258 convenient for doing quick experiments in place, or for querying things
259 like filesystem content, without needing to create additional cells that
260 you may not want to be saved in the notebook.
178 261
179 262 * :kbd:`Alt-Enter`:
180 Executes the current cell, shows the output, and inserts a *new* input cell between the current cell and the adjacent cell (if one exists). This is thus a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`.
263 Executes the current cell, shows the output, and inserts a *new* input
264 cell between the current cell and the adjacent cell (if one exists). This
265 is thus a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`.
181 266 (:kbd:`Ctrl-m a` adds a new cell below the current one.)
182 267
183
184
185 268 * :kbd:`Ctrl-m`:
186 269 This is the prefix for *all* other shortcuts, which consist of :kbd:`Ctrl-m` followed by a single letter or character. For example, if you type :kbd:`Ctrl-m h` (that is, the sole letter :kbd:`h` after :kbd:`Ctrl-m`), IPython will show you all the available keyboard shortcuts.
187 270
188 271
189 272 Input cell types
190 273 ----------------
191 Each IPython input cell has a *cell type*, of which there is a restricted number. The type of a cell may be set by using the cell type dropdown on the toolbar, or via the following keyboard shortcuts:
274 Each IPython input cell has a *cell type*, of which there is a restricted
275 number. The type of a cell may be set by using the cell type dropdown on the
276 toolbar, or via the following keyboard shortcuts:
192 277
193 278 * **code**: :kbd:`Ctrl-m y`
194 279 * **markdown**: :kbd:`Ctrl-m m`
@@ -200,50 +285,83 b' Upon initial creation, each input cell is by default a code cell.'
200 285
201 286 Code cells
202 287 ~~~~~~~~~~
203 A *code input cell* allows you to edit code inline within the cell, with full syntax highlighting and autocompletion/introspection. By default, the language associated to a code cell is Python, but other languages, such as ``julia`` and ``R``, can be handled using magic commands (see below).
204
205 When a code cell is executed with :kbd:`Shift-Enter`, the code that it contains is transparently exported and run in that language
206 (with automatic compiling, etc., if necessary). The result that is returned from this computation is then displayed in the notebook space as the cell's
207 *output*. If this output is of a textual nature, then it is placed into a numbered *output cell*.
208 However, many other possible forms of output are also possible, including
209 ``matplotlib`` figures and HTML tables (as used, for example, in the
210 ``pandas`` data analyis package). This is known as IPython's *rich display* capability.
288 A *code input cell* allows you to edit code inline within the cell, with full
289 syntax highlighting and autocompletion/introspection. By default, the language
290 associated to a code cell is Python, but other languages, such as ``julia``
291 and ``R``, can be handled using magic commands (see below).
292
293 When a code cell is executed with :kbd:`Shift-Enter`, the code that it
294 contains is transparently exported and run in that language (with automatic
295 compiling, etc., if necessary). The result that is returned from this
296 computation is then displayed in the notebook space as the cell's
297 *output*. If this output is of a textual nature, then it is placed into a
298 numbered *output cell*. However, many other possible forms of output are also
299 possible, including ``matplotlib`` figures and HTML tables (as used, for
300 example, in the ``pandas`` data analyis package). This is known as IPython's
301 *rich display* capability.
211 302
212 303
213 304 Rich text using Markdown
214 305 ~~~~~~~~~~~~~~~~~~~~~~~~
215 You can document the computational process in a literate way, alternating descriptive text with code, using *rich text*. In IPython this is accomplished by marking up text with the Markdown language. The corresponding cells are called *Markdown input cells*. The Markdown language provides a simple way to perform this text markup, that is, to specify which parts of the text should be emphasized (italics), bold, form lists, etc.
216
217 When a Markdown input cell is executed, the Markdown code is converted into the corresponding formatted rich text. This output then *replaces* the original Markdown input cell, leaving just the visually-significant marked up rich text. Markdown allows arbitrary HTML code for formatting.
218
219 Within Markdown cells, you can also include *mathematics* in a straightforward way, using standard LaTeX notation: ``$...$`` for inline mathematics and ``$$...$$`` for displayed mathematics. Mathematics is rendered natively in the browser by MathJax. Standard LaTeX and AMS-math environments, such as
220 ``\begin{equation}...\end{equation}``, and ``\begin{align}...\end{align}`` also work, and new LaTeX macros may be defined using standard LaTeX methods, such as ``\newcommand``, by placing them anywhere in a Markdown cell.
306 You can document the computational process in a literate way, alternating
307 descriptive text with code, using *rich text*. In IPython this is accomplished
308 by marking up text with the Markdown language. The corresponding cells are
309 called *Markdown input cells*. The Markdown language provides a simple way to
310 perform this text markup, that is, to specify which parts of the text should
311 be emphasized (italics), bold, form lists, etc.
312
313
314 When a Markdown input cell is executed, the Markdown code is converted into
315 the corresponding formatted rich text. This output then *replaces* the
316 original Markdown input cell, leaving just the visually-significant marked up
317 rich text. Markdown allows arbitrary HTML code for formatting.
318
319 Within Markdown cells, you can also include *mathematics* in a straightforward
320 way, using standard LaTeX notation: ``$...$`` for inline mathematics and
321 ``$$...$$`` for displayed mathematics. Mathematics is rendered natively in the
322 browser by MathJax. Standard LaTeX and AMS-math environments, such as
323 ``\begin{equation}...\end{equation}``, and ``\begin{align}...\end{align}``
324 also work, and new LaTeX macros may be defined using standard LaTeX methods,
325 such as ``\newcommand``, by placing them anywhere in a Markdown cell.
221 326
222 327 Raw input cells
223 328 ~~~~~~~~~~~~~~~
224 *Raw* input cells provide a place in which you can put additional information which you do not want to evaluated by the Notebook. This can be used, for example, to include extra information that is needed when exporting to a certain format. The output after evaluating a raw cell is just a verbatim copy of the input.
329 *Raw* input cells provide a place in which you can put additional information
330 which you do not want to evaluated by the Notebook. This can be used, for
331 example, to include extra information that is needed when exporting to a
332 certain format. The output after evaluating a raw cell is just a verbatim copy
333 of the input.
225 334
226 335 Heading cells
227 336 ~~~~~~~~~~~~~
228 You can provide a conceptual structure for your computational document as a whole using different levels of headings; there are 6 levels available, from level 1 (main title) down to level 6 (paragraph). These can be used later for constructing tables of contents, etc.
337 You can provide a conceptual structure for your computational document as a
338 whole using different levels of headings; there are 6 levels available, from
339 level 1 (main title) down to level 6 (paragraph). These can be used later for
340 constructing tables of contents, etc.
229 341
230 As with Markdown cells, a heading input cell is replaced by a rich text rendering of the heading when the cell is executed.
342 As with Markdown cells, a heading input cell is replaced by a rich text
343 rendering of the heading when the cell is executed.
231 344
232 345
233 346 Magic commands
234 347 ~~~~~~~~~~~~~~
235 348 Magic commands, or *magics*, are commands for controlling IPython itself.
236 They all begin with ``%`` and are entered into code input cells; the code cells are executed as usual with :kbd:`Shift-Enter`.
349 They all begin with ``%`` and are entered into code input cells; the code
350 cells are executed as usual with :kbd:`Shift-Enter`.
237 351
238 The magic commands call special functions defined by IPython which manipulate the computational state in certain ways.
352 The magic commands call special functions defined by IPython which manipulate
353 the computational state in certain ways.
239 354
240 355 There are two types of magics:
241 356
242 357 **line magics**:
243 These begin with a single ``%`` and take as arguments the rest of the *same line* of the code cell. Any other lines of the code cell are treated as if they were part of a standard code cell.
358 These begin with a single ``%`` and take as arguments the rest of the
359 *same line* of the code cell. Any other lines of the code cell are
360 treated as if they were part of a standard code cell.
244 361
245 362 **cell magics**:
246 These begin with ``%%`` and operate on the *entire* remaining contents of the code cell.
363 These begin with ``%%`` and operate on the *entire* remaining contents of
364 the code cell.
247 365
248 366 Line magics
249 367 ~~~~~~~~~~~
@@ -265,10 +383,12 b' Cell magics'
265 383 ~~~~~~~~~~~
266 384
267 385 * ``%%latex``:
268 Renders the entire contents of the cell in LaTeX, without needing to use explicit LaTeX delimiters.
386 Renders the entire contents of the cell in LaTeX, without needing to use
387 explicit LaTeX delimiters.
269 388
270 389 * ``%%bash``:
271 The code cell is executed by sending it to be executed by ``bash``. The output of the ``bash`` commands is captured and displayed in the notebook.
390 The code cell is executed by sending it to be executed by ``bash``. The
391 output of the ``bash`` commands is captured and displayed in the notebook.
272 392
273 393 * ``%%file filename``:
274 394 Writes the contents of the cell to the file ``filename``.
@@ -278,22 +398,36 b' Cell magics'
278 398 Execute the contents of the cell using the R language.
279 399
280 400
281 Several of the cell magics provide functionality to manipulate the filesystem of a remote server to which you otherwise do not have access.
401 Several of the cell magics provide functionality to manipulate the filesystem
402 of a remote server to which you otherwise do not have access.
282 403
283 404
284 405 Plotting
285 406 --------
286 One major feature of the Notebook is the ability to capture the result of plots as *inline* output, thus displaying the result of running some code right next to the code itself. IPython is designed to work seamlessly with the ``matplotlib`` plotting library to attain this functionality.
407 One major feature of the Notebook is the ability to capture the result of
408 plots as *inline* output, thus displaying the result of running some code
409 right next to the code itself. IPython is designed to work seamlessly with the
410 ``matplotlib`` plotting library to attain this functionality.
287 411
288 412 To set this up, before any plotting is performed you must execute the
289 ``%matplotlib`` magic command. This performs the necessary behind-the-scenes setup for IPython to work correctly hand in hand with ``matplotlib``; it does
290 *not*, however, actually execute any Python ``import`` commands, that is, no names are added to the namespace.
413 ``%matplotlib`` magic command. This performs the necessary behind-the-scenes
414 setup for IPython to work correctly hand in hand with ``matplotlib``; it does
415 *not*, however, actually execute any Python ``import`` commands, that is, no
416 names are added to the namespace.
291 417
292 For more agile *interactive* use of the notebook space, an alternative magic, ``%pylab``, is provided. This does the same work as the ``%matplotlib`` magic, but *in addition* it automatically executes a standard sequence of ``import`` statements required to work with the ``%matplotlib`` library:
418 For more agile *interactive* use of the notebook space, an alternative magic,
419 ``%pylab``, is provided. This does the same work as the ``%matplotlib`` magic,
420 but *in addition* it automatically executes a standard sequence of ``import``
421 statements required to work with the ``%matplotlib`` library:
293 422
294 It will import at the top level `numpy` as `np`, `pyplot` as `plt`, `matplotlib`, `pylab` and `mlab` from `matplotlib`, as well as *all names* from ``numpy`` and ``pylab``. A less invasive, but less interactive, option is ``%pylab --no-import-all``, which does not do these ``import *`` imports.
423 It will import at the top level `numpy` as `np`, `pyplot` as `plt`,
424 `matplotlib`, `pylab` and `mlab` from `matplotlib`, as well as *all names*
425 from ``numpy`` and ``pylab``. A less invasive, but less interactive, option is
426 ``%pylab --no-import-all``, which does not do these ``import *`` imports.
295 427
296 When the default ``%matplotlib`` or ``%pylab`` magics are used, the output of a plotting command is captured in a *separate* window. An alternative is to use::
428 When the default ``%matplotlib`` or ``%pylab`` magics are used, the output of
429 a plotting command is captured in a *separate* window. An alternative is to
430 use::
297 431
298 432 %matplotlib inline
299 433
@@ -301,20 +435,28 b' or::'
301 435
302 436 %pylab inline
303 437
304 These instead capture the output of the plot and display it *inline* within the notebook format, below the input cell that produced it. The resulting plots will then also be stored in the notebook document.
438 These instead capture the output of the plot and display it *inline* within
439 the notebook format, below the input cell that produced it. The resulting
440 plots will then also be stored in the notebook document.
305 441
306 442
307 443 Converting notebooks to other formats
308 444 -------------------------------------
309 Newly added in the 1.0 release of IPython is the ``nbconvert`` tool, which allows you to convert an ``.ipynb`` notebook document file into another static format.
445 Newly added in the 1.0 release of IPython is the ``nbconvert`` tool, which
446 allows you to convert an ``.ipynb`` notebook document file into another static
447 format.
310 448
311 Currently, only a command line tool is provided; in the future, it will also be possible to export from within the Notebook app. The command line syntax is::
449 Currently, only a command line tool is provided; in the future, it will also
450 be possible to export from within the Notebook app. The command line syntax
451 is::
312 452
313 453 $ ipython nbconvert --format=FORMAT notebook.ipynb
314 454
315 This will convert the IPython document file ``notebook.ipynb`` into the output format given by the ``FORMAT`` string.
455 This will convert the IPython document file ``notebook.ipynb`` into the output
456 format given by the ``FORMAT`` string.
316 457
317 The default output format is HTML, for which the `--format`` modifier is not actually required::
458 The default output format is HTML, for which the `--format`` modifier is not
459 actually required::
318 460
319 461 $ ipython nbconvert notebook.ipynb
320 462
@@ -352,20 +494,25 b' The currently supported export formats are the following:'
352 494
353 495 * Python:
354 496
355 Comments out all the non-Python code to produce a standard Python ``.py`` script with just the code content.
497 Comments out all the non-Python code to produce a standard Python ``.py``
498 script with just the code content.
356 499
357 The files output by ``nbconvert` are currently placed in a new subdirectory called ``nbconvert_build``.
500 The files output by ``nbconvert` are currently placed in a new subdirectory
501 called ``nbconvert_build``.
358 502
359 Each of the options for PDF export produces as an intermediate step a root LaTeX ``.tex`` file with the same name as the notebook, as well as individual files for each figure, and ``.text` files with textual output from running code cells; all of these files are located together in the ``nbconvert_build`` subdirectory.
503 Each of the options for PDF export produces as an intermediate step a root
504 LaTeX ``.tex`` file with the same name as the notebook, as well as individual
505 files for each figure, and ``.text` files with textual output from running
506 code cells; all of these files are located together in the ``nbconvert_build``
507 subdirectory.
360 508
361 509 To actually produce the final PDF file, run the following commands::
362 510
363 511 $ cd nbconvert_build
364 512 $ pdflatex notebook
365 513
366 This requires a local installation of LaTeX on your machine;
367 it outputs the PDF file ``notebook.pdf``, also inside the
368 ``nbconvert_build`` subdirectory.
514 This requires a local installation of LaTeX on your machine; it outputs the
515 PDF file ``notebook.pdf``, also inside the ``nbconvert_build`` subdirectory.
369 516
370 517 Alternatively, the output may be piped to standard output, ``stdout``, with::
371 518
@@ -394,11 +541,14 b' To see a list of available options enter::'
394 541 $ ipython notebook --help
395 542
396 543 Defaults for these options can also be set by creating a file named
397 ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile folder is a subfolder of your IPython directory; to find out where it is located, run::
544 ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile
545 folder is a subfolder of your IPython directory; to find out where it is
546 located, run::
398 547
399 548 $ ipython locate
400 549
401 To create a new set of default configuration files, with lots of information on available options, use::
550 To create a new set of default configuration files, with lots of information
551 on available options, use::
402 552
403 553 $ ipython profile create
404 554
@@ -409,11 +559,18 b' To create a new set of default configuration files, with lots of information on '
409 559
410 560 Extracting standard Python files from notebooks
411 561 -----------------------------------------------
412 ``.ipynb`` notebook document files are plain text files which store a representation in JSON format of the contents of a notebook space. As such, it is not a valid ``.py`` Python script, and so can be neither imported with
562 ``.ipynb`` notebook document files are plain text files which store a
563 representation in JSON format of the contents of a notebook space. As such, it
564 is not a valid ``.py`` Python script, and so can be neither imported with
413 565 ``import`` in Python, nor run as a standard Python script.
414 566
415 To extract just the Python code from within a notebook document, one option is to use ``ipython nbconvert``, as described above.
416 An alternative is to pass an argument to the IPython Notebook, from the moment when it is originally started, specifying that whenever it saves an ``.ipynb`` notebook document, it should, at the same time, save the corresponding standard ``.py`` script. To do so, you can execute the following command::
567 To extract just the Python code from within a notebook document, one option is
568 to use ``ipython nbconvert``, as described above.
569
570 An alternative is to pass an argument to the IPython Notebook, from the moment
571 when it is originally started, specifying that whenever it saves an ``.ipynb``
572 notebook document, it should, at the same time, save the corresponding
573 standard ``.py`` script. To do so, you can execute the following command::
417 574
418 575 ipython notebook --script
419 576
@@ -422,9 +579,17 b' or you can set this option permanently in your configuration file with::'
422 579 c = get_config()
423 580 c.NotebookManager.save_script=True
424 581
425 The result is that standard ``.py`` files are also now generated, and so they can be ``%run``, imported from regular IPython sessions or other notebooks, or executed at the command line, as usual. Since the raw code you have typed is exported you must avoid using syntax such as IPython magics and other IPython-specific extensions to the language for the files to be able to be successfully imported.
582 The result is that standard ``.py`` files are also now generated, and so they
583 can be ``%run``, imported from regular IPython sessions or other notebooks, or
584 executed at the command line, as usual. Since the raw code you have typed is
585 exported you must avoid using syntax such as IPython magics and other IPython-
586 specific extensions to the language for the files to be able to be
587 successfully imported.
588
589 In normal Python practice, the standard way to differentiate importable code
590 in a Python script from the "executable" part of a script is to use the
591 following idiom at the start of the executable part of the code::
426 592
427 In normal Python practice, the standard way to differentiate importable code in a Python script from the "executable" part of a script is to use the following idiom at the start of the executable part of the code::
428 593
429 594 if __name__ == '__main__'
430 595
@@ -470,26 +635,32 b' You can then add this to your :file:`ipython_notebook_config.py`, e.g.::'
470 635
471 636 # Password to use for web authentication
472 637 c = get_config()
473 c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
638 c.NotebookApp.password =
639 u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
474 640
475 641 When using a password, it is a good idea to also use SSL, so that your password
476 642 is not sent unencrypted by your browser. You can start the notebook to
477 communicate via a secure protocol mode using a self-signed certificate with the command::
643 communicate via a secure protocol mode using a self-signed certificate with
644 the command::
478 645
479 646 $ ipython notebook --certfile=mycert.pem
480 647
481 648 .. note::
482 649
483 A self-signed certificate can be generated with ``openssl``. For example, the following command will create a certificate valid for 365 days with both the key and certificate data written to the same file::
650 A self-signed certificate can be generated with ``openssl``. For example,
651 the following command will create a certificate valid for 365 days with
652 both the key and certificate data written to the same file::
484 653
485 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
654 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.
655 pem -out mycert.pem
486 656
487 657 Your browser will warn you of a dangerous certificate because it is
488 658 self-signed. If you want to have a fully compliant certificate that will not
489 659 raise warnings, it is possible (but rather involved) to obtain one,
490 660 `as explained in detailed in this tutorial`__.
491 661
492 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars
662 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-
663 secure-sertificate-for-free.ars
493 664
494 665 Keep in mind that when you enable SSL support, you will need to access the
495 666 notebook server over ``https://``, not over plain ``http://``. The startup
@@ -507,9 +678,11 b' how to connect to each kernel, with messages such as the following::'
507 678 [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
508 679
509 680 This long string is the name of a JSON file that contains all the port and
510 validation information necessary to connect to the kernel. You can then, for example, manually start a Qt console connected to the *same* kernel with::
681 validation information necessary to connect to the kernel. You can then, for
682 example, manually start a Qt console connected to the *same* kernel with::
511 683
512 $ ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
684 $ ipython qtconsole --existing
685 kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
513 686
514 687 If you have only a single kernel running, simply typing::
515 688
@@ -527,11 +700,15 b' Running a public notebook server'
527 700 If you want to access your notebook server remotely via a web browser,
528 701 you can do the following.
529 702
530 Start by creating a certificate file and a hashed password, as explained above. Then create a custom profile for the notebook, with the following command line, type::
703 Start by creating a certificate file and a hashed password, as explained
704 above. Then create a custom profile for the notebook, with the following
705 command line, type::
531 706
532 707 $ ipython profile create nbserver
533 708
534 In the profile directory just created, edit the file ``ipython_notebook_config.py``. By default, the file has all fields commented; the minimum set you need to uncomment and edit is the following::
709 In the profile directory just created, edit the file
710 ``ipython_notebook_config.py``. By default, the file has all fields
711 commented; the minimum set you need to uncomment and edit is the following::
535 712
536 713 c = get_config()
537 714
@@ -554,7 +731,8 b' Running with a different URL prefix'
554 731
555 732 The notebook dashboard (the landing page with an overview
556 733 of the notebooks in your working directory) typically lives at the URL
557 ``http://localhost:8888/``. If you prefer that it lives, together with the rest of the notebook, under a sub-directory,
734 ``http://localhost:8888/``. If you prefer that it lives, together with the
735 rest of the notebook, under a sub-directory,
558 736 e.g. ``http://localhost:8888/ipython/``, you can do so with
559 737 configuration options like the following (see above for instructions about
560 738 modifying ``ipython_notebook_config.py``)::
@@ -566,20 +744,26 b' modifying ``ipython_notebook_config.py``)::'
566 744 Using a different notebook store
567 745 --------------------------------
568 746
569 By default, the Notebook app stores the notebook documents that it saves as files in the working directory of the Notebook app, also known as the ``notebook_dir``. This logic is implemented in the :class:`FileNotebookManager` class. However, the server can be configured to use a different notebook manager class, which can
747 By default, the Notebook app stores the notebook documents that it saves as
748 files in the working directory of the Notebook app, also known as the
749 ``notebook_dir``. This logic is implemented in the
750 :class:`FileNotebookManager` class. However, the server can be configured to
751 use a different notebook manager class, which can
570 752 store the notebooks in a different format.
571 753
572 Currently, we ship a :class:`AzureNotebookManager` class that stores notebooks in Azure blob storage. This can be used by adding the following lines to your
754 Currently, we ship a :class:`AzureNotebookManager` class that stores notebooks
755 in Azure blob storage. This can be used by adding the following lines to your
573 756 ``ipython_notebook_config.py`` file::
574 757
575 c.NotebookApp.notebook_manager_class = 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager'
758 c.NotebookApp.notebook_manager_class =
759 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager'
576 760 c.AzureNotebookManager.account_name = u'paste_your_account_name_here'
577 761 c.AzureNotebookManager.account_key = u'paste_your_account_key_here'
578 762 c.AzureNotebookManager.container = u'notebooks'
579 763
580 In addition to providing your Azure Blob Storage account name and key, you will
581 have to provide a container name; you can use multiple containers to organize
582 your notebooks.
764 In addition to providing your Azure Blob Storage account name and key, you
765 will have to provide a container name; you can use multiple containers to
766 organize your notebooks.
583 767
584 768 .. _notebook_format:
585 769
@@ -591,11 +775,19 b' across lines to make them reasonably friendly to use in version-control'
591 775 workflows. You should be very careful if you ever manually edit this JSON
592 776 data, as it is extremely easy to corrupt its internal structure and make the
593 777 file impossible to load. In general, you should consider the notebook as a
594 file meant only to be edited by the IPython Notebook app itself, not for hand-editing.
778 file meant only to be edited by the IPython Notebook app itself, not for
779 hand-editing.
595 780
596 781 .. note::
597 782
598 Binary data such as figures are also saved directly in the JSON file. This provides convenient single-file portability, but means that the files can be large; a ``diff`` of binary data is also not very meaningful. Since the binary blobs are encoded in a single line, they affect only one line of the ``diff`` output, but they are typically very long lines. You can use the ``Cell | All Output | Clear`` menu option to remove all output from a notebook prior to committing it to version control, if this is a concern.
783 Binary data such as figures are also saved directly in the JSON file.
784 This provides convenient single-file portability, but means that the
785 files can be large; a ``diff`` of binary data is also not very
786 meaningful. Since the binary blobs are encoded in a single line, they
787 affect only one line of the ``diff`` output, but they are typically very
788 long lines. You can use the ``Cell | All Output | Clear`` menu option to
789 remove all output from a notebook prior to committing it to version
790 control, if this is a concern.
599 791
600 792 The notebook server can also generate a pure Python version of your notebook,
601 793 using the ``File | Download as`` menu option. The resulting ``.py`` file will
@@ -604,9 +796,13 b' prepended with a comment marker. The separation between code and Markdown'
604 796 cells is indicated with special comments and there is a header indicating the
605 797 format version. All output is removed when exporting to Python.
606 798
607 As an example, consider a simple notebook called ``simple.ipynb`` which contains one Markdown cell, with the content ``The simplest notebook.``, one code input cell with the content ``print "Hello, IPython!"``, and the corresponding output.
799 As an example, consider a simple notebook called ``simple.ipynb`` which
800 contains one Markdown cell, with the content ``The simplest notebook.``, one
801 code input cell with the content ``print "Hello, IPython!"``, and the
802 corresponding output.
608 803
609 The contents of the notebook document ``simple.ipynb`` is the following JSON container::
804 The contents of the notebook document ``simple.ipynb`` is the following JSON
805 container::
610 806
611 807 {
612 808 "metadata": {
@@ -657,7 +853,8 b' The corresponding Python script is::'
657 853
658 854 print "Hello, IPython"
659 855
660 Note that indeed the output of the code cell, which is present in the JSON container, has been removed in the ``.py`` script.
856 Note that indeed the output of the code cell, which is present in the JSON
857 container, has been removed in the ``.py`` script.
661 858
662 859
663 860 Known issues
General Comments 0
You need to be logged in to leave comments. Login now