##// END OF EJS Templates
add keyboard shortcut to docs, closes #2960
Paul Ivanov -
Show More
@@ -1,542 +1,581 b''
1 .. _htmlnotebook:
1 .. _htmlnotebook:
2
2
3 The IPython Notebook
3 The IPython Notebook
4 ====================
4 ====================
5
5
6 The IPython Notebook is part of the IPython package, which aims to provide a
6 The IPython Notebook is part of the IPython package, which aims to provide a
7 powerful, interactive approach to scientific computation.
7 powerful, interactive approach to scientific computation.
8 The IPython Notebook extends the previous text-console-based approach, and the
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
9 later Qt console, in a qualitatively new diretion, providing a web-based
10 application suitable for capturing the whole scientific computation process.
10 application suitable for capturing the whole scientific computation process.
11
11
12 .. seealso::
12 .. seealso::
13
13
14 :ref:`Installation requirements <installnotebook>` for the Notebook.
14 :ref:`Installation requirements <installnotebook>` for the Notebook.
15
15
16
16
17 .. Basic structure
17 .. Basic structure
18 .. ---------------
18 .. ---------------
19
19
20 Introduction
20 Introduction
21 ------------
21 ------------
22
22
23 The IPython Notebook combines two components:
23 The IPython Notebook combines two components:
24
24
25 * **The IPython Notebook web application**:
25 * **The IPython Notebook web application**:
26
26
27 The *IPython Notebook web app* is a browser-based tool for interactive
27 The *IPython Notebook web app* is a browser-based tool for interactive
28 authoring of literate computations, in which explanatory text,
28 authoring of literate computations, in which explanatory text,
29 mathematics, computations and rich media output may be combined. Input
29 mathematics, computations and rich media output may be combined. Input
30 and output are stored in persistent cells that may be edited in-place.
30 and output are stored in persistent cells that may be edited in-place.
31
31
32 * **Notebook documents**:
32 * **Notebook documents**:
33
33
34 *Notebook documents*, or *notebooks*, are plain text documents which
34 *Notebook documents*, or *notebooks*, are plain text documents which
35 record all inputs and outputs of the computations, interspersed with
35 record all inputs and outputs of the computations, interspersed with
36 text, mathematics and HTML 5 representations of objects, in a literate
36 text, mathematics and HTML 5 representations of objects, in a literate
37 style.
37 style.
38
38
39 Since the similarity in names can lead to some confusion, in this
39 Since the similarity in names can lead to some confusion, in this
40 documentation we will use capitalization of the word "notebook" to
40 documentation we will use capitalization of the word "notebook" to
41 distinguish the Notebook app and notebook documents, thinking of the
41 distinguish the Notebook app and notebook documents, thinking of the
42 Notebook app as being a proper noun. We will also always refer to the
42 Notebook app as being a proper noun. We will also always refer to the
43 "Notebook app" when we are referring to the browser-based interface,
43 "Notebook app" when we are referring to the browser-based interface,
44 and usually to "notebook documents", instead of "notebooks", for added
44 and usually to "notebook documents", instead of "notebooks", for added
45 precision.
45 precision.
46
46
47 We refer to the current state of the computational process taking place in the
47 We refer to the current state of the computational process taking place in the
48 Notebook app, i.e. the (numbered) sequence of input and output cells, as the
48 Notebook app, i.e. the (numbered) sequence of input and output cells, as the
49 *notebook space*. Notebook documents provide an *exact*, *one-to-one* record
49 *notebook space*. Notebook documents provide an *exact*, *one-to-one* record
50 of all the content in the notebook space, as a plain text file in JSON format.
50 of all the content in the notebook space, as a plain text file in JSON format.
51 The Notebook app automatically saves, at certain intervals, the contents of
51 The Notebook app automatically saves, at certain intervals, the contents of
52 the notebook space to a notebook document stored on disk, with the same name
52 the notebook space to a notebook document stored on disk, with the same name
53 as the title of the notebook space, and the file extension ``.ipynb``. For
53 as the title of the notebook space, and the file extension ``.ipynb``. For
54 this reason, there is no confusion about using the same word "notebook" for
54 this reason, there is no confusion about using the same word "notebook" for
55 both the notebook space and the corresponding notebook document, since they are
55 both the notebook space and the corresponding notebook document, since they are
56 really one and the same concept (we could say that they are "isomorphic").
56 really one and the same concept (we could say that they are "isomorphic").
57
57
58
58
59 Main features of the IPython Notebook web app
59 Main features of the IPython Notebook web app
60 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61
61
62 The main features of the IPython Notebook app include:
62 The main features of the IPython Notebook app include:
63
63
64 * In-browser editing for code, with automatic syntax highlighting and
64 * In-browser editing for code, with automatic syntax highlighting and
65 indentation and tab completion/introspection.
65 indentation and tab completion/introspection.
66
66
67 * Literate combination of code with rich text using the Markdown_ markup
67 * Literate combination of code with rich text using the Markdown_ markup
68 language.
68 language.
69
69
70 * Mathematics is easily included within the Markdown using LaTeX notation, and
70 * Mathematics is easily included within the Markdown using LaTeX notation, and
71 rendered natively by MathJax_.
71 rendered natively by MathJax_.
72
72
73 * Displays rich data representations (e.g. HTML / LaTeX / SVG) as the result
73 * Displays rich data representations (e.g. HTML / LaTeX / SVG) as the result
74 of computations.
74 of computations.
75
75
76 * Publication-quality figures in a range of formats (SVG / PNG), rendered by
76 * Publication-quality figures in a range of formats (SVG / PNG), rendered by
77 the matplotlib_ library, may be included inline and exported.
77 the matplotlib_ library, may be included inline and exported.
78
78
79
79
80 .. _MathJax: http://www.mathjax.org/
80 .. _MathJax: http://www.mathjax.org/
81 .. _matplotlib: http://matplotlib.org/
81 .. _matplotlib: http://matplotlib.org/
82 .. _Markdown: http://daringfireball.net/projects/markdown/syntax
82 .. _Markdown: http://daringfireball.net/projects/markdown/syntax
83
83
84
84
85 Notebook documents
85 Notebook documents
86 ~~~~~~~~~~~~~~~~~~
86 ~~~~~~~~~~~~~~~~~~
87
87
88 Notebook document files are simple JSON_ files with the
88 Notebook document files are simple JSON_ files with the
89 extension ``.ipynb``.
89 extension ``.ipynb``.
90 Since JSON is just plain text, they can be easily version-controlled and shared with colleagues.
90 Since JSON is just plain text, they can be easily version-controlled and shared with colleagues.
91 The notebook stores a *complete*, *reproducible*, *one-to-one* copy of the state of the
91 The notebook stores a *complete*, *reproducible*, *one-to-one* copy of the state of the
92 computational state as it is inside the Notebook app. All computations
92 computational state as it is inside the Notebook app. All computations
93 carried out, and the corresponding results obtained, can be combined in
93 carried out, and the corresponding results obtained, can be combined in
94 a literate way, interleaving executable code with rich text, mathematics,
94 a literate way, interleaving executable code with rich text, mathematics,
95 and rich representations of objects.
95 and rich representations of objects.
96
96
97 .. _JSON: http://en.wikipedia.org/wiki/JSON
97 .. _JSON: http://en.wikipedia.org/wiki/JSON
98
98
99 Notebooks may easily be exported to a range of static formats, including
99 Notebooks may easily be exported to a range of static formats, including
100 HTML (for example, for blog posts), PDF and slide shows,
100 HTML (for example, for blog posts), PDF and slide shows,
101 via the new nbconvert_ command.
101 via the new nbconvert_ command.
102
102
103 Furthermore, any ``.ipynb`` notebook document available from a public
103 Furthermore, any ``.ipynb`` notebook document available from a public
104 URL can be shared via the `IPython Notebook Viewer <nbviewer>`_ service.
104 URL can be shared via the `IPython Notebook Viewer <nbviewer>`_ service.
105 This service loads the notebook document from the URL and will
105 This service loads the notebook document from the URL and will
106 render it as a static web page. The results may thus be shared with a
106 render it as a static web page. The results may thus be shared with a
107 colleague, or as a public blog post, without other users needing to install
107 colleague, or as a public blog post, without other users needing to install
108 IPython themselves. NbViewer is simply NbConvert as a simple heroku webservice.
108 IPython themselves. NbViewer is simply NbConvert as a simple heroku webservice.
109
109
110 See the :ref:`installation documentation <install_index>` for directions on
110 See the :ref:`installation documentation <install_index>` for directions on
111 how to install the notebook and its dependencies.
111 how to install the notebook and its dependencies.
112
112
113 .. _nbviewer: http://nbviewer.ipython.org
113 .. _nbviewer: http://nbviewer.ipython.org
114
114
115 .. note::
115 .. note::
116
116
117 You can start more than one notebook server at the same time, if you want
117 You can start more than one notebook server at the same time, if you want
118 to work on notebooks in different directories. By default the first
118 to work on notebooks in different directories. By default the first
119 notebook server starts on port 8888, and later notebook servers search for
119 notebook server starts on port 8888, and later notebook servers search for
120 ports near that one. You can also manually specify the port with the
120 ports near that one. You can also manually specify the port with the
121 ``--port`` option.
121 ``--port`` option.
122
122
123
123
124 Basic workflow in the IPython Notebook web app
124 Basic workflow in the IPython Notebook web app
125 ----------------------------------------------
125 ----------------------------------------------
126
126
127 Starting up
127 Starting up
128 ~~~~~~~~~~~~
128 ~~~~~~~~~~~~
129
129
130 You can start running the Notebook web app using the following command::
130 You can start running the Notebook web app using the following command::
131
131
132 $ ipython notebook
132 $ ipython notebook
133
133
134 (Here, and in the sequel, the initial ``$`` represents the shell prompt,
134 (Here, and in the sequel, the initial ``$`` represents the shell prompt,
135 indicating that the command is to be run from the command line in a shell.)
135 indicating that the command is to be run from the command line in a shell.)
136
136
137 The landing page of the IPython Notebook application, the *dashboard*, shows
137 The landing page of the IPython Notebook application, the *dashboard*, shows
138 the notebooks currently available in the *notebook directory* (By default, the directory
138 the notebooks currently available in the *notebook directory* (By default, the directory
139 from which the notebook was started).
139 from which the notebook was started).
140 You can create new notebooks from the dashboard with the ``New Notebook``
140 You can create new notebooks from the dashboard with the ``New Notebook``
141 button, or open existing ones by clicking on their name.
141 button, or open existing ones by clicking on their name.
142 You can also drag and drop ``.ipynb`` notebooks and standard ``.py`` Python
142 You can also drag and drop ``.ipynb`` notebooks and standard ``.py`` Python
143 source code files into the notebook list area.
143 source code files into the notebook list area.
144
144
145
145
146 You can open an existing notebook directly, without having to go via the
146 You can open an existing notebook directly, without having to go via the
147 dashboard, with:
147 dashboard, with:
148
148
149 ipython notebook my_notebook
149 ipython notebook my_notebook
150
150
151 The `.ipynb` extension is assumed if no extension is given.
151 The `.ipynb` extension is assumed if no extension is given.
152
152
153 The `File | Open...` menu option will open the dashboard in a new browser tab,
153 The `File | Open...` menu option will open the dashboard in a new browser tab,
154 to allow you to select a current notebook
154 to allow you to select a current notebook
155 from the notebook directory or to create a new notebook.
155 from the notebook directory or to create a new notebook.
156
156
157
157
158
158
159 Notebook user interface
159 Notebook user interface
160 ~~~~~~~~~~~~~~~~~~~~~~~
160 ~~~~~~~~~~~~~~~~~~~~~~~
161
161
162 When you open a new notebook document in the Notebook, you will be presented
162 When you open a new notebook document in the Notebook, you will be presented
163 with the title associated to the notebook space/document, a *menu bar*, a
163 with the title associated to the notebook space/document, a *menu bar*, a
164 *toolbar* and an empty *input cell*.
164 *toolbar* and an empty *input cell*.
165
165
166 Notebook title
166 Notebook title
167 ^^^^^^^^^^^^^^
167 ^^^^^^^^^^^^^^
168 The title of the notebook document that is currently being edited is displayed
168 The title of the notebook document that is currently being edited is displayed
169 at the top of the page, next to the ``IP[y]: Notebook`` logo. This title may
169 at the top of the page, next to the ``IP[y]: Notebook`` logo. This title may
170 be edited directly by clicking on it. The title is reflected in the name of
170 be edited directly by clicking on it. The title is reflected in the name of
171 the ``.ipynb`` notebook document file that is saved.
171 the ``.ipynb`` notebook document file that is saved.
172
172
173 Menu bar
173 Menu bar
174 ^^^^^^^^
174 ^^^^^^^^
175 The menu bar presents different options that may be used to manipulate the way
175 The menu bar presents different options that may be used to manipulate the way
176 the Notebook functions.
176 the Notebook functions.
177
177
178 Toolbar
178 Toolbar
179 ^^^^^^^
179 ^^^^^^^
180 The tool bar gives a quick way of accessing the most-used operations within
180 The tool bar gives a quick way of accessing the most-used operations within
181 the Notebook, by clicking on an icon.
181 the Notebook, by clicking on an icon.
182
182
183
183
184 Creating a new notebook document
184 Creating a new notebook document
185 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
185 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
186
186
187 A new notebook space/document may be created at any time, either from the
187 A new notebook space/document may be created at any time, either from the
188 dashboard, or using the `File | New` menu option from within an active
188 dashboard, or using the `File | New` menu option from within an active
189 notebook. The new notebook is created within the same directory and
189 notebook. The new notebook is created within the same directory and
190 will open in a new browser tab. It will also be reflected as a new entry in
190 will open in a new browser tab. It will also be reflected as a new entry in
191 the notebook list on the dashboard.
191 the notebook list on the dashboard.
192
192
193
193
194 Structure of a notebook document
194 Structure of a notebook document
195 --------------------------------
195 --------------------------------
196
196
197 Input cells
197 Input cells
198 ~~~~~~~~~~~
198 ~~~~~~~~~~~
199 Input cells are at the core of the functionality of the IPython Notebook.
199 Input cells are at the core of the functionality of the IPython Notebook.
200 They are regions in the document in which you can enter different types of
200 They are regions in the document in which you can enter different types of
201 text and commands. To *execute* or *run* the *current cell*, i.e. the cell
201 text and commands. To *execute* or *run* the *current cell*, i.e. the cell
202 under the cursor, you can use the :kbd:`Shift-Enter` key combination.
202 under the cursor, you can use the :kbd:`Shift-Enter` key combination.
203 This tells the Notebook app to perform the relevant operation for each type of
203 This tells the Notebook app to perform the relevant operation for each type of
204 cell (see below), and then to display the resulting output.
204 cell (see below), and then to display the resulting output.
205
205
206 The notebook consists of a sequence of input cells, labelled ``In[n]``, which
206 The notebook consists of a sequence of input cells, labelled ``In[n]``, which
207 may be executed in a non-linear way, and outputs ``Out[n]``, where ``n`` is a
207 may be executed in a non-linear way, and outputs ``Out[n]``, where ``n`` is a
208 number which denotes the order in which the cells were executed over the
208 number which denotes the order in which the cells were executed over the
209 history of the computational process. The contents of all of these cells are
209 history of the computational process. The contents of all of these cells are
210 accessible as Python variables with the same names, forming a complete record
210 accessible as Python variables with the same names, forming a complete record
211 of the history of the computation.
211 of the history of the computation.
212
212
213
213
214
214
215 Input cell types
215 Input cell types
216 ~~~~~~~~~~~~~~~~
216 ~~~~~~~~~~~~~~~~
217 Each IPython input cell has a *cell type*, of which there is a restricted
217 Each IPython input cell has a *cell type*, of which there is a restricted
218 number. The type of a cell may be set by using the cell type dropdown on the
218 number. The type of a cell may be set by using the cell type dropdown on the
219 toolbar, or via the following keyboard shortcuts:
219 toolbar, or via the following keyboard shortcuts:
220
220
221 * **code**: :kbd:`Ctrl-m y`
221 * **code**: :kbd:`Ctrl-m y`
222 * **markdown**: :kbd:`Ctrl-m m`
222 * **markdown**: :kbd:`Ctrl-m m`
223 * **raw**: :kbd:`Ctrl-m t`
223 * **raw**: :kbd:`Ctrl-m t`
224 * **heading**: :kbd:`Ctrl-m 1` - :kbd:`Ctrl-m 6`
224 * **heading**: :kbd:`Ctrl-m 1` - :kbd:`Ctrl-m 6`
225
225
226 Upon initial creation, each input cell is by default a code cell.
226 Upon initial creation, each input cell is by default a code cell.
227
227
228
228
229 Code cells
229 Code cells
230 ^^^^^^^^^^
230 ^^^^^^^^^^
231 A *code input cell* allows you to edit code inline within the cell, with full
231 A *code input cell* allows you to edit code inline within the cell, with full
232 syntax highlighting and autocompletion/introspection. By default, the language
232 syntax highlighting and autocompletion/introspection. By default, the language
233 associated to a code cell is Python, but other languages, such as ``julia``
233 associated to a code cell is Python, but other languages, such as ``julia``
234 and ``R``, can be handled using magic commands (see below).
234 and ``R``, can be handled using magic commands (see below).
235
235
236 When a code cell is executed with :kbd:`Shift-Enter`, the code that it
236 When a code cell is executed with :kbd:`Shift-Enter`, the code that it
237 contains is transparently exported and run in that language (with automatic
237 contains is transparently exported and run in that language (with automatic
238 compiling, etc., if necessary). The result that is returned from this
238 compiling, etc., if necessary). The result that is returned from this
239 computation is then displayed in the notebook space as the cell's
239 computation is then displayed in the notebook space as the cell's
240 *output*. If this output is of a textual nature, it is placed into a
240 *output*. If this output is of a textual nature, it is placed into a
241 numbered *output cell*. However, many other possible forms of output are also
241 numbered *output cell*. However, many other possible forms of output are also
242 possible, including ``matplotlib`` figures and HTML tables (as used, for
242 possible, including ``matplotlib`` figures and HTML tables (as used, for
243 example, in the ``pandas`` data analyis package). This is known as IPython's
243 example, in the ``pandas`` data analyis package). This is known as IPython's
244 *rich display* capability.
244 *rich display* capability.
245
245
246
246
247 Markdown cells
247 Markdown cells
248 ^^^^^^^^^^^^^^
248 ^^^^^^^^^^^^^^
249 You can document the computational process in a literate way, alternating
249 You can document the computational process in a literate way, alternating
250 descriptive text with code, using *rich text*. In IPython this is accomplished
250 descriptive text with code, using *rich text*. In IPython this is accomplished
251 by marking up text with the Markdown language. The corresponding cells are
251 by marking up text with the Markdown language. The corresponding cells are
252 called *Markdown input cells*. The Markdown language provides a simple way to
252 called *Markdown input cells*. The Markdown language provides a simple way to
253 perform this text markup, that is, to specify which parts of the text should
253 perform this text markup, that is, to specify which parts of the text should
254 be emphasized (italics), bold, form lists, etc.
254 be emphasized (italics), bold, form lists, etc.
255
255
256
256
257 When a Markdown input cell is executed, the Markdown code is converted into
257 When a Markdown input cell is executed, the Markdown code is converted into
258 the corresponding formatted rich text. This output then *replaces* the
258 the corresponding formatted rich text. This output then *replaces* the
259 original Markdown input cell, leaving just the visually-significant marked up
259 original Markdown input cell, leaving just the visually-significant marked up
260 rich text. Markdown allows arbitrary HTML code for formatting.
260 rich text. Markdown allows arbitrary HTML code for formatting.
261
261
262 Within Markdown cells, you can also include *mathematics* in a straightforward
262 Within Markdown cells, you can also include *mathematics* in a straightforward
263 way, using standard LaTeX notation: ``$...$`` for inline mathematics and
263 way, using standard LaTeX notation: ``$...$`` for inline mathematics and
264 ``$$...$$`` for displayed mathematics. When the Markdown cell is executed,
264 ``$$...$$`` for displayed mathematics. When the Markdown cell is executed,
265 the LaTeX portions are automatically rendered in the HTML output as equations
265 the LaTeX portions are automatically rendered in the HTML output as equations
266 with high quality typography. This is made possible by MathJax_, which
266 with high quality typography. This is made possible by MathJax_, which
267 supports a `large subset <mathjax_tex>`_ of LaTeX functionality
267 supports a `large subset <mathjax_tex>`_ of LaTeX functionality
268
268
269 .. _mathjax_tex: http://docs.mathjax.org/en/latest/tex.html
269 .. _mathjax_tex: http://docs.mathjax.org/en/latest/tex.html
270
270
271 Standard mathematics environments defined by LaTeX and AMS-LaTeX (the
271 Standard mathematics environments defined by LaTeX and AMS-LaTeX (the
272 `amsmath` package) also work, such as
272 `amsmath` package) also work, such as
273 ``\begin{equation}...\end{equation}``, and ``\begin{align}...\end{align}``.
273 ``\begin{equation}...\end{equation}``, and ``\begin{align}...\end{align}``.
274 New LaTeX macros may be defined using standard methods,
274 New LaTeX macros may be defined using standard methods,
275 such as ``\newcommand``, by placing them anywhere *between math delimiters* in
275 such as ``\newcommand``, by placing them anywhere *between math delimiters* in
276 a Markdown cell. These definitions are then available throughout the rest of
276 a Markdown cell. These definitions are then available throughout the rest of
277 the IPython session. (Note, however, that more care must be taken when using
277 the IPython session. (Note, however, that more care must be taken when using
278 nbconvert_ to output to LaTeX).
278 nbconvert_ to output to LaTeX).
279
279
280 Raw input cells
280 Raw input cells
281 ~~~~~~~~~~~~~~~
281 ~~~~~~~~~~~~~~~
282
282
283 *Raw* input cells provide a place in which you can write *output* directly.
283 *Raw* input cells provide a place in which you can write *output* directly.
284 Raw cells are not evaluated by the Notebook, and have no output.
284 Raw cells are not evaluated by the Notebook, and have no output.
285 When passed through nbconvert, Raw cells arrive in the destination format unmodified,
285 When passed through nbconvert, Raw cells arrive in the destination format unmodified,
286 allowing you to type full latex into a raw cell, which will only be rendered
286 allowing you to type full latex into a raw cell, which will only be rendered
287 by latex after conversion by nbconvert.
287 by latex after conversion by nbconvert.
288
288
289 Heading cells
289 Heading cells
290 ~~~~~~~~~~~~~
290 ~~~~~~~~~~~~~
291
291
292 You can provide a conceptual structure for your computational document as a
292 You can provide a conceptual structure for your computational document as a
293 whole using different levels of headings; there are 6 levels available, from
293 whole using different levels of headings; there are 6 levels available, from
294 level 1 (top level) down to level 6 (paragraph). These can be used later for
294 level 1 (top level) down to level 6 (paragraph). These can be used later for
295 constructing tables of contents, etc.
295 constructing tables of contents, etc.
296
296
297 As with Markdown cells, a heading input cell is replaced by a rich text
297 As with Markdown cells, a heading input cell is replaced by a rich text
298 rendering of the heading when the cell is executed.
298 rendering of the heading when the cell is executed.
299
299
300
300
301 Basic workflow
301 Basic workflow
302 --------------
302 --------------
303
303
304 The normal workflow in a notebook is, then, quite similar to a standard
304 The normal workflow in a notebook is, then, quite similar to a standard
305 IPython session, with the difference that you can edit cells in-place multiple
305 IPython session, with the difference that you can edit cells in-place multiple
306 times until you obtain the desired results, rather than having to
306 times until you obtain the desired results, rather than having to
307 rerun separate scripts with the ``%run`` magic command. (Magic commands do,
307 rerun separate scripts with the ``%run`` magic command. (Magic commands do,
308 however, also work in the notebook; see below).
308 however, also work in the notebook; see below).
309
309
310 Typically, you will work on a computational problem in pieces, organizing
310 Typically, you will work on a computational problem in pieces, organizing
311 related ideas into cells and moving forward once previous parts work
311 related ideas into cells and moving forward once previous parts work
312 correctly. This is much more convenient for interactive exploration than
312 correctly. This is much more convenient for interactive exploration than
313 breaking up a computation into scripts that must be executed together, as was
313 breaking up a computation into scripts that must be executed together, as was
314 previously necessary, especially if parts of them take a long time to run
314 previously necessary, especially if parts of them take a long time to run
315
315
316 The only significant limitation that the Notebook currently has, compared to
316 The only significant limitation that the Notebook currently has, compared to
317 the Qt console, is that it cannot run any code that expects input from the
317 the Qt console, is that it cannot run any code that expects input from the
318 kernel (such as scripts that call :func:`raw_input`). Very importantly, this
318 kernel (such as scripts that call :func:`raw_input`). Very importantly, this
319 means that the ``%debug`` magic does *not* currently work in the notebook!
319 means that the ``%debug`` magic does *not* currently work in the notebook!
320
320
321 This limitation will be overcome in the future, but in the meantime, there is
321 This limitation will be overcome in the future, but in the meantime, there is
322 a simple solution for debugging: you can attach a Qt console to your existing
322 a simple solution for debugging: you can attach a Qt console to your existing
323 notebook kernel, and run ``%debug`` from the Qt console.
323 notebook kernel, and run ``%debug`` from the Qt console.
324 If your notebook is running on a local computer (i.e. if you are accessing it
324 If your notebook is running on a local computer (i.e. if you are accessing it
325 via your localhost address at ``127.0.0.1``), then you can just type
325 via your localhost address at ``127.0.0.1``), then you can just type
326 ``%qtconsole`` in the notebook and a Qt console will open up, connected to
326 ``%qtconsole`` in the notebook and a Qt console will open up, connected to
327 that same kernel.
327 that same kernel.
328
328
329 At certain moments, it may be necessary to interrupt a calculation which is
329 At certain moments, it may be necessary to interrupt a calculation which is
330 taking too long to complete. This may be done with the ``Kernel | Interrupt``
330 taking too long to complete. This may be done with the ``Kernel | Interrupt``
331 menu option, or the :kbd:``Ctrl-i`` keyboard shortcut.
331 menu option, or the :kbd:``Ctrl-i`` keyboard shortcut.
332 Similarly, it may be necessary or desirable to restart the whole computational
332 Similarly, it may be necessary or desirable to restart the whole computational
333 process, with the ``Kernel | Restart`` menu option or :kbd:``Ctrl-.``
333 process, with the ``Kernel | Restart`` menu option or :kbd:``Ctrl-.``
334 shortcut. This gives an equivalent state to loading the notebook document
334 shortcut. This gives an equivalent state to loading the notebook document
335 afresh.
335 afresh.
336
336
337
337
338 .. warning::
338 .. warning::
339
339
340 While in simple cases you can "roundtrip" a notebook to Python, edit the
340 While in simple cases you can "roundtrip" a notebook to Python, edit the
341 Python file, and then import it back without loss of main content, this is
341 Python file, and then import it back without loss of main content, this is
342 in general *not guaranteed to work*. First, there is extra metadata
342 in general *not guaranteed to work*. First, there is extra metadata
343 saved in the notebook that may not be saved to the ``.py`` format. And as
343 saved in the notebook that may not be saved to the ``.py`` format. And as
344 the notebook format evolves in complexity, there will be attributes of the
344 the notebook format evolves in complexity, there will be attributes of the
345 notebook that will not survive a roundtrip through the Python form. You
345 notebook that will not survive a roundtrip through the Python form. You
346 should think of the Python format as a way to output a script version of a
346 should think of the Python format as a way to output a script version of a
347 notebook and the import capabilities as a way to load existing code to get
347 notebook and the import capabilities as a way to load existing code to get
348 a notebook started. But the Python version is *not* an alternate notebook
348 a notebook started. But the Python version is *not* an alternate notebook
349 format.
349 format.
350
350
351
351
352 Keyboard shortcuts
352 Keyboard shortcuts
353 ~~~~~~~~~~~~~~~~~~
353 ~~~~~~~~~~~~~~~~~~
354 All actions in the notebook can be achieved with the mouse, but keyboard
354 All actions in the notebook can be performed with the mouse, but keyboard
355 shortcuts are also available for the most common ones, so that productive use
355 shortcuts are also available for the most common ones. The essential shortcuts
356 of the notebook can be achieved with minimal mouse usage. The main shortcuts
357 to remember are the following:
356 to remember are the following:
358
357
359 * :kbd:`Shift-Enter`:
358 * :kbd:`Shift-Enter`: run cell
360
361 Execute the current cell, show output (if any), and jump to the next cell
359 Execute the current cell, show output (if any), and jump to the next cell
362 below. If :kbd:`Shift-Enter` is invoked on the last input cell, a new code
360 below. If :kbd:`Shift-Enter` is invoked on the last input cell, a new code
363 cell will also be created. Note that in the notebook, typing :kbd:`Enter`
361 cell will also be created. Note that in the notebook, typing :kbd:`Enter`
364 on its own *never* forces execution, but rather just inserts a new line in
362 on its own *never* forces execution, but rather just inserts a new line in
365 the current input cell. In the Notebook it is thus always necessary to use
363 the current input cell. :kbd:`Shift-Enter` is equivalent to clicking the
366 :kbd:`Shift-Enter` to execute the cell (or use the ``Cell | Run`` menu
364 ``Cell | Run`` menu item.
367 item).
368
365
369 * :kbd:`Ctrl-Enter`:
366 * :kbd:`Ctrl-Enter`: run cell in-place
370 Execute the current cell as if it were in "terminal mode", where any
367 Execute the current cell as if it were in "terminal mode", where any
371 output is shown, but the cursor *remains* in the current cell. This is
368 output is shown, but the cursor *remains* in the current cell. The cell's
372 convenient for doing quick experiments in place, or for querying things
369 entire contents are selected after execution, so you can just start typing
373 like filesystem content, without needing to create additional cells that
370 and only the new input will be in the cell. This is convenient for doing
374 you may not want to be saved in the notebook.
371 quick experiments in place, or for querying things like filesystem
375
372 content, without needing to create additional cells that you may not want
376 * :kbd:`Alt-Enter`:
373 to be saved in the notebook.
374
375 * :kbd:`Alt-Enter`: run cell, insert below
377 Executes the current cell, shows the output, and inserts a *new* input
376 Executes the current cell, shows the output, and inserts a *new* input
378 cell between the current cell and the adjacent cell (if one exists). This
377 cell between the current cell and the cell below (if one exists). This
379 is thus a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`.
378 is thus a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`.
380 (:kbd:`Ctrl-m a` adds a new cell above the current one.)
379 (:kbd:`Ctrl-m a` adds a new cell above the current one.)
381
380
382 * :kbd:`Ctrl-m`:
381 * :kbd:`Ctrl-m`:
383 This is the prefix for *all* other shortcuts, which consist of :kbd:`Ctrl-m`
382 This is the prefix for *all* other shortcuts, which consist of :kbd:`Ctrl-m`
384 followed by a single letter or character. For example, if you type
383 followed by a single letter or character. For example, if you type
385 :kbd:`Ctrl-m h` (that is, the sole letter :kbd:`h` after :kbd:`Ctrl-m`),
384 :kbd:`Ctrl-m h` (that is, the sole letter :kbd:`h` after :kbd:`Ctrl-m`),
386 IPython will show you all the available keyboard shortcuts.
385 IPython will show you all the available keyboard shortcuts.
386
387
388 ..
389 TODO: these live in IPython/html/static/notebook/js/quickhelp.js
390 They were last updated for IPython 1.0 release, so update them again for
391 future releases.
392
393 Here is the complete set of keyboard shortcuts available:
394
395 ============ ==========================
396 **Shortcut** **Action**
397 ------------ --------------------------
398 Shift-Enter run cell
399 Ctrl-Enter run cell in-place
400 Alt-Enter run cell, insert below
401 Ctrl-m x cut cell
402 Ctrl-m c copy cell
403 Ctrl-m v paste cell
404 Ctrl-m d delete cell
405 Ctrl-m z undo last cell deletion
406 Ctrl-m - split cell
407 Ctrl-m a insert cell above
408 Ctrl-m b insert cell below
409 Ctrl-m o toggle output
410 Ctrl-m O toggle output scroll
411 Ctrl-m l toggle line numbers
412 Ctrl-m s save notebook
413 Ctrl-m j move cell down
414 Ctrl-m k move cell up
415 Ctrl-m y code cell
416 Ctrl-m m markdown cell
417 Ctrl-m t raw cell
418 Ctrl-m 1-6 heading 1-6 cell
419 Ctrl-m p select previous
420 Ctrl-m n select next
421 Ctrl-m i interrupt kernel
422 Ctrl-m . restart kernel
423 Ctrl-m h show keyboard shortcuts
424 ============ ==========================
425
387
426
388
427
389 Magic commands
428 Magic commands
390 --------------
429 --------------
391 Magic commands, or *magics*, are commands for controlling IPython itself.
430 Magic commands, or *magics*, are commands for controlling IPython itself.
392 They all begin with ``%`` and are entered into code input cells; the code
431 They all begin with ``%`` and are entered into code input cells; the code
393 cells are executed as usual with :kbd:`Shift-Enter`.
432 cells are executed as usual with :kbd:`Shift-Enter`.
394
433
395 The magic commands call special functions defined by IPython which manipulate
434 The magic commands call special functions defined by IPython which manipulate
396 the computational state in certain ways.
435 the computational state in certain ways.
397
436
398 There are two types of magics:
437 There are two types of magics:
399
438
400 - **line magics**:
439 - **line magics**:
401
440
402 These begin with a single ``%`` and take as arguments the rest of the
441 These begin with a single ``%`` and take as arguments the rest of the
403 *same line* of the code cell. Any other lines of the code cell are
442 *same line* of the code cell. Any other lines of the code cell are
404 treated as if they were part of a standard code cell.
443 treated as if they were part of a standard code cell.
405
444
406 - **cell magics**:
445 - **cell magics**:
407
446
408 These begin with ``%%`` and operate on the *entire* remaining contents
447 These begin with ``%%`` and operate on the *entire* remaining contents
409 of the code cell.
448 of the code cell.
410
449
411 Line magics
450 Line magics
412 ~~~~~~~~~~~
451 ~~~~~~~~~~~
413 Some of the available line magics are the following:
452 Some of the available line magics are the following:
414
453
415 * ``%load filename``:
454 * ``%load filename``:
416
455
417 Loads the contents of the file ``filename`` into a new code cell. This
456 Loads the contents of the file ``filename`` into a new code cell. This
418 can be a URL for a remote file.
457 can be a URL for a remote file.
419
458
420 * ``%timeit code``:
459 * ``%timeit code``:
421
460
422 An easy way to time how long the single line of code ``code`` takes to
461 An easy way to time how long the single line of code ``code`` takes to
423 run
462 run
424
463
425 * ``%config``:
464 * ``%config``:
426
465
427 Configuration of the IPython Notebook
466 Configuration of the IPython Notebook
428
467
429 * ``%lsmagic``:
468 * ``%lsmagic``:
430
469
431 Provides a list of all available magic commands
470 Provides a list of all available magic commands
432
471
433 Cell magics
472 Cell magics
434 ~~~~~~~~~~~
473 ~~~~~~~~~~~
435
474
436 * ``%%latex``:
475 * ``%%latex``:
437
476
438 Renders the entire contents of the cell in LaTeX, without needing to use
477 Renders the entire contents of the cell in LaTeX, without needing to use
439 explicit LaTeX delimiters.
478 explicit LaTeX delimiters.
440
479
441 * ``%%bash``:
480 * ``%%bash``:
442
481
443 The code cell is executed by sending it to be executed by ``bash``. The
482 The code cell is executed by sending it to be executed by ``bash``. The
444 output of the ``bash`` commands is captured and displayed in the
483 output of the ``bash`` commands is captured and displayed in the
445 notebook.
484 notebook.
446
485
447 * ``%%file filename``:
486 * ``%%file filename``:
448
487
449 Writes the contents of the cell to the file ``filename``.
488 Writes the contents of the cell to the file ``filename``.
450 **Caution**: The file is over-written without warning!
489 **Caution**: The file is over-written without warning!
451
490
452 * ``%%R``:
491 * ``%%R``:
453
492
454 Execute the contents of the cell using the R language.
493 Execute the contents of the cell using the R language.
455
494
456 * ``%%timeit``:
495 * ``%%timeit``:
457
496
458 Version of ``%timeit`` which times the entire block of code in the
497 Version of ``%timeit`` which times the entire block of code in the
459 current code cell.
498 current code cell.
460
499
461
500
462
501
463 Several of the cell magics provide functionality to manipulate the filesystem
502 Several of the cell magics provide functionality to manipulate the filesystem
464 of a remote server to which you otherwise do not have access.
503 of a remote server to which you otherwise do not have access.
465
504
466
505
467 Plotting
506 Plotting
468 --------
507 --------
469 One major feature of the Notebook is the ability to interact with
508 One major feature of the Notebook is the ability to interact with
470 plots that are the output of running code cells. IPython is designed to work
509 plots that are the output of running code cells. IPython is designed to work
471 seamlessly with the ``matplotlib`` plotting library to provide this
510 seamlessly with the ``matplotlib`` plotting library to provide this
472 functionality.
511 functionality.
473
512
474 To set this up, before any plotting is performed you must execute the
513 To set this up, before any plotting is performed you must execute the
475 ``%matplotlib`` magic command. This performs the necessary behind-the-scenes
514 ``%matplotlib`` magic command. This performs the necessary behind-the-scenes
476 setup for IPython to work correctly hand in hand with ``matplotlib``; it does
515 setup for IPython to work correctly hand in hand with ``matplotlib``; it does
477 *not*, however, actually execute any Python ``import`` commands, that is, no
516 *not*, however, actually execute any Python ``import`` commands, that is, no
478 names are added to the namespace.
517 names are added to the namespace.
479
518
480 If the ``%matplotlib`` magic is called without an argument, the
519 If the ``%matplotlib`` magic is called without an argument, the
481 output of a plotting command is displayed using the default ``matplotlib``
520 output of a plotting command is displayed using the default ``matplotlib``
482 backend in a separate window. Alternatively, the backend can be explicitly
521 backend in a separate window. Alternatively, the backend can be explicitly
483 requested using, for example::
522 requested using, for example::
484
523
485 %matplotlib gtk
524 %matplotlib gtk
486
525
487 A particularly interesting backend is the ``inline`` backend.
526 A particularly interesting backend is the ``inline`` backend.
488 This is applicable only for the IPython Notebook and the IPython QtConsole.
527 This is applicable only for the IPython Notebook and the IPython QtConsole.
489 It can be invoked as follows::
528 It can be invoked as follows::
490
529
491 %matplotlib inline
530 %matplotlib inline
492
531
493 With this backend, output of plotting commands is displayed *inline* within
532 With this backend, output of plotting commands is displayed *inline* within
494 the notebook format, directly below the input cell that produced it. The
533 the notebook format, directly below the input cell that produced it. The
495 resulting plots will then also be stored in the notebook document. This
534 resulting plots will then also be stored in the notebook document. This
496 provides a key part of the functionality for reproducibility_ that the IPython
535 provides a key part of the functionality for reproducibility_ that the IPython
497 Notebook provides.
536 Notebook provides.
498
537
499 .. _reproducibility: https://en.wikipedia.org/wiki/Reproducibility
538 .. _reproducibility: https://en.wikipedia.org/wiki/Reproducibility
500
539
501
540
502
541
503 Configuring the IPython Notebook
542 Configuring the IPython Notebook
504 --------------------------------
543 --------------------------------
505 The IPython Notebook can be run with a variety of command line arguments.
544 The IPython Notebook can be run with a variety of command line arguments.
506 To see a list of available options enter::
545 To see a list of available options enter::
507
546
508 $ ipython notebook --help
547 $ ipython notebook --help
509
548
510 Defaults for these options can also be set by creating a file named
549 Defaults for these options can also be set by creating a file named
511 ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile
550 ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile
512 folder is a subfolder of your IPython directory; to find out where it is
551 folder is a subfolder of your IPython directory; to find out where it is
513 located, run::
552 located, run::
514
553
515 $ ipython locate
554 $ ipython locate
516
555
517 To create a new set of default configuration files, with lots of information
556 To create a new set of default configuration files, with lots of information
518 on available options, use::
557 on available options, use::
519
558
520 $ ipython profile create
559 $ ipython profile create
521
560
522 .. seealso:
561 .. seealso:
523
562
524 :ref:`config_overview`, in particular :ref:`Profiles`.
563 :ref:`config_overview`, in particular :ref:`Profiles`.
525
564
526
565
527 Importing `.py` files
566 Importing `.py` files
528 ----------------------
567 ----------------------
529
568
530
569
531 ``.py`` files will be imported into the IPython Notebook as a notebook with
570 ``.py`` files will be imported into the IPython Notebook as a notebook with
532 the same basename, but an ``.ipynb`` extension, located in the notebook
571 the same basename, but an ``.ipynb`` extension, located in the notebook
533 directory. The notebook created will have just one cell, which will contain
572 directory. The notebook created will have just one cell, which will contain
534 all the code in the ``.py`` file. You can later manually partition this into
573 all the code in the ``.py`` file. You can later manually partition this into
535 individual cells using the ``Edit | Split Cell`` menu option, or the
574 individual cells using the ``Edit | Split Cell`` menu option, or the
536 :kbd:`Ctrl-m -` keyboard shortcut.
575 :kbd:`Ctrl-m -` keyboard shortcut.
537
576
538 .. Alternatively, prior to importing the ``.py``, you can manually add ``# <
577 .. Alternatively, prior to importing the ``.py``, you can manually add ``# <
539 nbformat>2</nbformat>`` at the start of the file, and then add separators for
578 nbformat>2</nbformat>`` at the start of the file, and then add separators for
540 text and code cells, to get a cleaner import with the file already broken into
579 text and code cells, to get a cleaner import with the file already broken into
541 individual cells.
580 individual cells.
542
581
General Comments 0
You need to be logged in to leave comments. Login now