##// END OF EJS Templates
Reference notebook examples, fixes #4146.
Dražen Lučanin -
Show More
@@ -1,492 +1,495 b''
1 .. _htmlnotebook:
1 .. _htmlnotebook:
2
2
3 The IPython Notebook
3 The IPython Notebook
4 ====================
4 ====================
5
5
6 Introduction
6 Introduction
7 ------------
7 ------------
8
8
9 The notebook extends the console-based approach to interactive computing in
9 The notebook extends the console-based approach to interactive computing in
10 a qualitatively new direction, providing a web-based application suitable for
10 a qualitatively new direction, providing a web-based application suitable for
11 capturing the whole computation process: developing, documenting, and
11 capturing the whole computation process: developing, documenting, and
12 executing code, as well as communicating the results. The IPython notebook
12 executing code, as well as communicating the results. The IPython notebook
13 combines two components:
13 combines two components:
14
14
15 **A web application**: a browser-based tool for interactive authoring of
15 **A web application**: a browser-based tool for interactive authoring of
16 documents which combine explanatory text, mathematics, computations and their
16 documents which combine explanatory text, mathematics, computations and their
17 rich media output.
17 rich media output.
18
18
19 **Notebook documents**: a representation of all content visible in the web
19 **Notebook documents**: a representation of all content visible in the web
20 application, including inputs and outputs of the computations, explanatory
20 application, including inputs and outputs of the computations, explanatory
21 text, mathematics, images, and rich media representations of objects.
21 text, mathematics, images, and rich media representations of objects.
22
22
23 .. seealso::
23 .. seealso::
24
24
25 See the :ref:`installation documentation <installnotebook>` for directions
25 See the :ref:`installation documentation <installnotebook>` for directions
26 on how to install the notebook and its dependencies.
26 on how to install the notebook and its dependencies.
27
27
28
28
29 Main features of the web application
29 Main features of the web application
30 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
30 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31
31
32 * In-browser editing for code, with automatic syntax highlighting,
32 * In-browser editing for code, with automatic syntax highlighting,
33 indentation, and tab completion/introspection.
33 indentation, and tab completion/introspection.
34
34
35 * The ability to execute code from the browser, with the results of
35 * The ability to execute code from the browser, with the results of
36 computations attached to the code which generated them.
36 computations attached to the code which generated them.
37
37
38 * Displaying the result of computation using rich media representations, such
38 * Displaying the result of computation using rich media representations, such
39 as HTML, LaTeX, PNG, SVG, etc. For example, publication-quality figures
39 as HTML, LaTeX, PNG, SVG, etc. For example, publication-quality figures
40 rendered by the matplotlib_ library, can be included inline.
40 rendered by the matplotlib_ library, can be included inline.
41
41
42 * In-browser editing for rich text using the Markdown_ markup language, which
42 * In-browser editing for rich text using the Markdown_ markup language, which
43 can provide commentary for the code, is not limited to plain text.
43 can provide commentary for the code, is not limited to plain text.
44
44
45 * The ability to easily include mathematical notation within markdown cells
45 * The ability to easily include mathematical notation within markdown cells
46 using LaTeX, and rendered natively by MathJax_.
46 using LaTeX, and rendered natively by MathJax_.
47
47
48
48
49
49
50 .. _MathJax: http://www.mathjax.org/
50 .. _MathJax: http://www.mathjax.org/
51
51
52
52
53 Notebook documents
53 Notebook documents
54 ~~~~~~~~~~~~~~~~~~
54 ~~~~~~~~~~~~~~~~~~
55 Notebook documents contains the inputs and outputs of a interactive session as
55 Notebook documents contains the inputs and outputs of a interactive session as
56 well as additional text that accompanies the code but is not meant for
56 well as additional text that accompanies the code but is not meant for
57 execution. In this way, notebook files can serve as a complete computational
57 execution. In this way, notebook files can serve as a complete computational
58 record of a session, interleaving executable code with explanatory text,
58 record of a session, interleaving executable code with explanatory text,
59 mathematics, and rich representations of resulting objects. These documents
59 mathematics, and rich representations of resulting objects. These documents
60 are internally JSON_ files and are saved with the ``.ipynb`` extension. Since
60 are internally JSON_ files and are saved with the ``.ipynb`` extension. Since
61 JSON is a plain text format, they can be version-controlled and shared with
61 JSON is a plain text format, they can be version-controlled and shared with
62 colleagues.
62 colleagues.
63
63
64 .. _JSON: http://en.wikipedia.org/wiki/JSON
64 .. _JSON: http://en.wikipedia.org/wiki/JSON
65
65
66 Notebooks may be exported to a range of static formats, including HTML (for
66 Notebooks may be exported to a range of static formats, including HTML (for
67 example, for blog posts), reStructeredText, LaTeX, PDF, and slide shows, via
67 example, for blog posts), reStructeredText, LaTeX, PDF, and slide shows, via
68 the new :ref:`nbconvert <nbconvert>` command.
68 the new :ref:`nbconvert <nbconvert>` command.
69
69
70 Furthermore, any ``.ipynb`` notebook document available from a public
70 Furthermore, any ``.ipynb`` notebook document available from a public
71 URL can be shared via the `IPython Notebook Viewer <nbviewer>`_ (nbviewer_).
71 URL can be shared via the `IPython Notebook Viewer <nbviewer>`_ (nbviewer_).
72 This service loads the notebook document from the URL and renders it as a
72 This service loads the notebook document from the URL and renders it as a
73 static web page. The results may thus be shared with a colleague, or as a
73 static web page. The results may thus be shared with a colleague, or as a
74 public blog post, without other users needing to install IPython themselves.
74 public blog post, without other users needing to install IPython themselves.
75 In effect, nbviewer_ is simply :ref:`nbconvert <nbconvert>` as a web service,
75 In effect, nbviewer_ is simply :ref:`nbconvert <nbconvert>` as a web service,
76 so you can do your own static conversions with nbconvert, without relying on
76 so you can do your own static conversions with nbconvert, without relying on
77 nbviewer.
77 nbviewer.
78
78
79
79
80
80
81 .. seealso::
81 .. seealso::
82
82
83 :ref:`Details on the notebook JSON file format <notebook_format>`
83 :ref:`Details on the notebook JSON file format <notebook_format>`
84
84
85
85
86 Starting the notebook server
86 Starting the notebook server
87 ----------------------------
87 ----------------------------
88
88
89 You can start running a notebook server from the command line using the
89 You can start running a notebook server from the command line using the
90 following command::
90 following command::
91
91
92 ipython notebook
92 ipython notebook
93
93
94 This will print some information about the notebook server in your console,
94 This will print some information about the notebook server in your console,
95 and open a web browser to the URL of the web application (by default,
95 and open a web browser to the URL of the web application (by default,
96 ``http://127.0.0.1:8888``).
96 ``http://127.0.0.1:8888``).
97
97
98 The landing page of the IPython notebook web application, the **dashboard**,
98 The landing page of the IPython notebook web application, the **dashboard**,
99 shows the notebooks currently available in the notebook directory (by default,
99 shows the notebooks currently available in the notebook directory (by default,
100 the directory from which the notebook server was started).
100 the directory from which the notebook server was started).
101
101
102 You can create new notebooks from the dashboard with the ``New Notebook``
102 You can create new notebooks from the dashboard with the ``New Notebook``
103 button, or open existing ones by clicking on their name. You can also drag
103 button, or open existing ones by clicking on their name. You can also drag
104 and drop ``.ipynb`` notebooks and standard ``.py`` Python source code files
104 and drop ``.ipynb`` notebooks and standard ``.py`` Python source code files
105 into the notebook list area.
105 into the notebook list area.
106
106
107 When starting a notebook server from the command line, you can also open a
107 When starting a notebook server from the command line, you can also open a
108 particular notebook directly, bypassing the dashboard, with ``ipython notebook
108 particular notebook directly, bypassing the dashboard, with ``ipython notebook
109 my_notebook.ipynb``. The ``.ipynb`` extension is assumed if no extension is
109 my_notebook.ipynb``. The ``.ipynb`` extension is assumed if no extension is
110 given.
110 given.
111
111
112 When you are inside an open notebook, the `File | Open...` menu option will
112 When you are inside an open notebook, the `File | Open...` menu option will
113 open the dashboard in a new browser tab, to allow you to open another notebook
113 open the dashboard in a new browser tab, to allow you to open another notebook
114 from the notebook directory or to create a new notebook.
114 from the notebook directory or to create a new notebook.
115
115
116
116
117 .. note::
117 .. note::
118
118
119 You can start more than one notebook server at the same time, if you want
119 You can start more than one notebook server at the same time, if you want
120 to work on notebooks in different directories. By default the first
120 to work on notebooks in different directories. By default the first
121 notebook server starts on port 8888, and later notebook servers search for
121 notebook server starts on port 8888, and later notebook servers search for
122 ports near that one. You can also manually specify the port with the
122 ports near that one. You can also manually specify the port with the
123 ``--port`` option.
123 ``--port`` option.
124
124
125 Creating a new notebook document
125 Creating a new notebook document
126 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127
127
128 A new notebook may be created at any time, either from the dashboard, or using
128 A new notebook may be created at any time, either from the dashboard, or using
129 the `File | New` menu option from within an active notebook. The new notebook
129 the `File | New` menu option from within an active notebook. The new notebook
130 is created within the same directory and will open in a new browser tab. It
130 is created within the same directory and will open in a new browser tab. It
131 will also be reflected as a new entry in the notebook list on the dashboard.
131 will also be reflected as a new entry in the notebook list on the dashboard.
132
132
133
133
134 Opening notebooks
134 Opening notebooks
135 ~~~~~~~~~~~~~~~~~
135 ~~~~~~~~~~~~~~~~~
136 An open notebook has **exactly one** interactive session connected to an
136 An open notebook has **exactly one** interactive session connected to an
137 :ref:`IPython kernel <ipythonzmq>`, which will execute code sent by the user
137 :ref:`IPython kernel <ipythonzmq>`, which will execute code sent by the user
138 and communicate back results. This kernel remains active if the web browser
138 and communicate back results. This kernel remains active if the web browser
139 window is closed, and reopening the same notebook from the dashboard will
139 window is closed, and reopening the same notebook from the dashboard will
140 reconnect the web application to the same kernel. In the dashboard, notebooks
140 reconnect the web application to the same kernel. In the dashboard, notebooks
141 with an active kernel have a ``Shutdown`` button next to them, whereas
141 with an active kernel have a ``Shutdown`` button next to them, whereas
142 notebooks without an active kernel have a ``Delete`` button in its place.
142 notebooks without an active kernel have a ``Delete`` button in its place.
143
143
144 Other clients may connect to the same underlying IPython kernel.
144 Other clients may connect to the same underlying IPython kernel.
145 The notebook server always prints to the terminal the full details of
145 The notebook server always prints to the terminal the full details of
146 how to connect to each kernel, with messages such as the following::
146 how to connect to each kernel, with messages such as the following::
147
147
148 [NotebookApp] Kernel started: 87f7d2c0-13e3-43df-8bb8-1bd37aaf3373
148 [NotebookApp] Kernel started: 87f7d2c0-13e3-43df-8bb8-1bd37aaf3373
149
149
150 This long string is the kernel's ID which is sufficient for getting the
150 This long string is the kernel's ID which is sufficient for getting the
151 information necessary to connect to the kernel. You can also request this
151 information necessary to connect to the kernel. You can also request this
152 connection data by running the ``%connect_info`` :ref:`magic
152 connection data by running the ``%connect_info`` :ref:`magic
153 <magics_explained>`. This will print the same ID information as well as the
153 <magics_explained>`. This will print the same ID information as well as the
154 content of the JSON data structure it contains.
154 content of the JSON data structure it contains.
155
155
156 You can then, for example, manually start a Qt console connected to the *same*
156 You can then, for example, manually start a Qt console connected to the *same*
157 kernel from the command line, by passing a portion of the ID::
157 kernel from the command line, by passing a portion of the ID::
158
158
159 $ ipython qtconsole --existing 87f7d2c0
159 $ ipython qtconsole --existing 87f7d2c0
160
160
161 Without an ID, ``--existing`` will connect to the most recently
161 Without an ID, ``--existing`` will connect to the most recently
162 started kernel. This can also be done by running the ``%qtconsole``
162 started kernel. This can also be done by running the ``%qtconsole``
163 :ref:`magic <magics_explained>` in the notebook.
163 :ref:`magic <magics_explained>` in the notebook.
164
164
165 .. seealso::
165 .. seealso::
166
166
167 :ref:`ipythonzmq`
167 :ref:`ipythonzmq`
168
168
169 Notebook user interface
169 Notebook user interface
170 -----------------------
170 -----------------------
171
171
172 When you create a new notebook document, you will be presented with the
172 When you create a new notebook document, you will be presented with the
173 **notebook name**, a **menu bar**, a **toolbar** and an empty **code
173 **notebook name**, a **menu bar**, a **toolbar** and an empty **code
174 cell**.
174 cell**.
175
175
176 **notebook name**: The name of the notebook document is displayed at the top
176 **notebook name**: The name of the notebook document is displayed at the top
177 of the page, next to the ``IP[y]: Notebook`` logo. This name reflects the name
177 of the page, next to the ``IP[y]: Notebook`` logo. This name reflects the name
178 of the ``.ipynb`` notebook document file. Clicking on the notebook name
178 of the ``.ipynb`` notebook document file. Clicking on the notebook name
179 brings up a dialog which allows you to rename it. Thus, renaming a notebook
179 brings up a dialog which allows you to rename it. Thus, renaming a notebook
180 from "Untitled0" to "My first notebook" in the browser, renames the
180 from "Untitled0" to "My first notebook" in the browser, renames the
181 ``Untitled0.ipynb`` file to ``My first notebook.ipynb``.
181 ``Untitled0.ipynb`` file to ``My first notebook.ipynb``.
182
182
183 **menu bar**: The menu bar presents different options that may be used to
183 **menu bar**: The menu bar presents different options that may be used to
184 manipulate the way the notebook functions.
184 manipulate the way the notebook functions.
185
185
186 **toolbar**: The tool bar gives a quick way of performing the most-used
186 **toolbar**: The tool bar gives a quick way of performing the most-used
187 operations within the notebook, by clicking on an icon.
187 operations within the notebook, by clicking on an icon.
188
188
189 **code cell**: the default type of cell, read on for an explanation of cells
189 **code cell**: the default type of cell, read on for an explanation of cells
190
190
191
191
192 Structure of a notebook document
192 Structure of a notebook document
193 --------------------------------
193 --------------------------------
194
194
195 The notebook consists of a sequence of cells. A cell is a multi-line
195 The notebook consists of a sequence of cells. A cell is a multi-line
196 text input field, and its contents can be executed by using
196 text input field, and its contents can be executed by using
197 :kbd:`Shift-Enter`, or by clicking either the "Play" button the toolbar, or
197 :kbd:`Shift-Enter`, or by clicking either the "Play" button the toolbar, or
198 `Cell | Run` in the menu bar. The execution behavior of a cell is determined
198 `Cell | Run` in the menu bar. The execution behavior of a cell is determined
199 the cell's type. There are four types of cells: **code cells**, **markdown
199 the cell's type. There are four types of cells: **code cells**, **markdown
200 cells**, **raw cells** and **heading cells**. Every cell starts off
200 cells**, **raw cells** and **heading cells**. Every cell starts off
201 being a **code cell**, but its type can be changed by using a dropdown on the
201 being a **code cell**, but its type can be changed by using a dropdown on the
202 toolbar (which will be "Code", initially), or via :ref:`keyboard shortcuts
202 toolbar (which will be "Code", initially), or via :ref:`keyboard shortcuts
203 <keyboard-shortcuts>`.
203 <keyboard-shortcuts>`.
204
204
205 For more information on the different things you can do in a notebook,
206 see the `collection of examples
207 <https://github.com/ipython/ipython/tree/master/examples/notebooks#readme>`_.
205
208
206 Code cells
209 Code cells
207 ~~~~~~~~~~
210 ~~~~~~~~~~
208 A *code cell* allows you to edit and write new code, with full syntax
211 A *code cell* allows you to edit and write new code, with full syntax
209 highlighting and tab completion. By default, the language associated to a code
212 highlighting and tab completion. By default, the language associated to a code
210 cell is Python, but other languages, such as ``Julia`` and ``R``, can be
213 cell is Python, but other languages, such as ``Julia`` and ``R``, can be
211 handled using :ref:`cell magic commands <magics_explained>`.
214 handled using :ref:`cell magic commands <magics_explained>`.
212
215
213 When a code cell is executed, code that it contains is sent to the kernel
216 When a code cell is executed, code that it contains is sent to the kernel
214 associated with the notebook. The results that are returned from this
217 associated with the notebook. The results that are returned from this
215 computation are then displayed in the notebook as the cell's *output*. The
218 computation are then displayed in the notebook as the cell's *output*. The
216 output is not limited to text, with many other possible forms of output are
219 output is not limited to text, with many other possible forms of output are
217 also possible, including ``matplotlib`` figures and HTML tables (as used, for
220 also possible, including ``matplotlib`` figures and HTML tables (as used, for
218 example, in the ``pandas`` data analysis package). This is known as IPython's
221 example, in the ``pandas`` data analysis package). This is known as IPython's
219 *rich display* capability.
222 *rich display* capability.
220
223
221 .. seealso::
224 .. seealso::
222
225
223 `Basic Output`_ example notebook
226 `Basic Output`_ example notebook
224
227
225 `Rich Display System`_ example notebook
228 `Rich Display System`_ example notebook
226
229
227 Markdown cells
230 Markdown cells
228 ~~~~~~~~~~~~~~
231 ~~~~~~~~~~~~~~
229 You can document the computational process in a literate way, alternating
232 You can document the computational process in a literate way, alternating
230 descriptive text with code, using *rich text*. In IPython this is accomplished
233 descriptive text with code, using *rich text*. In IPython this is accomplished
231 by marking up text with the Markdown language. The corresponding cells are
234 by marking up text with the Markdown language. The corresponding cells are
232 called *Markdown cells*. The Markdown language provides a simple way to
235 called *Markdown cells*. The Markdown language provides a simple way to
233 perform this text markup, that is, to specify which parts of the text should
236 perform this text markup, that is, to specify which parts of the text should
234 be emphasized (italics), bold, form lists, etc.
237 be emphasized (italics), bold, form lists, etc.
235
238
236
239
237 When a Markdown cell is executed, the Markdown code is converted into
240 When a Markdown cell is executed, the Markdown code is converted into
238 the corresponding formatted rich text. Markdown allows arbitrary HTML code for
241 the corresponding formatted rich text. Markdown allows arbitrary HTML code for
239 formatting.
242 formatting.
240
243
241 Within Markdown cells, you can also include *mathematics* in a straightforward
244 Within Markdown cells, you can also include *mathematics* in a straightforward
242 way, using standard LaTeX notation: ``$...$`` for inline mathematics and
245 way, using standard LaTeX notation: ``$...$`` for inline mathematics and
243 ``$$...$$`` for displayed mathematics. When the Markdown cell is executed,
246 ``$$...$$`` for displayed mathematics. When the Markdown cell is executed,
244 the LaTeX portions are automatically rendered in the HTML output as equations
247 the LaTeX portions are automatically rendered in the HTML output as equations
245 with high quality typography. This is made possible by MathJax_, which
248 with high quality typography. This is made possible by MathJax_, which
246 supports a `large subset <mathjax_tex>`_ of LaTeX functionality
249 supports a `large subset <mathjax_tex>`_ of LaTeX functionality
247
250
248 .. _mathjax_tex: http://docs.mathjax.org/en/latest/tex.html
251 .. _mathjax_tex: http://docs.mathjax.org/en/latest/tex.html
249
252
250 Standard mathematics environments defined by LaTeX and AMS-LaTeX (the
253 Standard mathematics environments defined by LaTeX and AMS-LaTeX (the
251 `amsmath` package) also work, such as
254 `amsmath` package) also work, such as
252 ``\begin{equation}...\end{equation}``, and ``\begin{align}...\end{align}``.
255 ``\begin{equation}...\end{equation}``, and ``\begin{align}...\end{align}``.
253 New LaTeX macros may be defined using standard methods,
256 New LaTeX macros may be defined using standard methods,
254 such as ``\newcommand``, by placing them anywhere *between math delimiters* in
257 such as ``\newcommand``, by placing them anywhere *between math delimiters* in
255 a Markdown cell. These definitions are then available throughout the rest of
258 a Markdown cell. These definitions are then available throughout the rest of
256 the IPython session.
259 the IPython session.
257
260
258 .. seealso::
261 .. seealso::
259
262
260 `Markdown Cells`_ example notebook
263 `Markdown Cells`_ example notebook
261
264
262 Raw cells
265 Raw cells
263 ~~~~~~~~~
266 ~~~~~~~~~
264
267
265 *Raw* cells provide a place in which you can write *output* directly.
268 *Raw* cells provide a place in which you can write *output* directly.
266 Raw cells are not evaluated by the notebook.
269 Raw cells are not evaluated by the notebook.
267 When passed through :ref:`nbconvert <nbconvert>`, raw cells arrive in the
270 When passed through :ref:`nbconvert <nbconvert>`, raw cells arrive in the
268 destination format unmodified. For example, this allows you to type full LaTeX
271 destination format unmodified. For example, this allows you to type full LaTeX
269 into a raw cell, which will only be rendered by LaTeX after conversion by
272 into a raw cell, which will only be rendered by LaTeX after conversion by
270 nbconvert.
273 nbconvert.
271
274
272 Heading cells
275 Heading cells
273 ~~~~~~~~~~~~~
276 ~~~~~~~~~~~~~
274
277
275 You can provide a conceptual structure for your computational document as a
278 You can provide a conceptual structure for your computational document as a
276 whole using different levels of headings; there are 6 levels available, from
279 whole using different levels of headings; there are 6 levels available, from
277 level 1 (top level) down to level 6 (paragraph). These can be used later for
280 level 1 (top level) down to level 6 (paragraph). These can be used later for
278 constructing tables of contents, etc. As with Markdown cells, a heading
281 constructing tables of contents, etc. As with Markdown cells, a heading
279 cell is replaced by a rich text rendering of the heading when the cell is
282 cell is replaced by a rich text rendering of the heading when the cell is
280 executed.
283 executed.
281
284
282
285
283 Basic workflow
286 Basic workflow
284 --------------
287 --------------
285
288
286 The normal workflow in a notebook is, then, quite similar to a standard
289 The normal workflow in a notebook is, then, quite similar to a standard
287 IPython session, with the difference that you can edit cells in-place multiple
290 IPython session, with the difference that you can edit cells in-place multiple
288 times until you obtain the desired results, rather than having to
291 times until you obtain the desired results, rather than having to
289 rerun separate scripts with the ``%run`` magic command.
292 rerun separate scripts with the ``%run`` magic command.
290
293
291
294
292 Typically, you will work on a computational problem in pieces, organizing
295 Typically, you will work on a computational problem in pieces, organizing
293 related ideas into cells and moving forward once previous parts work
296 related ideas into cells and moving forward once previous parts work
294 correctly. This is much more convenient for interactive exploration than
297 correctly. This is much more convenient for interactive exploration than
295 breaking up a computation into scripts that must be executed together, as was
298 breaking up a computation into scripts that must be executed together, as was
296 previously necessary, especially if parts of them take a long time to run.
299 previously necessary, especially if parts of them take a long time to run.
297
300
298 At certain moments, it may be necessary to interrupt a calculation which is
301 At certain moments, it may be necessary to interrupt a calculation which is
299 taking too long to complete. This may be done with the `Kernel | Interrupt`
302 taking too long to complete. This may be done with the `Kernel | Interrupt`
300 menu option, or the :kbd:`Ctrl-m i` keyboard shortcut.
303 menu option, or the :kbd:`Ctrl-m i` keyboard shortcut.
301 Similarly, it may be necessary or desirable to restart the whole computational
304 Similarly, it may be necessary or desirable to restart the whole computational
302 process, with the `Kernel | Restart` menu option or :kbd:`Ctrl-m .`
305 process, with the `Kernel | Restart` menu option or :kbd:`Ctrl-m .`
303 shortcut.
306 shortcut.
304
307
305 A notebook may be downloaded in either a ``.ipynb`` or ``.py`` file from the
308 A notebook may be downloaded in either a ``.ipynb`` or ``.py`` file from the
306 menu option `File | Download as`. Choosing the ``.py`` option downloads a
309 menu option `File | Download as`. Choosing the ``.py`` option downloads a
307 Python ``.py`` script, in which all rich output has been removed and the
310 Python ``.py`` script, in which all rich output has been removed and the
308 content of markdown cells have been inserted as comments.
311 content of markdown cells have been inserted as comments.
309
312
310 .. seealso::
313 .. seealso::
311
314
312 `Running Code in the IPython Notebook`_ example notebook
315 `Running Code in the IPython Notebook`_ example notebook
313
316
314 `Basic Output`_ example notebook
317 `Basic Output`_ example notebook
315
318
316 :ref:`a warning about doing "roundtrip" conversions <note_about_roundtrip>`.
319 :ref:`a warning about doing "roundtrip" conversions <note_about_roundtrip>`.
317
320
318 .. _keyboard-shortcuts:
321 .. _keyboard-shortcuts:
319
322
320 Keyboard shortcuts
323 Keyboard shortcuts
321 ~~~~~~~~~~~~~~~~~~
324 ~~~~~~~~~~~~~~~~~~
322 All actions in the notebook can be performed with the mouse, but keyboard
325 All actions in the notebook can be performed with the mouse, but keyboard
323 shortcuts are also available for the most common ones. The essential shortcuts
326 shortcuts are also available for the most common ones. The essential shortcuts
324 to remember are the following:
327 to remember are the following:
325
328
326 * :kbd:`Shift-Enter`: run cell
329 * :kbd:`Shift-Enter`: run cell
327 Execute the current cell, show output (if any), and jump to the next cell
330 Execute the current cell, show output (if any), and jump to the next cell
328 below. If :kbd:`Shift-Enter` is invoked on the last cell, a new code
331 below. If :kbd:`Shift-Enter` is invoked on the last cell, a new code
329 cell will also be created. Note that in the notebook, typing :kbd:`Enter`
332 cell will also be created. Note that in the notebook, typing :kbd:`Enter`
330 on its own *never* forces execution, but rather just inserts a new line in
333 on its own *never* forces execution, but rather just inserts a new line in
331 the current cell. :kbd:`Shift-Enter` is equivalent to clicking the
334 the current cell. :kbd:`Shift-Enter` is equivalent to clicking the
332 ``Cell | Run`` menu item.
335 ``Cell | Run`` menu item.
333
336
334 * :kbd:`Ctrl-Enter`: run cell in-place
337 * :kbd:`Ctrl-Enter`: run cell in-place
335 Execute the current cell as if it were in "terminal mode", where any
338 Execute the current cell as if it were in "terminal mode", where any
336 output is shown, but the cursor *remains* in the current cell. The cell's
339 output is shown, but the cursor *remains* in the current cell. The cell's
337 entire contents are selected after execution, so you can just start typing
340 entire contents are selected after execution, so you can just start typing
338 and only the new input will be in the cell. This is convenient for doing
341 and only the new input will be in the cell. This is convenient for doing
339 quick experiments in place, or for querying things like filesystem
342 quick experiments in place, or for querying things like filesystem
340 content, without needing to create additional cells that you may not want
343 content, without needing to create additional cells that you may not want
341 to be saved in the notebook.
344 to be saved in the notebook.
342
345
343 * :kbd:`Alt-Enter`: run cell, insert below
346 * :kbd:`Alt-Enter`: run cell, insert below
344 Executes the current cell, shows the output, and inserts a *new*
347 Executes the current cell, shows the output, and inserts a *new*
345 cell between the current cell and the cell below (if one exists). This
348 cell between the current cell and the cell below (if one exists). This
346 is thus a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`.
349 is thus a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`.
347 (:kbd:`Ctrl-m a` adds a new cell above the current one.)
350 (:kbd:`Ctrl-m a` adds a new cell above the current one.)
348
351
349 * :kbd:`Ctrl-m`:
352 * :kbd:`Ctrl-m`:
350 This is the prefix for *all* other shortcuts, which consist of :kbd:`Ctrl-m`
353 This is the prefix for *all* other shortcuts, which consist of :kbd:`Ctrl-m`
351 followed by a single letter or character. For example, if you type
354 followed by a single letter or character. For example, if you type
352 :kbd:`Ctrl-m h` (that is, the sole letter :kbd:`h` after :kbd:`Ctrl-m`),
355 :kbd:`Ctrl-m h` (that is, the sole letter :kbd:`h` after :kbd:`Ctrl-m`),
353 IPython will show you all the available keyboard shortcuts.
356 IPython will show you all the available keyboard shortcuts.
354
357
355
358
356 ..
359 ..
357 TODO: these live in IPython/html/static/notebook/js/quickhelp.js
360 TODO: these live in IPython/html/static/notebook/js/quickhelp.js
358 They were last updated for IPython 1.0 release, so update them again for
361 They were last updated for IPython 1.0 release, so update them again for
359 future releases.
362 future releases.
360
363
361 Here is the complete set of keyboard shortcuts available:
364 Here is the complete set of keyboard shortcuts available:
362
365
363 ============ ==========================
366 ============ ==========================
364 **Shortcut** **Action**
367 **Shortcut** **Action**
365 ------------ --------------------------
368 ------------ --------------------------
366 Shift-Enter run cell
369 Shift-Enter run cell
367 Ctrl-Enter run cell in-place
370 Ctrl-Enter run cell in-place
368 Alt-Enter run cell, insert below
371 Alt-Enter run cell, insert below
369 Ctrl-m x cut cell
372 Ctrl-m x cut cell
370 Ctrl-m c copy cell
373 Ctrl-m c copy cell
371 Ctrl-m v paste cell
374 Ctrl-m v paste cell
372 Ctrl-m d delete cell
375 Ctrl-m d delete cell
373 Ctrl-m z undo last cell deletion
376 Ctrl-m z undo last cell deletion
374 Ctrl-m - split cell
377 Ctrl-m - split cell
375 Ctrl-m a insert cell above
378 Ctrl-m a insert cell above
376 Ctrl-m b insert cell below
379 Ctrl-m b insert cell below
377 Ctrl-m o toggle output
380 Ctrl-m o toggle output
378 Ctrl-m O toggle output scroll
381 Ctrl-m O toggle output scroll
379 Ctrl-m l toggle line numbers
382 Ctrl-m l toggle line numbers
380 Ctrl-m s save notebook
383 Ctrl-m s save notebook
381 Ctrl-m j move cell down
384 Ctrl-m j move cell down
382 Ctrl-m k move cell up
385 Ctrl-m k move cell up
383 Ctrl-m y code cell
386 Ctrl-m y code cell
384 Ctrl-m m markdown cell
387 Ctrl-m m markdown cell
385 Ctrl-m t raw cell
388 Ctrl-m t raw cell
386 Ctrl-m 1-6 heading 1-6 cell
389 Ctrl-m 1-6 heading 1-6 cell
387 Ctrl-m p select previous
390 Ctrl-m p select previous
388 Ctrl-m n select next
391 Ctrl-m n select next
389 Ctrl-m i interrupt kernel
392 Ctrl-m i interrupt kernel
390 Ctrl-m . restart kernel
393 Ctrl-m . restart kernel
391 Ctrl-m h show keyboard shortcuts
394 Ctrl-m h show keyboard shortcuts
392 ============ ==========================
395 ============ ==========================
393
396
394
397
395
398
396
399
397
400
398 Plotting
401 Plotting
399 --------
402 --------
400 One major feature of the notebook is the ability to display plots that are the
403 One major feature of the notebook is the ability to display plots that are the
401 output of running code cells. IPython is designed to work seamlessly with the
404 output of running code cells. IPython is designed to work seamlessly with the
402 matplotlib_ plotting library to provide this functionality.
405 matplotlib_ plotting library to provide this functionality.
403
406
404 To set this up, before any plotting is performed you must execute the
407 To set this up, before any plotting is performed you must execute the
405 ``%matplotlib`` :ref:`magic command <magics_explained>`. This performs the
408 ``%matplotlib`` :ref:`magic command <magics_explained>`. This performs the
406 necessary behind-the-scenes setup for IPython to work correctly hand in hand
409 necessary behind-the-scenes setup for IPython to work correctly hand in hand
407 with ``matplotlib``; it does *not*, however, actually execute any Python
410 with ``matplotlib``; it does *not*, however, actually execute any Python
408 ``import`` commands, that is, no names are added to the namespace.
411 ``import`` commands, that is, no names are added to the namespace.
409
412
410 If the ``%matplotlib`` magic is called without an argument, the
413 If the ``%matplotlib`` magic is called without an argument, the
411 output of a plotting command is displayed using the default ``matplotlib``
414 output of a plotting command is displayed using the default ``matplotlib``
412 backend in a separate window. Alternatively, the backend can be explicitly
415 backend in a separate window. Alternatively, the backend can be explicitly
413 requested using, for example::
416 requested using, for example::
414
417
415 %matplotlib gtk
418 %matplotlib gtk
416
419
417 A particularly interesting backend, provided by IPython, is the ``inline``
420 A particularly interesting backend, provided by IPython, is the ``inline``
418 backend. This is available only for the IPython Notebook and the
421 backend. This is available only for the IPython Notebook and the
419 :ref:`IPython QtConsole <qtconsole>`. It can be invoked as follows::
422 :ref:`IPython QtConsole <qtconsole>`. It can be invoked as follows::
420
423
421 %matplotlib inline
424 %matplotlib inline
422
425
423 With this backend, the output of plotting commands is displayed *inline*
426 With this backend, the output of plotting commands is displayed *inline*
424 within the notebook, directly below the code cell that produced it. The
427 within the notebook, directly below the code cell that produced it. The
425 resulting plots will then also be stored in the notebook document.
428 resulting plots will then also be stored in the notebook document.
426
429
427 .. seealso::
430 .. seealso::
428
431
429 `Plotting with Matplotlib`_ example notebook
432 `Plotting with Matplotlib`_ example notebook
430
433
431
434
432 Configuring the IPython Notebook
435 Configuring the IPython Notebook
433 --------------------------------
436 --------------------------------
434 The notebook server can be run with a variety of command line arguments.
437 The notebook server can be run with a variety of command line arguments.
435 To see a list of available options enter::
438 To see a list of available options enter::
436
439
437 $ ipython notebook --help
440 $ ipython notebook --help
438
441
439 Defaults for these options can also be set by creating a file named
442 Defaults for these options can also be set by creating a file named
440 ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile
443 ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile
441 folder is a subfolder of your IPython directory; to find out where it is
444 folder is a subfolder of your IPython directory; to find out where it is
442 located, run::
445 located, run::
443
446
444 $ ipython locate
447 $ ipython locate
445
448
446 To create a new set of default configuration files, with lots of information
449 To create a new set of default configuration files, with lots of information
447 on available options, use::
450 on available options, use::
448
451
449 $ ipython profile create
452 $ ipython profile create
450
453
451 .. seealso::
454 .. seealso::
452
455
453 :ref:`config_overview`, in particular :ref:`Profiles`.
456 :ref:`config_overview`, in particular :ref:`Profiles`.
454
457
455 :ref:`notebook_security`
458 :ref:`notebook_security`
456
459
457 :ref:`notebook_public_server`
460 :ref:`notebook_public_server`
458
461
459
462
460 Importing ``.py`` files
463 Importing ``.py`` files
461 -----------------------
464 -----------------------
462
465
463 ``.py`` files will be imported as a notebook with
466 ``.py`` files will be imported as a notebook with
464 the same basename, but an ``.ipynb`` extension, located in the notebook
467 the same basename, but an ``.ipynb`` extension, located in the notebook
465 directory. The notebook created will have just one cell, which will contain
468 directory. The notebook created will have just one cell, which will contain
466 all the code in the ``.py`` file. You can later manually partition this into
469 all the code in the ``.py`` file. You can later manually partition this into
467 individual cells using the ``Edit | Split Cell`` menu option, or the
470 individual cells using the ``Edit | Split Cell`` menu option, or the
468 :kbd:`Ctrl-m -` keyboard shortcut.
471 :kbd:`Ctrl-m -` keyboard shortcut.
469
472
470 Note that ``.py`` scripts obtained from a notebook document using nbconvert_
473 Note that ``.py`` scripts obtained from a notebook document using nbconvert_
471 maintain the structure of the notebook in comments. Reimporting such a
474 maintain the structure of the notebook in comments. Reimporting such a
472 script back into a notebook will preserve this structure.
475 script back into a notebook will preserve this structure.
473
476
474 .. _note_about_roundtrip:
477 .. _note_about_roundtrip:
475
478
476 .. warning::
479 .. warning::
477
480
478 While in simple cases you can "roundtrip" a notebook to Python, edit the
481 While in simple cases you can "roundtrip" a notebook to Python, edit the
479 Python file, and then import it back without loss of main content, this is
482 Python file, and then import it back without loss of main content, this is
480 in general *not guaranteed to work*. First, there is extra metadata
483 in general *not guaranteed to work*. First, there is extra metadata
481 saved in the notebook that may not be saved to the ``.py`` format. And as
484 saved in the notebook that may not be saved to the ``.py`` format. And as
482 the notebook format evolves in complexity, there will be attributes of the
485 the notebook format evolves in complexity, there will be attributes of the
483 notebook that will not survive a roundtrip through the Python form. You
486 notebook that will not survive a roundtrip through the Python form. You
484 should think of the Python format as a way to output a script version of a
487 should think of the Python format as a way to output a script version of a
485 notebook and the import capabilities as a way to load existing code to get
488 notebook and the import capabilities as a way to load existing code to get
486 a notebook started. But the Python version is *not* an alternate notebook
489 a notebook started. But the Python version is *not* an alternate notebook
487 format.
490 format.
488
491
489 .. seealso::
492 .. seealso::
490 :ref:`notebook_format`
493 :ref:`notebook_format`
491
494
492 .. include:: ../links.txt
495 .. include:: ../links.txt
General Comments 0
You need to be logged in to leave comments. Login now