##// END OF EJS Templates
Fixed up all formatting issues and links found in documentation review. Also minor editing for clarity. Ready to be merged.
David P. Sanders -
Show More
@@ -21,18 +21,18 b' The IPython Notebook combines two components:'
21 21
22 22 * **The IPython Notebook web application**:
23 23
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.
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.
28 28
29 29 * **Notebook documents**:
30 30
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.
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 34
35 Since the similarity in names can lead to some confusion, in the documentation
35 Since the similarity in names can lead to some confusion, in this documentation
36 36 we will use capitalization of the word "notebook" to distinguish the
37 37 *N*otebook app and *n*otebook documents, thinking of the Notebook app as being
38 38 a proper noun. We will also always refer to the "Notebook app" when we are
@@ -56,41 +56,45 b' Main features of the IPython Notebook web app'
56 56
57 57 The main features of the IPython Notebook app include:
58 58
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.
59 * In-browser editing for code, with automatic syntax highlighting and indentation and tab completion/introspection.
60
61 * Literate combination of code with rich text using the Markdown_ markup language.
62
63 * Mathematics is easily included within the Markdown using LaTeX notation, and rendered natively by MathJax_.
64
65 * Displays rich data representations (e.g. HTML / LaTeX / SVG) as the result of computations.
66
67 * Publication-quality figures in a range of formats (SVG / PNG), rendered by the matplotlib_ library, may be included inline and exported.
68
69
70 .. _MathJax: http://www.mathjax.org/
71 .. _matplotlib: http://matplotlib.org/
72 .. _Markdown: http://daringfireball.net/projects/markdown/syntax
69 73
70 74
71 75 Notebook documents
72 76 ------------------
73 77
74 Notebook document files are just standard text files with the extension
75 ``.ipynb``, stored in the working directory on your computer. This file can be easily put under version control and shared with colleagues.
78 Notebook document files are just standard, ASCII-coded text files with the extension ``.ipynb``, stored in the working directory on your computer. Since the contents of the files are just plain text, they can be easily version-controlled and shared with colleagues.
76 79
77 Despite the fact that the notebook documents are plain text files, they use
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.
80 Internally, notebook document files use the JSON_ format, allowing them to
81 store a *complete*, *reproducible*, *one-to-one* copy of the state of the computational state as it is inside the Notebook app.
80 82 All computations carried out, and the corresponding results obtained, can be
81 combined in a literate way, mixing them with descriptive text, mathematics,
82 and HTML 5 representations of objects.
83 combined in a literate way, interleaving executable code with rich text, mathematics, and HTML 5 representations of objects.
84
85 .. _JSON: http://en.wikipedia.org/wiki/JSON
83 86
84 87 Notebooks may easily be exported to a range of static formats, including
85 HTML (for example, for blog posts), PDF and slide shows.
86 Furthermore, any publicly available notebook may be shared via the
87 `IPython Notebook Viewer <http://nbviewer.ipython.org>`_ service, which will
88 provide it as a static web page. The results may thus be shared without having
89 to install anything.
88 HTML (for example, for blog posts), PDF and slide shows, via the newly-included `nbconvert script`_ functionality.
89
90 Furthermore, any ``.ipynb`` notebook document with a publicly-available URL can be shared via the `IPython Notebook Viewer`_ service. This service loads the notebook document from the URL which will
91 provide it as a static web page. The results may thus be shared with a colleague, or as a public blog post, without other users needing to install IPython themselves.
90 92
91 93 See the :ref:`installation documentation <install_index>` for directions on
92 94 how to install the notebook and its dependencies.
93 95
96 .. _`Ipython Notebook Viewer`: http://nbviewer.ipython.org
97
94 98 .. note::
95 99
96 100 You can start more than one notebook server at the same time, if you want to
@@ -107,6 +111,8 b' You can start running the Notebook web app using the following command::'
107 111
108 112 $ ipython notebook
109 113
114 (Here, and in the sequel, the initial ``$`` represents the shell prompt, indicating that the command is to be run from the command line in a shell.)
115
110 116 The landing page of the notebook server application, the *dashboard*, shows
111 117 the notebooks currently available in the *working directory* (the directory
112 118 from which the notebook was started).
@@ -128,7 +134,7 b' text and code cells, to get a cleaner import with the file already broken into'
128 134 individual cells.
129 135
130 136 When you open or create a new notebook, your browser tab will reflect the name
131 of that notebook, prefixed with "IPy".
137 of that notebook, prefixed by the "IPy" icon denoting that the tab corresponds to the IPython Notebook.
132 138 The URL is currently not meant to be human-readable and is not persistent
133 139 across invocations of the notebook server; however, this will change in a
134 140 future version of IPython.
@@ -164,6 +170,11 b' Toolbar'
164 170 The tool bar gives a quick way of accessing the most-used operations within
165 171 the Notebook, by clicking on an icon.
166 172
173 Creating a new notebook document
174 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
175
176 A new notebook space/document may be created at any time, either from the dashboard, or using the `File | New` menu option from within an active notebook. The new notebook is created within the same working directory and will open in a new browser tab. It will also be reflected as a new entry in the notebook list on the dashboard.
177
167 178
168 179 Input cells
169 180 -----------
@@ -209,16 +220,12 b' via your localhost address at ``127.0.0.1``), then you can just type'
209 220 ``%qtconsole`` in the notebook and a Qt console will open up, connected to
210 221 that same kernel.
211 222
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.
215 After a kernel restart, it as if the notebook were loaded afresh.
216
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.
223 At certain moments, it may be necessary to interrupt a calculation which is
224 taking too long to complete. This may be done with the ``Kernel | Interrupt``
225 menu option, or the :kbd:``Ctrl-i`` keyboard shortcut.
226 Similarly, it may be necessary or desirable to restart the whole computational
227 process, with the ``Kernel | Restart`` menu option or :kbd:``Ctrl-.`` shortcut.
228 This gives an equivalent state to loading the notebook document afresh.
222 229
223 230
224 231 .. warning::
@@ -263,7 +270,7 b' to remember are the following:'
263 270 Executes the current cell, shows the output, and inserts a *new* input
264 271 cell between the current cell and the adjacent cell (if one exists). This
265 272 is thus a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`.
266 (:kbd:`Ctrl-m a` adds a new cell below the current one.)
273 (:kbd:`Ctrl-m a` adds a new cell above the current one.)
267 274
268 275 * :kbd:`Ctrl-m`:
269 276 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.
@@ -294,7 +301,7 b' When a code cell is executed with :kbd:`Shift-Enter`, the code that it'
294 301 contains is transparently exported and run in that language (with automatic
295 302 compiling, etc., if necessary). The result that is returned from this
296 303 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
304 *output*. If this output is of a textual nature, it is placed into a
298 305 numbered *output cell*. However, many other possible forms of output are also
299 306 possible, including ``matplotlib`` figures and HTML tables (as used, for
300 307 example, in the ``pandas`` data analyis package). This is known as IPython's
@@ -318,11 +325,14 b' rich text. Markdown allows arbitrary HTML code for formatting.'
318 325
319 326 Within Markdown cells, you can also include *mathematics* in a straightforward
320 327 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.
328 ``$$...$$`` for displayed mathematics. When the Markdown cell is executed, the LaTeX portions are automatically rendered in the HTML output as equations with high quality typography. This is made possible by MathJax_, which supports a `large subset`_ of LaTeX functionality
329
330 .. _`large subset`: http://docs.mathjax.org/en/latest/tex.html
331
332 Standard mathematics environments defined by LaTeX and AMS-LaTeX (the `amsmath` package) also work, such as
333 ``\begin{equation}...\end{equation}``, and ``\begin{align}...\end{align}``.
334 New LaTeX macros may be defined using standard methods,
335 such as ``\newcommand``, by placing them anywhere *between math delimiters* in a Markdown cell. These definitions are then available throughout the rest of the IPython session. (Note, however, that more care must be taken when using the `nbconvert script`_ to output to LaTeX).
326 336
327 337 Raw input cells
328 338 ~~~~~~~~~~~~~~~
@@ -336,7 +346,7 b' Heading cells'
336 346 ~~~~~~~~~~~~~
337 347 You can provide a conceptual structure for your computational document as a
338 348 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
349 level 1 (top level) down to level 6 (paragraph). These can be used later for
340 350 constructing tables of contents, etc.
341 351
342 352 As with Markdown cells, a heading input cell is replaced by a rich text
@@ -354,48 +364,64 b' the computational state in certain ways.'
354 364
355 365 There are two types of magics:
356 366
357 **line magics**:
367 - **line magics**:
368
358 369 These begin with a single ``%`` and take as arguments the rest of the
359 370 *same line* of the code cell. Any other lines of the code cell are
360 371 treated as if they were part of a standard code cell.
361 372
362 **cell magics**:
363 These begin with ``%%`` and operate on the *entire* remaining contents of
364 the code cell.
373 - **cell magics**:
374
375 These begin with ``%%`` and operate on the *entire* remaining contents of
376 the code cell.
365 377
366 378 Line magics
367 379 ~~~~~~~~~~~
368 380 Some of the available line magics are the following:
369 381
370 382 * ``%load filename``:
371 Loads the contents of the file ``filename`` into a new code cell.
372 383
373 * ``%timeit code``:
374 A simple way to time how long the single line of code ``code`` takes to run
384 Loads the contents of the file ``filename`` into a new code cell. This
385 can be a URL for a remote file.
386
387 * ``%timeit code``:
388
389 An easy way to time how long the single line of code ``code`` takes to run
375 390
376 391 * ``%config``:
377 Configuration of the IPython Notebook
392
393 Configuration of the IPython Notebook
378 394
379 395 * ``%lsmagic``:
380 Provides a list of all available magic commands
396
397 Provides a list of all available magic commands
381 398
382 399 Cell magics
383 400 ~~~~~~~~~~~
384 401
385 402 * ``%%latex``:
386 Renders the entire contents of the cell in LaTeX, without needing to use
387 explicit LaTeX delimiters.
403
404 Renders the entire contents of the cell in LaTeX, without needing to use
405 explicit LaTeX delimiters.
388 406
389 407 * ``%%bash``:
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.
408
409 The code cell is executed by sending it to be executed by ``bash``. The
410 output of the ``bash`` commands is captured and displayed in the notebook.
392 411
393 412 * ``%%file filename``:
394 Writes the contents of the cell to the file ``filename``.
395 **Caution**: The file is over-written without warning!
413
414 Writes the contents of the cell to the file ``filename``.
415 **Caution**: The file is over-written without warning!
396 416
397 417 * ``%%R``:
398 Execute the contents of the cell using the R language.
418
419 Execute the contents of the cell using the R language.
420
421 * ``%%timeit``:
422
423 Version of ``%timeit`` which times the entire block of code in the current code cell.
424
399 425
400 426
401 427 Several of the cell magics provide functionality to manipulate the filesystem
@@ -404,10 +430,10 b' of a remote server to which you otherwise do not have access.'
404 430
405 431 Plotting
406 432 --------
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.
433 One major feature of the Notebook is the ability to interact with
434 plots that are the output of running code cells. IPython is designed to work
435 seamlessly with the ``matplotlib`` plotting library to provide this
436 functionality.
411 437
412 438 To set this up, before any plotting is performed you must execute the
413 439 ``%matplotlib`` magic command. This performs the necessary behind-the-scenes
@@ -418,45 +444,58 b' names are added to the namespace.'
418 444 For more agile *interactive* use of the notebook space, an alternative magic,
419 445 ``%pylab``, is provided. This does the same work as the ``%matplotlib`` magic,
420 446 but *in addition* it automatically executes a standard sequence of ``import``
421 statements required to work with the ``%matplotlib`` library:
447 statements required to work with the ``%matplotlib`` library, importing the
448 following names into the namespace:
422 449
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.
450 ``numpy`` as ``np``; ``matplotlib.pyplot`` as ``plt``;
451 ``matplotlib``, ``pylab`` and ``mlab`` from ``matplotlib``; and *all names*
452 from within ``numpy`` and ``pylab``.
427 453
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::
454 However, the use of ``%pylab`` is discouraged, since names coming from
455 different packages may collide. In general, the use of ``from package import
456 *`` is discouraged. A better option is then::
457
458 %pylab --no-import-all
431 459
432 %matplotlib inline
460 which imports the names listed above, but does *not* perform this ``import *``
461 imports.
462
463 If the ``%matplotlib`` or ``%pylab` magics are called without an argument, the
464 output of a plotting command is displayed using the default ``matplotlib``
465 backend in a separate window. Alternatively, the backend can be explicitly
466 requested using, for example::
467
468 %matplotlib gtk
433 469
434 or::
470 A particularly interesting backend is the ``inline`` backend.
471 This is applicable only for the IPython Notebook and the IPython Qtconsole.
472 It can be invoked as follows::
473
474 %matplotlib inline
435 475
436 %pylab inline
476 With this backend, output of plotting commands is displayed *inline* within
477 the notebook format, directly below the input cell that produced it. The resulting plots will then also be stored in the notebook document. This provides a key part of the functionality for reproducibility_ that the IPython Notebook provides.
437 478
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.
479 .. _reproducibility: https://en.wikipedia.org/wiki/Reproducibility
441 480
481 .. _`nbconvert script`:
442 482
443 483 Converting notebooks to other formats
444 484 -------------------------------------
445 485 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.
486 allows you to convert an ``.ipynb`` notebook document file into various static
487 formats.
448 488
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::
489 Currently, ``nbconvert`` is provided as a command line tool, run as a script using IPython. In the future, a direct export capability from within the IPython Notebook web app is planned.
490
491 The command-line syntax to run the ``nbconvert`` script is::
452 492
453 493 $ ipython nbconvert --format=FORMAT notebook.ipynb
454 494
455 495 This will convert the IPython document file ``notebook.ipynb`` into the output
456 496 format given by the ``FORMAT`` string.
457 497
458 The default output format is HTML, for which the `--format`` modifier is not
459 actually required::
498 The default output format is HTML, for which the ``--format`` modifier may be omitted::
460 499
461 500 $ ipython nbconvert notebook.ipynb
462 501
@@ -464,57 +503,59 b' The currently supported export formats are the following:'
464 503
465 504 * HTML:
466 505
467 - ``full_html``:
506 - **full_html**:
468 507 Standard HTML
469 508
470 - ``simple_html``:
509 - **simple_html**:
471 510 Simplified HTML
472 511
473 - ``reveal``:
512 - **reveal**:
474 513 HTML slideshow presentation for use with the ``reveal.js`` package
475 514
476 515 * PDF:
477 516
478 - ``sphinx_howto``:
479 The format for Sphinx HOWTOs; similar to an ``article`` in LaTeX
517 - **sphinx_howto**:
518 The format for Sphinx_ HOWTOs; similar to an ``article`` in LaTeX
480 519
481 - ``sphinx_manual``:
482 The format for Sphinx manuals; similar to a `book`` in LaTeX
520 - **sphinx_manual**:
521 The format for Sphinx_ manuals; similar to a ``book`` in LaTeX
483 522
484 - ``latex``:
523 - **latex**:
485 524 An article formatted completely using LaTeX
486 525
487 526 * Markup:
488 527
489 - ``rst``:
490 reStructuredText markup
528 - **rst**:
529 reStructuredText_ markup
530
531 - **markdown**:
532 Markdown_ markup
491 533
492 - ``markdown``:
493 Markdown markup
534 .. _Sphinx: http://sphinx-doc.org/
535 .. _reStructuredText: http://docutils.sourceforge.net/rst.html
494 536
495 537 * Python:
496 538
497 Comments out all the non-Python code to produce a standard Python ``.py``
498 script with just the code content.
539 Comments out all the non-Python code to produce a ``.py`` Python
540 script with just the code content. Currently the output includes IPython magics, and so can be run with ``ipython``, after changing the extension of the script to ``.ipy``.
499 541
500 The files output by ``nbconvert` are currently placed in a new subdirectory
542 The files output by ``nbconvert`` are all placed in a new subdirectory
501 543 called ``nbconvert_build``.
502 544
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
545 Each of the options for PDF export produces as an intermediate step a LaTeX
546 ``.tex`` file with the same basename as the notebook, as well as individual
505 547 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.
548 code cells.
508 549
509 550 To actually produce the final PDF file, run the following commands::
510 551
511 552 $ cd nbconvert_build
512 553 $ pdflatex notebook
513 554
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.
555 This requires a local installation of LaTeX on your machine.
556 The output is a PDF file ``notebook.pdf``, also placed inside the ``nbconvert_build`` subdirectory.
516 557
517 Alternatively, the output may be piped to standard output, ``stdout``, with::
558 Alternatively, the output may be sent to standard output with::
518 559
519 560 $ ipython nbconvert mynotebook.ipynb --stdout
520 561
@@ -560,31 +601,34 b' on available options, use::'
560 601 Extracting standard Python files from notebooks
561 602 -----------------------------------------------
562 603 ``.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
565 ``import`` in Python, nor run as a standard Python script.
604 representation in JSON format of the contents of a notebook space. As such,
605 they are not valid ``.py`` Python scripts, and so can be neither imported
606 directly with ``import`` in Python, nor run directly as a standard Python
607 script (though both of these are possible with simple workarounds).
566 608
567 To extract just the Python code from within a notebook document, one option is
568 to use ``ipython nbconvert``, as described above.
609
610 To extract the Python code from within a notebook document, the simplest method is to use the ``File | Download as | Python (.py)`` menu item; the resulting ``.py`` script will be downloaded to your browser's default download location.
569 611
570 612 An alternative is to pass an argument to the IPython Notebook, from the moment
571 613 when it is originally started, specifying that whenever it saves an ``.ipynb``
572 614 notebook document, it should, at the same time, save the corresponding
573 standard ``.py`` script. To do so, you can execute the following command::
615 ``.py`` script. To do so, you can execute the following command::
574 616
575 ipython notebook --script
617 $ ipython notebook --script
576 618
577 619 or you can set this option permanently in your configuration file with::
578 620
579 621 c = get_config()
580 622 c.NotebookManager.save_script=True
581 623
582 The result is that standard ``.py`` files are also now generated, and so they
624 The result is that standard ``.py`` files are also now generated, which
583 625 can be ``%run``, imported from regular IPython sessions or other notebooks, or
584 626 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-
627 exported, you must avoid using syntax such as IPython magics and other IPython-
586 628 specific extensions to the language for the files to be able to be
587 successfully imported.
629 successfully imported; or you can change the script's extension to ``.ipy`` and run it with::
630
631 $ ipython script.ipy
588 632
589 633 In normal Python practice, the standard way to differentiate importable code
590 634 in a Python script from the "executable" part of a script is to use the
General Comments 0
You need to be logged in to leave comments. Login now