##// END OF EJS Templates
notebook docs touchup...
David P. Sanders -
Show More
@@ -1,212 +1,212 b''
1 .. _nbconvert:
1 .. _nbconvert:
2
2
3 Converting notebooks to other formats
3 Converting notebooks to other formats
4 =====================================
4 =====================================
5
5
6 Newly added in the 1.0 release of IPython is the ``nbconvert`` tool, which
6 Newly added in the 1.0 release of IPython is the ``nbconvert`` tool, which
7 allows you to convert an ``.ipynb`` notebook document file into various static
7 allows you to convert an ``.ipynb`` notebook document file into various static
8 formats.
8 formats.
9
9
10 Currently, ``nbconvert`` is provided as a command line tool, run as a script
10 Currently, ``nbconvert`` is provided as a command line tool, run as a script
11 using IPython. A direct export capability from within the
11 using IPython. A direct export capability from within the
12 IPython Notebook web app is planned.
12 IPython Notebook web app is planned.
13
13
14 The command-line syntax to run the ``nbconvert`` script is::
14 The command-line syntax to run the ``nbconvert`` script is::
15
15
16 $ ipython nbconvert --to FORMAT notebook.ipynb
16 $ ipython nbconvert --to FORMAT notebook.ipynb
17
17
18 This will convert the IPython document file ``notebook.ipynb`` into the output
18 This will convert the IPython document file ``notebook.ipynb`` into the output
19 format given by the ``FORMAT`` string.
19 format given by the ``FORMAT`` string.
20
20
21 The default output format is html, for which the ``--to`` argument may be
21 The default output format is html, for which the ``--to`` argument may be
22 omitted::
22 omitted::
23
23
24 $ ipython nbconvert notebook.ipynb
24 $ ipython nbconvert notebook.ipynb
25
25
26 IPython provides a few templates for some output formats, and these can be
26 IPython provides a few templates for some output formats, and these can be
27 specified via an additional ``--template`` argument.
27 specified via an additional ``--template`` argument.
28
28
29 The currently supported export formats are:
29 The currently supported export formats are:
30
30
31 * ``--to html``
31 * ``--to html``
32
32
33 - ``--template full`` (default)
33 - ``--template full`` (default)
34
34
35 A full static HTML render of the notebook.
35 A full static HTML render of the notebook.
36 This looks very similar to the interactive view.
36 This looks very similar to the interactive view.
37
37
38 - ``--template basic``
38 - ``--template basic``
39
39
40 Simplified HTML, useful for embedding in webpages, blogs, etc.
40 Simplified HTML, useful for embedding in webpages, blogs, etc.
41 This excludes HTML headers.
41 This excludes HTML headers.
42
42
43 * ``--to latex``
43 * ``--to latex``
44
44
45 Latex export. This generates ``NOTEBOOK_NAME.tex`` file,
45 Latex export. This generates ``NOTEBOOK_NAME.tex`` file,
46 ready for export. You can automatically run latex on it to generate a PDF
46 ready for export. You can automatically run latex on it to generate a PDF
47 by adding ``--post PDF``.
47 by adding ``--post PDF``.
48
48
49 - ``--template article`` (default)
49 - ``--template article`` (default)
50
50
51 Latex article, derived from Sphinx's howto template.
51 Latex article, derived from Sphinx's howto template.
52
52
53 - ``--template book``
53 - ``--template book``
54
54
55 Latex book, derived from Sphinx's manual template.
55 Latex book, derived from Sphinx's manual template.
56
56
57 - ``--template basic``
57 - ``--template basic``
58
58
59 Very basic latex output - mainly meant as a starting point for custom templates.
59 Very basic latex output - mainly meant as a starting point for custom templates.
60
60
61 * ``--to slides``
61 * ``--to slides``
62
62
63 This generates a Reveal.js HTML slideshow.
63 This generates a Reveal.js HTML slideshow.
64 It must be served by an HTTP server. The easiest way to get this is to add
64 It must be served by an HTTP server. The easiest way to get this is to add
65 ``--post serve`` on the command-line.
65 ``--post serve`` on the command-line.
66
66
67 * ``--to markdown``
67 * ``--to markdown``
68
68
69 Simple markdown output. Markdown cells are unaffected,
69 Simple markdown output. Markdown cells are unaffected,
70 and code cells are placed in triple-backtick (``\`\`\```) blocks.
70 and code cells are placed in triple-backtick (```````) blocks.
71
71
72 * ``--to rst``
72 * ``--to rst``
73
73
74 Basic reStructuredText output. Useful as a starting point for embedding notebooks
74 Basic reStructuredText output. Useful as a starting point for embedding notebooks
75 in Sphinx docs.
75 in Sphinx docs.
76
76
77 * ``--to python``
77 * ``--to python``
78
78
79 Convert a notebook to an executable Python script.
79 Convert a notebook to an executable Python script.
80 This is the simplest way to get a Python script out of a notebook.
80 This is the simplest way to get a Python script out of a notebook.
81 If there were any magics in the notebook, this may only be executable from
81 If there were any magics in the notebook, this may only be executable from
82 an IPython session.
82 an IPython session.
83
83
84 .. note::
84 .. note::
85
85
86 nbconvert uses pandoc_ to convert between various markup languages,
86 nbconvert uses pandoc_ to convert between various markup languages,
87 so pandoc is a dependency of most nbconvert transforms,
87 so pandoc is a dependency of most nbconvert transforms,
88 excluding Markdown and Python.
88 excluding Markdown and Python.
89
89
90 .. _pandoc: http://johnmacfarlane.net/pandoc/
90 .. _pandoc: http://johnmacfarlane.net/pandoc/
91
91
92 The output file created by ``nbconvert`` will have the same base name as
92 The output file created by ``nbconvert`` will have the same base name as
93 the notebook and will be placed in the current working directory. Any
93 the notebook and will be placed in the current working directory. Any
94 supporting files (graphics, etc) will be placed in a new directory with the
94 supporting files (graphics, etc) will be placed in a new directory with the
95 same base name as the notebook, suffixed with ``_files``::
95 same base name as the notebook, suffixed with ``_files``::
96
96
97 $ ipython nbconvert notebook.ipynb
97 $ ipython nbconvert notebook.ipynb
98 $ ls
98 $ ls
99 notebook.ipynb notebook.html notebook_files/
99 notebook.ipynb notebook.html notebook_files/
100
100
101 For simple single-file output, such as html, markdown, etc.,
101 For simple single-file output, such as html, markdown, etc.,
102 the output may be sent to standard output with::
102 the output may be sent to standard output with::
103
103
104 $ ipython nbconvert --to markdown notebook.ipynb --stdout
104 $ ipython nbconvert --to markdown notebook.ipynb --stdout
105
105
106 Multiple notebooks can be specified from the command line::
106 Multiple notebooks can be specified from the command line::
107
107
108 $ ipython nbconvert notebook*.ipynb
108 $ ipython nbconvert notebook*.ipynb
109 $ ipython nbconvert notebook1.ipynb notebook2.ipynb
109 $ ipython nbconvert notebook1.ipynb notebook2.ipynb
110
110
111 or via a list in a configuration file, say ``mycfg.py``, containing the text::
111 or via a list in a configuration file, say ``mycfg.py``, containing the text::
112
112
113 c = get_config()
113 c = get_config()
114 c.NbConvertApp.notebooks = ["notebook1.ipynb", "notebook2.ipynb"]
114 c.NbConvertApp.notebooks = ["notebook1.ipynb", "notebook2.ipynb"]
115
115
116 and using the command::
116 and using the command::
117
117
118 $ ipython nbconvert --config mycfg.py
118 $ ipython nbconvert --config mycfg.py
119
119
120
120
121 .. _notebook_format:
121 .. _notebook_format:
122
122
123 Notebook JSON file format
123 Notebook JSON file format
124 -------------------------
124 -------------------------
125
125
126 Notebook documents are JSON files with an ``.ipynb`` extension, formatted
126 Notebook documents are JSON files with an ``.ipynb`` extension, formatted
127 as legibly as possible with minimal extra indentation and cell content broken
127 as legibly as possible with minimal extra indentation and cell content broken
128 across lines to make them reasonably friendly to use in version-control
128 across lines to make them reasonably friendly to use in version-control
129 workflows. You should be very careful if you ever manually edit this JSON
129 workflows. You should be very careful if you ever manually edit this JSON
130 data, as it is extremely easy to corrupt its internal structure and make the
130 data, as it is extremely easy to corrupt its internal structure and make the
131 file impossible to load. In general, you should consider the notebook as a
131 file impossible to load. In general, you should consider the notebook as a
132 file meant only to be edited by the IPython Notebook app itself, not for
132 file meant only to be edited by the IPython Notebook app itself, not for
133 hand-editing.
133 hand-editing.
134
134
135 .. note::
135 .. note::
136
136
137 Binary data such as figures are also saved directly in the JSON file.
137 Binary data such as figures are also saved directly in the JSON file.
138 This provides convenient single-file portability, but means that the
138 This provides convenient single-file portability, but means that the
139 files can be large; a ``diff`` of binary data is also not very
139 files can be large; a ``diff`` of binary data is also not very
140 meaningful. Since the binary blobs are encoded in a single line, they
140 meaningful. Since the binary blobs are encoded in a single line, they
141 affect only one line of the ``diff`` output, but they are typically very
141 affect only one line of the ``diff`` output, but they are typically very
142 long lines. You can use the ``Cell | All Output | Clear`` menu option to
142 long lines. You can use the ``Cell | All Output | Clear`` menu option to
143 remove all output from a notebook prior to committing it to version
143 remove all output from a notebook prior to committing it to version
144 control, if this is a concern.
144 control, if this is a concern.
145
145
146 The notebook server can also generate a pure Python version of your notebook,
146 The notebook server can also generate a pure Python version of your notebook,
147 using the ``File | Download as`` menu option. The resulting ``.py`` file will
147 using the ``File | Download as`` menu option. The resulting ``.py`` file will
148 contain all the code cells from your notebook verbatim, and all Markdown cells
148 contain all the code cells from your notebook verbatim, and all Markdown cells
149 prepended with a comment marker. The separation between code and Markdown
149 prepended with a comment marker. The separation between code and Markdown
150 cells is indicated with special comments and there is a header indicating the
150 cells is indicated with special comments and there is a header indicating the
151 format version. All output is removed when exporting to Python.
151 format version. All output is removed when exporting to Python.
152
152
153 As an example, consider a simple notebook called ``simple.ipynb`` which
153 As an example, consider a simple notebook called ``simple.ipynb`` which
154 contains one Markdown cell, with the content ``The simplest notebook.``, one
154 contains one Markdown cell, with the content ``The simplest notebook.``, one
155 code input cell with the content ``print "Hello, IPython!"``, and the
155 code input cell with the content ``print "Hello, IPython!"``, and the
156 corresponding output.
156 corresponding output.
157
157
158 The contents of the notebook document ``simple.ipynb`` is the following JSON
158 The contents of the notebook document ``simple.ipynb`` is the following JSON
159 container::
159 container::
160
160
161 {
161 {
162 "metadata": {
162 "metadata": {
163 "name": "simple"
163 "name": "simple"
164 },
164 },
165 "nbformat": 3,
165 "nbformat": 3,
166 "nbformat_minor": 0,
166 "nbformat_minor": 0,
167 "worksheets": [
167 "worksheets": [
168 {
168 {
169 "cells": [
169 "cells": [
170 {
170 {
171 "cell_type": "markdown",
171 "cell_type": "markdown",
172 "metadata": {},
172 "metadata": {},
173 "source": "The simplest notebook."
173 "source": "The simplest notebook."
174 },
174 },
175 {
175 {
176 "cell_type": "code",
176 "cell_type": "code",
177 "collapsed": false,
177 "collapsed": false,
178 "input": "print \"Hello, IPython\"",
178 "input": "print \"Hello, IPython\"",
179 "language": "python",
179 "language": "python",
180 "metadata": {},
180 "metadata": {},
181 "outputs": [
181 "outputs": [
182 {
182 {
183 "output_type": "stream",
183 "output_type": "stream",
184 "stream": "stdout",
184 "stream": "stdout",
185 "text": "Hello, IPython\n"
185 "text": "Hello, IPython\n"
186 }
186 }
187 ],
187 ],
188 "prompt_number": 1
188 "prompt_number": 1
189 }
189 }
190 ],
190 ],
191 "metadata": {}
191 "metadata": {}
192 }
192 }
193 ]
193 ]
194 }
194 }
195
195
196
196
197 The corresponding Python script is::
197 The corresponding Python script is::
198
198
199 # -*- coding: utf-8 -*-
199 # -*- coding: utf-8 -*-
200 # <nbformat>3.0</nbformat>
200 # <nbformat>3.0</nbformat>
201
201
202 # <markdowncell>
202 # <markdowncell>
203
203
204 # The simplest notebook.
204 # The simplest notebook.
205
205
206 # <codecell>
206 # <codecell>
207
207
208 print "Hello, IPython"
208 print "Hello, IPython"
209
209
210 Note that indeed the output of the code cell, which is present in the JSON
210 Note that indeed the output of the code cell, which is present in the JSON
211 container, has been removed in the ``.py`` script.
211 container, has been removed in the ``.py`` script.
212
212
@@ -1,581 +1,590 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 renders
106 render it as a static web page. The results may thus be shared with a
106 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 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 .. _nbconvert: ./nbconvert.html
114
113 .. _nbviewer: http://nbviewer.ipython.org
115 .. _nbviewer: http://nbviewer.ipython.org
114
116
115 .. note::
117 .. note::
116
118
117 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
118 to work on notebooks in different directories. By default the first
120 to work on notebooks in different directories. By default the first
119 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
120 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
121 ``--port`` option.
123 ``--port`` option.
122
124
123
125
124 Basic workflow in the IPython Notebook web app
126 Basic workflow in the IPython Notebook web app
125 ----------------------------------------------
127 ----------------------------------------------
126
128
127 Starting up
129 Starting up
128 ~~~~~~~~~~~~
130 ~~~~~~~~~~~~
129
131
130 You can start running the Notebook web app using the following command::
132 You can start running the Notebook web app using the following command::
131
133
132 $ ipython notebook
134 $ ipython notebook
133
135
134 (Here, and in the sequel, the initial ``$`` represents the shell prompt,
136 (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.)
137 indicating that the command is to be run from the command line in a shell.)
136
138
137 The landing page of the IPython Notebook application, the *dashboard*, shows
139 The landing page of the IPython Notebook application, the *dashboard*, shows
138 the notebooks currently available in the *notebook directory* (By default, the directory
140 the notebooks currently available in the *notebook directory* (By default, the directory
139 from which the notebook was started).
141 from which the notebook was started).
140 You can create new notebooks from the dashboard with the ``New Notebook``
142 You can create new notebooks from the dashboard with the ``New Notebook``
141 button, or open existing ones by clicking on their name.
143 button, or open existing ones by clicking on their name.
142 You can also drag and drop ``.ipynb`` notebooks and standard ``.py`` Python
144 You can also drag and drop ``.ipynb`` notebooks and standard ``.py`` Python
143 source code files into the notebook list area.
145 source code files into the notebook list area.
144
146
145
147
146 You can open an existing notebook directly, without having to go via the
148 You can open an existing notebook directly, without having to go via the
147 dashboard, with:
149 dashboard, with::
148
150
149 ipython notebook my_notebook
151 ipython notebook my_notebook
150
152
151 The `.ipynb` extension is assumed if no extension is given.
153 The ``.ipynb`` extension is assumed if no extension is given.
152
154
153 The `File | Open...` menu option will open the dashboard in a new browser tab,
155 The `File | Open...` menu option will open the dashboard in a new browser tab,
154 to allow you to select a current notebook
156 to allow you to select a current notebook
155 from the notebook directory or to create a new notebook.
157 from the notebook directory or to create a new notebook.
156
158
157
159
158
160
159 Notebook user interface
161 Notebook user interface
160 ~~~~~~~~~~~~~~~~~~~~~~~
162 ~~~~~~~~~~~~~~~~~~~~~~~
161
163
162 When you open a new notebook document in the Notebook, you will be presented
164 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
165 with the title associated to the notebook space/document, a *menu bar*, a
164 *toolbar* and an empty *input cell*.
166 *toolbar* and an empty *input cell*.
165
167
166 Notebook title
168 Notebook title
167 ^^^^^^^^^^^^^^
169 ^^^^^^^^^^^^^^
168 The title of the notebook document that is currently being edited is displayed
170 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
171 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
172 be edited directly by clicking on it. The title is reflected in the name of
171 the ``.ipynb`` notebook document file that is saved.
173 the ``.ipynb`` notebook document file that is saved.
172
174
173 Menu bar
175 Menu bar
174 ^^^^^^^^
176 ^^^^^^^^
175 The menu bar presents different options that may be used to manipulate the way
177 The menu bar presents different options that may be used to manipulate the way
176 the Notebook functions.
178 the Notebook functions.
177
179
178 Toolbar
180 Toolbar
179 ^^^^^^^
181 ^^^^^^^
180 The tool bar gives a quick way of accessing the most-used operations within
182 The tool bar gives a quick way of accessing the most-used operations within
181 the Notebook, by clicking on an icon.
183 the Notebook, by clicking on an icon.
182
184
183
185
184 Creating a new notebook document
186 Creating a new notebook document
185 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
186
188
187 A new notebook space/document may be created at any time, either from the
189 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
190 dashboard, or using the `File | New` menu option from within an active
189 notebook. The new notebook is created within the same directory and
191 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
192 will open in a new browser tab. It will also be reflected as a new entry in
191 the notebook list on the dashboard.
193 the notebook list on the dashboard.
192
194
193
195
194 Structure of a notebook document
196 Structure of a notebook document
195 --------------------------------
197 --------------------------------
196
198
197 Input cells
199 Input cells
198 ~~~~~~~~~~~
200 ~~~~~~~~~~~
199 Input cells are at the core of the functionality of the IPython Notebook.
201 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
202 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
203 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.
204 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
205 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.
206 cell (see below), and then to display the resulting output.
205
207
206 The notebook consists of a sequence of input cells, labelled ``In[n]``, which
208 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
209 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
210 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
211 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
212 accessible as Python variables with the same names, forming a complete record
211 of the history of the computation.
213 of the history of the computation.
212
214
213
215
214
216
215 Input cell types
217 Input cell types
216 ~~~~~~~~~~~~~~~~
218 ~~~~~~~~~~~~~~~~
217 Each IPython input cell has a *cell type*, of which there is a restricted
219 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
220 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:
221 toolbar, or via the following keyboard shortcuts:
220
222
221 * **code**: :kbd:`Ctrl-m y`
223 * **code**: :kbd:`Ctrl-m y`
222 * **markdown**: :kbd:`Ctrl-m m`
224 * **markdown**: :kbd:`Ctrl-m m`
223 * **raw**: :kbd:`Ctrl-m t`
225 * **raw**: :kbd:`Ctrl-m t`
224 * **heading**: :kbd:`Ctrl-m 1` - :kbd:`Ctrl-m 6`
226 * **heading**: :kbd:`Ctrl-m 1` - :kbd:`Ctrl-m 6`
225
227
226 Upon initial creation, each input cell is by default a code cell.
228 Upon initial creation, each input cell is by default a code cell.
227
229
228
230
229 Code cells
231 Code cells
230 ^^^^^^^^^^
232 ^^^^^^^^^^
231 A *code input cell* allows you to edit code inline within the cell, with full
233 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
234 syntax highlighting and autocompletion/introspection. By default, the language
233 associated to a code cell is Python, but other languages, such as ``julia``
235 associated to a code cell is Python, but other languages, such as ``julia``
234 and ``R``, can be handled using magic commands (see below).
236 and ``R``, can be handled using magic commands (see below).
235
237
236 When a code cell is executed with :kbd:`Shift-Enter`, the code that it
238 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
239 contains is transparently exported and run in that language (with automatic
238 compiling, etc., if necessary). The result that is returned from this
240 compiling, etc., if necessary). The result that is returned from this
239 computation is then displayed in the notebook space as the cell's
241 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
242 *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
243 numbered *output cell*. However, many other possible forms of output are also
242 possible, including ``matplotlib`` figures and HTML tables (as used, for
244 possible, including ``matplotlib`` figures and HTML tables (as used, for
243 example, in the ``pandas`` data analyis package). This is known as IPython's
245 example, in the ``pandas`` data analyis package). This is known as IPython's
244 *rich display* capability.
246 *rich display* capability.
245
247
246
248
247 Markdown cells
249 Markdown cells
248 ^^^^^^^^^^^^^^
250 ^^^^^^^^^^^^^^
249 You can document the computational process in a literate way, alternating
251 You can document the computational process in a literate way, alternating
250 descriptive text with code, using *rich text*. In IPython this is accomplished
252 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
253 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
254 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
255 perform this text markup, that is, to specify which parts of the text should
254 be emphasized (italics), bold, form lists, etc.
256 be emphasized (italics), bold, form lists, etc.
255
257
256
258
257 When a Markdown input cell is executed, the Markdown code is converted into
259 When a Markdown input cell is executed, the Markdown code is converted into
258 the corresponding formatted rich text. This output then *replaces* the
260 the corresponding formatted rich text. This output then *replaces* the
259 original Markdown input cell, leaving just the visually-significant marked up
261 original Markdown input cell, leaving just the visually-significant marked up
260 rich text. Markdown allows arbitrary HTML code for formatting.
262 rich text. Markdown allows arbitrary HTML code for formatting.
261
263
262 Within Markdown cells, you can also include *mathematics* in a straightforward
264 Within Markdown cells, you can also include *mathematics* in a straightforward
263 way, using standard LaTeX notation: ``$...$`` for inline mathematics and
265 way, using standard LaTeX notation: ``$...$`` for inline mathematics and
264 ``$$...$$`` for displayed mathematics. When the Markdown cell is executed,
266 ``$$...$$`` for displayed mathematics. When the Markdown cell is executed,
265 the LaTeX portions are automatically rendered in the HTML output as equations
267 the LaTeX portions are automatically rendered in the HTML output as equations
266 with high quality typography. This is made possible by MathJax_, which
268 with high quality typography. This is made possible by MathJax_, which
267 supports a `large subset <mathjax_tex>`_ of LaTeX functionality
269 supports a `large subset <mathjax_tex>`_ of LaTeX functionality
268
270
269 .. _mathjax_tex: http://docs.mathjax.org/en/latest/tex.html
271 .. _mathjax_tex: http://docs.mathjax.org/en/latest/tex.html
270
272
271 Standard mathematics environments defined by LaTeX and AMS-LaTeX (the
273 Standard mathematics environments defined by LaTeX and AMS-LaTeX (the
272 `amsmath` package) also work, such as
274 `amsmath` package) also work, such as
273 ``\begin{equation}...\end{equation}``, and ``\begin{align}...\end{align}``.
275 ``\begin{equation}...\end{equation}``, and ``\begin{align}...\end{align}``.
274 New LaTeX macros may be defined using standard methods,
276 New LaTeX macros may be defined using standard methods,
275 such as ``\newcommand``, by placing them anywhere *between math delimiters* in
277 such as ``\newcommand``, by placing them anywhere *between math delimiters* in
276 a Markdown cell. These definitions are then available throughout the rest of
278 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
279 the IPython session. (Note, however, that more care must be taken when using
278 nbconvert_ to output to LaTeX).
280 nbconvert_ to output to LaTeX).
279
281
280 Raw input cells
282 Raw input cells
281 ~~~~~~~~~~~~~~~
283 ~~~~~~~~~~~~~~~
282
284
283 *Raw* input cells provide a place in which you can write *output* directly.
285 *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.
286 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,
287 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
288 allowing you to type full latex into a raw cell, which will only be rendered
287 by latex after conversion by nbconvert.
289 by latex after conversion by nbconvert.
288
290
289 Heading cells
291 Heading cells
290 ~~~~~~~~~~~~~
292 ~~~~~~~~~~~~~
291
293
292 You can provide a conceptual structure for your computational document as a
294 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
295 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
296 level 1 (top level) down to level 6 (paragraph). These can be used later for
295 constructing tables of contents, etc.
297 constructing tables of contents, etc.
296
298
297 As with Markdown cells, a heading input cell is replaced by a rich text
299 As with Markdown cells, a heading input cell is replaced by a rich text
298 rendering of the heading when the cell is executed.
300 rendering of the heading when the cell is executed.
299
301
300
302
301 Basic workflow
303 Basic workflow
302 --------------
304 --------------
303
305
304 The normal workflow in a notebook is, then, quite similar to a standard
306 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
307 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
308 times until you obtain the desired results, rather than having to
307 rerun separate scripts with the ``%run`` magic command. (Magic commands do,
309 rerun separate scripts with the ``%run`` magic command. (Magic commands do,
308 however, also work in the notebook; see below).
310 however, also work in the notebook; see below).
309
311
310 Typically, you will work on a computational problem in pieces, organizing
312 Typically, you will work on a computational problem in pieces, organizing
311 related ideas into cells and moving forward once previous parts work
313 related ideas into cells and moving forward once previous parts work
312 correctly. This is much more convenient for interactive exploration than
314 correctly. This is much more convenient for interactive exploration than
313 breaking up a computation into scripts that must be executed together, as was
315 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
316 previously necessary, especially if parts of them take a long time to run
315
317
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
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!
320
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
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
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
327 that same kernel.
328
329 At certain moments, it may be necessary to interrupt a calculation which is
318 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``
319 taking too long to complete. This may be done with the ``Kernel | Interrupt``
331 menu option, or the :kbd:``Ctrl-i`` keyboard shortcut.
320 menu option, or the :kbd:``Ctrl-i`` keyboard shortcut.
332 Similarly, it may be necessary or desirable to restart the whole computational
321 Similarly, it may be necessary or desirable to restart the whole computational
333 process, with the ``Kernel | Restart`` menu option or :kbd:``Ctrl-.``
322 process, with the ``Kernel | Restart`` menu option or :kbd:``Ctrl-.``
334 shortcut. This gives an equivalent state to loading the notebook document
323 shortcut. This gives an equivalent state to loading the notebook document
335 afresh.
324 afresh.
336
325
337
326 A notebook may be downloaded in either ``.ipynb`` or raw ``.py`` form from the
327 menu option ``File | Download as``. Choosing the ``.py`` option downloads a
328 Python ``.py`` script, in which all output has been removed and the content of
329 Markdown cells in comment areas. See ref:`below <notebook_format>` for more
330 details on the notebook format.
331
338 .. warning::
332 .. warning::
339
333
340 While in simple cases you can "roundtrip" a notebook to Python, edit the
334 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
335 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
336 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
337 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
338 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
339 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
340 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
341 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
342 a notebook started. But the Python version is *not* an alternate notebook
349 format.
343 format.
350
344
351
345
352 Keyboard shortcuts
346 Keyboard shortcuts
353 ~~~~~~~~~~~~~~~~~~
347 ~~~~~~~~~~~~~~~~~~
354 All actions in the notebook can be performed with the mouse, but keyboard
348 All actions in the notebook can be performed with the mouse, but keyboard
355 shortcuts are also available for the most common ones. The essential shortcuts
349 shortcuts are also available for the most common ones. The essential shortcuts
356 to remember are the following:
350 to remember are the following:
357
351
358 * :kbd:`Shift-Enter`: run cell
352 * :kbd:`Shift-Enter`: run cell
359 Execute the current cell, show output (if any), and jump to the next cell
353 Execute the current cell, show output (if any), and jump to the next cell
360 below. If :kbd:`Shift-Enter` is invoked on the last input cell, a new code
354 below. If :kbd:`Shift-Enter` is invoked on the last input cell, a new code
361 cell will also be created. Note that in the notebook, typing :kbd:`Enter`
355 cell will also be created. Note that in the notebook, typing :kbd:`Enter`
362 on its own *never* forces execution, but rather just inserts a new line in
356 on its own *never* forces execution, but rather just inserts a new line in
363 the current input cell. :kbd:`Shift-Enter` is equivalent to clicking the
357 the current input cell. :kbd:`Shift-Enter` is equivalent to clicking the
364 ``Cell | Run`` menu item.
358 ``Cell | Run`` menu item.
365
359
366 * :kbd:`Ctrl-Enter`: run cell in-place
360 * :kbd:`Ctrl-Enter`: run cell in-place
367 Execute the current cell as if it were in "terminal mode", where any
361 Execute the current cell as if it were in "terminal mode", where any
368 output is shown, but the cursor *remains* in the current cell. The cell's
362 output is shown, but the cursor *remains* in the current cell. The cell's
369 entire contents are selected after execution, so you can just start typing
363 entire contents are selected after execution, so you can just start typing
370 and only the new input will be in the cell. This is convenient for doing
364 and only the new input will be in the cell. This is convenient for doing
371 quick experiments in place, or for querying things like filesystem
365 quick experiments in place, or for querying things like filesystem
372 content, without needing to create additional cells that you may not want
366 content, without needing to create additional cells that you may not want
373 to be saved in the notebook.
367 to be saved in the notebook.
374
368
375 * :kbd:`Alt-Enter`: run cell, insert below
369 * :kbd:`Alt-Enter`: run cell, insert below
376 Executes the current cell, shows the output, and inserts a *new* input
370 Executes the current cell, shows the output, and inserts a *new* input
377 cell between the current cell and the cell below (if one exists). This
371 cell between the current cell and the cell below (if one exists). This
378 is thus a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`.
372 is thus a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`.
379 (:kbd:`Ctrl-m a` adds a new cell above the current one.)
373 (:kbd:`Ctrl-m a` adds a new cell above the current one.)
380
374
381 * :kbd:`Ctrl-m`:
375 * :kbd:`Ctrl-m`:
382 This is the prefix for *all* other shortcuts, which consist of :kbd:`Ctrl-m`
376 This is the prefix for *all* other shortcuts, which consist of :kbd:`Ctrl-m`
383 followed by a single letter or character. For example, if you type
377 followed by a single letter or character. For example, if you type
384 :kbd:`Ctrl-m h` (that is, the sole letter :kbd:`h` after :kbd:`Ctrl-m`),
378 :kbd:`Ctrl-m h` (that is, the sole letter :kbd:`h` after :kbd:`Ctrl-m`),
385 IPython will show you all the available keyboard shortcuts.
379 IPython will show you all the available keyboard shortcuts.
386
380
387
381
388 ..
382 ..
389 TODO: these live in IPython/html/static/notebook/js/quickhelp.js
383 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
384 They were last updated for IPython 1.0 release, so update them again for
391 future releases.
385 future releases.
392
386
393 Here is the complete set of keyboard shortcuts available:
387 Here is the complete set of keyboard shortcuts available:
394
388
395 ============ ==========================
389 ============ ==========================
396 **Shortcut** **Action**
390 **Shortcut** **Action**
397 ------------ --------------------------
391 ------------ --------------------------
398 Shift-Enter run cell
392 Shift-Enter run cell
399 Ctrl-Enter run cell in-place
393 Ctrl-Enter run cell in-place
400 Alt-Enter run cell, insert below
394 Alt-Enter run cell, insert below
401 Ctrl-m x cut cell
395 Ctrl-m x cut cell
402 Ctrl-m c copy cell
396 Ctrl-m c copy cell
403 Ctrl-m v paste cell
397 Ctrl-m v paste cell
404 Ctrl-m d delete cell
398 Ctrl-m d delete cell
405 Ctrl-m z undo last cell deletion
399 Ctrl-m z undo last cell deletion
406 Ctrl-m - split cell
400 Ctrl-m - split cell
407 Ctrl-m a insert cell above
401 Ctrl-m a insert cell above
408 Ctrl-m b insert cell below
402 Ctrl-m b insert cell below
409 Ctrl-m o toggle output
403 Ctrl-m o toggle output
410 Ctrl-m O toggle output scroll
404 Ctrl-m O toggle output scroll
411 Ctrl-m l toggle line numbers
405 Ctrl-m l toggle line numbers
412 Ctrl-m s save notebook
406 Ctrl-m s save notebook
413 Ctrl-m j move cell down
407 Ctrl-m j move cell down
414 Ctrl-m k move cell up
408 Ctrl-m k move cell up
415 Ctrl-m y code cell
409 Ctrl-m y code cell
416 Ctrl-m m markdown cell
410 Ctrl-m m markdown cell
417 Ctrl-m t raw cell
411 Ctrl-m t raw cell
418 Ctrl-m 1-6 heading 1-6 cell
412 Ctrl-m 1-6 heading 1-6 cell
419 Ctrl-m p select previous
413 Ctrl-m p select previous
420 Ctrl-m n select next
414 Ctrl-m n select next
421 Ctrl-m i interrupt kernel
415 Ctrl-m i interrupt kernel
422 Ctrl-m . restart kernel
416 Ctrl-m . restart kernel
423 Ctrl-m h show keyboard shortcuts
417 Ctrl-m h show keyboard shortcuts
424 ============ ==========================
418 ============ ==========================
425
419
426
420
427
421
428 Magic commands
422 Magic commands
429 --------------
423 --------------
430 Magic commands, or *magics*, are commands for controlling IPython itself.
424 Magic commands, or *magics*, are commands for controlling IPython itself.
431 They all begin with ``%`` and are entered into code input cells; the code
425 They all begin with ``%`` and are entered into code input cells; the code
432 cells are executed as usual with :kbd:`Shift-Enter`.
426 cells are executed as usual with :kbd:`Shift-Enter`.
433
427
434 The magic commands call special functions defined by IPython which manipulate
428 The magic commands call special functions defined by IPython which manipulate
435 the computational state in certain ways.
429 the computational state in certain ways.
436
430
437 There are two types of magics:
431 There are two types of magics:
438
432
439 - **line magics**:
433 - **line magics**:
440
434
441 These begin with a single ``%`` and take as arguments the rest of the
435 These begin with a single ``%`` and take as arguments the rest of the
442 *same line* of the code cell. Any other lines of the code cell are
436 *same line* of the code cell. Any other lines of the code cell are
443 treated as if they were part of a standard code cell.
437 treated as if they were part of a standard code cell.
444
438
445 - **cell magics**:
439 - **cell magics**:
446
440
447 These begin with ``%%`` and operate on the *entire* remaining contents
441 These begin with ``%%`` and operate on the *entire* remaining contents
448 of the code cell.
442 of the code cell.
449
443
450 Line magics
444 Line magics
451 ~~~~~~~~~~~
445 ~~~~~~~~~~~
452 Some of the available line magics are the following:
446 Some of the available line magics are the following:
453
447
454 * ``%load filename``:
448 * ``%load filename``:
455
449
456 Loads the contents of the file ``filename`` into a new code cell. This
450 Loads the contents of the file ``filename`` into a new code cell. This
457 can be a URL for a remote file.
451 can be a URL for a remote file.
458
452
459 * ``%timeit code``:
453 * ``%timeit code``:
460
454
461 An easy way to time how long the single line of code ``code`` takes to
455 An easy way to time how long the single line of code ``code`` takes to
462 run
456 run
463
457
464 * ``%config``:
458 * ``%config``:
465
459
466 Configuration of the IPython Notebook
460 Configuration of the IPython Notebook
467
461
468 * ``%lsmagic``:
462 * ``%lsmagic``:
469
463
470 Provides a list of all available magic commands
464 Provides a list of all available magic commands
471
465
472 Cell magics
466 Cell magics
473 ~~~~~~~~~~~
467 ~~~~~~~~~~~
474
468
475 * ``%%latex``:
469 * ``%%latex``:
476
470
477 Renders the entire contents of the cell in LaTeX, without needing to use
471 Renders the entire contents of the cell in LaTeX, without needing to use
478 explicit LaTeX delimiters.
472 explicit LaTeX delimiters.
479
473
480 * ``%%bash``:
474 * ``%%bash``:
481
475
482 The code cell is executed by sending it to be executed by ``bash``. The
476 The code cell is executed by sending it to be executed by ``bash``. The
483 output of the ``bash`` commands is captured and displayed in the
477 output of the ``bash`` commands is captured and displayed in the
484 notebook.
478 notebook.
485
479
486 * ``%%file filename``:
480 * ``%%file filename``:
487
481
488 Writes the contents of the cell to the file ``filename``.
482 Writes the contents of the cell to the file ``filename``.
489 **Caution**: The file is over-written without warning!
483 **Caution**: The file is over-written without warning!
490
484
491 * ``%%R``:
485 * ``%%R``:
492
486
493 Execute the contents of the cell using the R language.
487 Execute the contents of the cell using the R language.
494
488
495 * ``%%timeit``:
489 * ``%%timeit``:
496
490
497 Version of ``%timeit`` which times the entire block of code in the
491 Version of ``%timeit`` which times the entire block of code in the
498 current code cell.
492 current code cell.
499
493
500
494
501
495
502 Several of the cell magics provide functionality to manipulate the filesystem
496 Several of the cell magics provide functionality to manipulate the filesystem
503 of a remote server to which you otherwise do not have access.
497 of a remote server to which you otherwise do not have access.
504
498
505
499
506 Plotting
500 Plotting
507 --------
501 --------
508 One major feature of the Notebook is the ability to interact with
502 One major feature of the Notebook is the ability to interact with
509 plots that are the output of running code cells. IPython is designed to work
503 plots that are the output of running code cells. IPython is designed to work
510 seamlessly with the ``matplotlib`` plotting library to provide this
504 seamlessly with the ``matplotlib`` plotting library to provide this
511 functionality.
505 functionality.
512
506
513 To set this up, before any plotting is performed you must execute the
507 To set this up, before any plotting is performed you must execute the
514 ``%matplotlib`` magic command. This performs the necessary behind-the-scenes
508 ``%matplotlib`` magic command. This performs the necessary behind-the-scenes
515 setup for IPython to work correctly hand in hand with ``matplotlib``; it does
509 setup for IPython to work correctly hand in hand with ``matplotlib``; it does
516 *not*, however, actually execute any Python ``import`` commands, that is, no
510 *not*, however, actually execute any Python ``import`` commands, that is, no
517 names are added to the namespace.
511 names are added to the namespace.
518
512
519 If the ``%matplotlib`` magic is called without an argument, the
513 If the ``%matplotlib`` magic is called without an argument, the
520 output of a plotting command is displayed using the default ``matplotlib``
514 output of a plotting command is displayed using the default ``matplotlib``
521 backend in a separate window. Alternatively, the backend can be explicitly
515 backend in a separate window. Alternatively, the backend can be explicitly
522 requested using, for example::
516 requested using, for example::
523
517
524 %matplotlib gtk
518 %matplotlib gtk
525
519
526 A particularly interesting backend is the ``inline`` backend.
520 A particularly interesting backend is the ``inline`` backend.
527 This is applicable only for the IPython Notebook and the IPython QtConsole.
521 This is applicable only for the IPython Notebook and the IPython QtConsole.
528 It can be invoked as follows::
522 It can be invoked as follows::
529
523
530 %matplotlib inline
524 %matplotlib inline
531
525
532 With this backend, output of plotting commands is displayed *inline* within
526 With this backend, output of plotting commands is displayed *inline* within
533 the notebook format, directly below the input cell that produced it. The
527 the notebook format, directly below the input cell that produced it. The
534 resulting plots will then also be stored in the notebook document. This
528 resulting plots will then also be stored in the notebook document. This
535 provides a key part of the functionality for reproducibility_ that the IPython
529 provides a key part of the functionality for reproducibility_ that the IPython
536 Notebook provides.
530 Notebook provides.
537
531
538 .. _reproducibility: https://en.wikipedia.org/wiki/Reproducibility
532 .. _reproducibility: https://en.wikipedia.org/wiki/Reproducibility
539
533
540
534
541
535
542 Configuring the IPython Notebook
536 Configuring the IPython Notebook
543 --------------------------------
537 --------------------------------
544 The IPython Notebook can be run with a variety of command line arguments.
538 The IPython Notebook can be run with a variety of command line arguments.
545 To see a list of available options enter::
539 To see a list of available options enter::
546
540
547 $ ipython notebook --help
541 $ ipython notebook --help
548
542
549 Defaults for these options can also be set by creating a file named
543 Defaults for these options can also be set by creating a file named
550 ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile
544 ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile
551 folder is a subfolder of your IPython directory; to find out where it is
545 folder is a subfolder of your IPython directory; to find out where it is
552 located, run::
546 located, run::
553
547
554 $ ipython locate
548 $ ipython locate
555
549
556 To create a new set of default configuration files, with lots of information
550 To create a new set of default configuration files, with lots of information
557 on available options, use::
551 on available options, use::
558
552
559 $ ipython profile create
553 $ ipython profile create
560
554
561 .. seealso:
555 .. seealso::
562
556
563 :ref:`config_overview`, in particular :ref:`Profiles`.
557 :ref:`config_overview`, in particular :ref:`Profiles`.
564
558
565
559
566 Importing `.py` files
560 Importing ``.py`` files
567 ----------------------
561 -----------------------
568
569
562
570 ``.py`` files will be imported into the IPython Notebook as a notebook with
563 ``.py`` files will be imported into the IPython Notebook as a notebook with
571 the same basename, but an ``.ipynb`` extension, located in the notebook
564 the same basename, but an ``.ipynb`` extension, located in the notebook
572 directory. The notebook created will have just one cell, which will contain
565 directory. The notebook created will have just one cell, which will contain
573 all the code in the ``.py`` file. You can later manually partition this into
566 all the code in the ``.py`` file. You can later manually partition this into
574 individual cells using the ``Edit | Split Cell`` menu option, or the
567 individual cells using the ``Edit | Split Cell`` menu option, or the
575 :kbd:`Ctrl-m -` keyboard shortcut.
568 :kbd:`Ctrl-m -` keyboard shortcut.
576
569
577 .. Alternatively, prior to importing the ``.py``, you can manually add ``# <
570 Note that ``.py`` scripts obtained from a notebook document using nbconvert_
578 nbformat>2</nbformat>`` at the start of the file, and then add separators for
571 maintain the structure of the notebook in comments. Reimporting such a
579 text and code cells, to get a cleaner import with the file already broken into
572 script back into the Notebook will preserve this structxure.
580 individual cells.
581
573
574
575 .. warning::
576
577 You can "roundtrip" a notebook to Python, by exporting the
578 notebook to a ``.py`` script, editing the script, and then importing it back
579 into the Notebook without loss of main content. However,
580 in general this is *not guaranteed* to work. First, there is extra metadata
581 saved in the notebook that may not be saved to the ``.py`` format. Second,
582 as the notebook format evolves in complexity, there will be attributes of
583 the notebook that will not survive a roundtrip through the Python form. You
584 should think of the Python format as a way to output a script version of a
585 notebook and the import capabilities as a way to load existing code to get
586 a notebook started. But the Python version is *not* an alternate notebook
587 format.
588
589 .. seealso::
590 :ref:`notebook_format`
@@ -1,182 +1,187 b''
1 .. _working_remotely.txt
1 .. _working_remotely.txt
2
2
3 Working remotely
3 Working remotely
4 ================
4 ================
5
5
6
6
7 The IPython Notebook web app is based on a server-client structure.
7 The IPython Notebook web app is based on a server-client structure.
8 This server uses a two-process kernel architecture based on ZeroMQ, as well as
8 This server uses a two-process kernel architecture based on ZeroMQ_, as well
9 Tornado for serving HTTP requests. Other clients may connect to the same
9 as Tornado_ for serving HTTP requests. Other clients may connect to the same
10 underlying IPython kernel; see below.
10 underlying IPython kernel; see below.
11
11
12 .. _ZeroMQ: http://zeromq.org
13
14 .. _Tornado: http://www.tornadoweb.org
15
16
12 .. _notebook_security:
17 .. _notebook_security:
13
18
14 Security
19 Security
15 --------
20 --------
16
21
17 You can protect your Notebook server with a simple single password by
22 You can protect your Notebook server with a simple single password by
18 setting the :attr:`NotebookApp.password` configurable. You can prepare a
23 setting the :attr:`NotebookApp.password` configurable. You can prepare a
19 hashed password using the function :func:`IPython.lib.security.passwd`:
24 hashed password using the function :func:`IPython.lib.security.passwd`:
20
25
21 .. sourcecode:: ipython
26 .. sourcecode:: ipython
22
27
23 In [1]: from IPython.lib import passwd
28 In [1]: from IPython.lib import passwd
24 In [2]: passwd()
29 In [2]: passwd()
25 Enter password:
30 Enter password:
26 Verify password:
31 Verify password:
27 Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
32 Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
28
33
29 .. note::
34 .. note::
30
35
31 :func:`~IPython.lib.security.passwd` can also take the password as a string
36 :func:`~IPython.lib.security.passwd` can also take the password as a string
32 argument. **Do not** pass it as an argument inside an IPython session, as it
37 argument. **Do not** pass it as an argument inside an IPython session, as it
33 will be saved in your input history.
38 will be saved in your input history.
34
39
35 You can then add this to your :file:`ipython_notebook_config.py`, e.g.::
40 You can then add this to your :file:`ipython_notebook_config.py`, e.g.::
36
41
37 # Password to use for web authentication
42 # Password to use for web authentication
38 c = get_config()
43 c = get_config()
39 c.NotebookApp.password =
44 c.NotebookApp.password =
40 u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
45 u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
41
46
42 When using a password, it is a good idea to also use SSL, so that your
47 When using a password, it is a good idea to also use SSL, so that your
43 password is not sent unencrypted by your browser. You can start the notebook
48 password is not sent unencrypted by your browser. You can start the notebook
44 to communicate via a secure protocol mode using a self-signed certificate with
49 to communicate via a secure protocol mode using a self-signed certificate with
45 the command::
50 the command::
46
51
47 $ ipython notebook --certfile=mycert.pem
52 $ ipython notebook --certfile=mycert.pem
48
53
49 .. note::
54 .. note::
50
55
51 A self-signed certificate can be generated with ``openssl``. For example,
56 A self-signed certificate can be generated with ``openssl``. For example,
52 the following command will create a certificate valid for 365 days with
57 the following command will create a certificate valid for 365 days with
53 both the key and certificate data written to the same file::
58 both the key and certificate data written to the same file::
54
59
55 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.
60 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.
56 pem -out mycert.pem
61 pem -out mycert.pem
57
62
58 Your browser will warn you of a dangerous certificate because it is
63 Your browser will warn you of a dangerous certificate because it is
59 self-signed. If you want to have a fully compliant certificate that will not
64 self-signed. If you want to have a fully compliant certificate that will not
60 raise warnings, it is possible (but rather involved) to obtain one,
65 raise warnings, it is possible (but rather involved) to obtain one,
61 `as explained in detailed in this tutorial`__.
66 as explained in detail in `this tutorial`__.
62
67
63 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-
68 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-
64 secure-sertificate-for-free.ars
69 secure-sertificate-for-free.ars
65
70
66 Keep in mind that when you enable SSL support, you will need to access the
71 Keep in mind that when you enable SSL support, you will need to access the
67 notebook server over ``https://``, not over plain ``http://``. The startup
72 notebook server over ``https://``, not over plain ``http://``. The startup
68 message from the server prints this, but it is easy to overlook and think the
73 message from the server prints this, but it is easy to overlook and think the
69 server is for some reason non-responsive.
74 server is for some reason non-responsive.
70
75
71
76
72 Connecting to an existing kernel
77 Connecting to an existing kernel
73 ---------------------------------
78 ---------------------------------
74
79
75 The notebook server always prints to the terminal the full details of
80 The notebook server always prints to the terminal the full details of
76 how to connect to each kernel, with messages such as the following::
81 how to connect to each kernel, with messages such as the following::
77
82
78 [IPKernelApp] To connect another client to this kernel, use:
83 [IPKernelApp] To connect another client to this kernel, use:
79 [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
84 [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
80
85
81 This long string is the name of a JSON file that contains all the port and
86 This long string is the name of a JSON file that contains all the port and
82 validation information necessary to connect to the kernel. You can then, for
87 validation information necessary to connect to the kernel. You can then, for
83 example, manually start a Qt console connected to the *same* kernel with::
88 example, manually start a Qt console connected to the *same* kernel with::
84
89
85 $ ipython qtconsole --existing
90 $ ipython qtconsole --existing
86 kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
91 kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
87
92
88 If you have only a single kernel running, simply typing::
93 If you have only a single kernel running, simply typing::
89
94
90 $ ipython qtconsole --existing
95 $ ipython qtconsole --existing
91
96
92 will automatically find it. (It will always find the most recently
97 will automatically find it. (It will always find the most recently
93 started kernel if there is more than one.) You can also request this
98 started kernel if there is more than one.) You can also request this
94 connection data by typing ``%connect_info``; this will print the same
99 connection data by typing ``%connect_info``; this will print the same
95 file information as well as the content of the JSON data structure it
100 file information as well as the content of the JSON data structure it
96 contains.
101 contains.
97
102
98
103
99 Running a public notebook server
104 Running a public notebook server
100 --------------------------------
105 --------------------------------
101
106
102 If you want to access your notebook server remotely via a web browser,
107 If you want to access your notebook server remotely via a web browser,
103 you can do the following.
108 you can do the following.
104
109
105 Start by creating a certificate file and a hashed password, as explained
110 Start by creating a certificate file and a hashed password, as explained
106 above. Then create a custom profile for the notebook, with the following
111 above. Then create a custom profile for the notebook, with the following
107 command line, type::
112 command line, type::
108
113
109 $ ipython profile create nbserver
114 $ ipython profile create nbserver
110
115
111 In the profile directory just created, edit the file
116 In the profile directory just created, edit the file
112 ``ipython_notebook_config.py``. By default, the file has all fields
117 ``ipython_notebook_config.py``. By default, the file has all fields
113 commented; the minimum set you need to uncomment and edit is the following::
118 commented; the minimum set you need to uncomment and edit is the following::
114
119
115 c = get_config()
120 c = get_config()
116
121
117 # Kernel config
122 # Kernel config
118 c.IPKernelApp.pylab = 'inline' # if you want plotting support always
123 c.IPKernelApp.pylab = 'inline' # if you want plotting support always
119
124
120 # Notebook config
125 # Notebook config
121 c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
126 c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
122 c.NotebookApp.ip = '*'
127 c.NotebookApp.ip = '*'
123 c.NotebookApp.open_browser = False
128 c.NotebookApp.open_browser = False
124 c.NotebookApp.password = u'sha1:bcd259ccf...[your hashed password here]'
129 c.NotebookApp.password = u'sha1:bcd259ccf...[your hashed password here]'
125 # It is a good idea to put it on a known, fixed port
130 # It is a good idea to put it on a known, fixed port
126 c.NotebookApp.port = 9999
131 c.NotebookApp.port = 9999
127
132
128 You can then start the notebook and access it later by pointing your browser
133 You can then start the notebook and access it later by pointing your browser
129 to ``https://your.host.com:9999`` with ``ipython notebook
134 to ``https://your.host.com:9999`` with ``ipython notebook
130 --profile=nbserver``.
135 --profile=nbserver``.
131
136
132 Running with a different URL prefix
137 Running with a different URL prefix
133 -----------------------------------
138 -----------------------------------
134
139
135 The notebook dashboard (the landing page with an overview
140 The notebook dashboard (the landing page with an overview
136 of the notebooks in your working directory) typically lives at the URL
141 of the notebooks in your working directory) typically lives at the URL
137 ``http://localhost:8888/``. If you prefer that it lives, together with the
142 ``http://localhost:8888/``. If you prefer that it lives, together with the
138 rest of the notebook, under a sub-directory,
143 rest of the notebook, under a sub-directory,
139 e.g. ``http://localhost:8888/ipython/``, you can do so with
144 e.g. ``http://localhost:8888/ipython/``, you can do so with
140 configuration options like the following (see above for instructions about
145 configuration options like the following (see above for instructions about
141 modifying ``ipython_notebook_config.py``)::
146 modifying ``ipython_notebook_config.py``)::
142
147
143 c.NotebookApp.base_project_url = '/ipython/'
148 c.NotebookApp.base_project_url = '/ipython/'
144 c.NotebookApp.base_kernel_url = '/ipython/'
149 c.NotebookApp.base_kernel_url = '/ipython/'
145 c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'}
150 c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'}
146
151
147 Using a different notebook store
152 Using a different notebook store
148 --------------------------------
153 --------------------------------
149
154
150 By default, the Notebook app stores the notebook documents that it saves as
155 By default, the Notebook app stores the notebook documents that it saves as
151 files in the working directory of the Notebook app, also known as the
156 files in the working directory of the Notebook app, also known as the
152 ``notebook_dir``. This logic is implemented in the
157 ``notebook_dir``. This logic is implemented in the
153 :class:`FileNotebookManager` class. However, the server can be configured to
158 :class:`FileNotebookManager` class. However, the server can be configured to
154 use a different notebook manager class, which can
159 use a different notebook manager class, which can
155 store the notebooks in a different format.
160 store the notebooks in a different format.
156
161
157 Currently, we ship a :class:`AzureNotebookManager` class that stores notebooks
162 Currently, we ship a :class:`AzureNotebookManager` class that stores notebooks
158 in Azure blob storage. This can be used by adding the following lines to your
163 in Azure blob storage. This can be used by adding the following lines to your
159 ``ipython_notebook_config.py`` file::
164 ``ipython_notebook_config.py`` file::
160
165
161 c.NotebookApp.notebook_manager_class =
166 c.NotebookApp.notebook_manager_class =
162 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager'
167 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager'
163 c.AzureNotebookManager.account_name = u'paste_your_account_name_here'
168 c.AzureNotebookManager.account_name = u'paste_your_account_name_here'
164 c.AzureNotebookManager.account_key = u'paste_your_account_key_here'
169 c.AzureNotebookManager.account_key = u'paste_your_account_key_here'
165 c.AzureNotebookManager.container = u'notebooks'
170 c.AzureNotebookManager.container = u'notebooks'
166
171
167 In addition to providing your Azure Blob Storage account name and key, you
172 In addition to providing your Azure Blob Storage account name and key, you
168 will have to provide a container name; you can use multiple containers to
173 will have to provide a container name; you can use multiple containers to
169 organize your notebooks.
174 organize your notebooks.
170
175
171
176
172 Known issues
177 Known issues
173 ------------
178 ------------
174
179
175 When behind a proxy, especially if your system or browser is set to autodetect
180 When behind a proxy, especially if your system or browser is set to autodetect
176 the proxy, the Notebook app might fail to connect to the server's websockets,
181 the proxy, the Notebook app might fail to connect to the server's websockets,
177 and present you with a warning at startup. In this case, you need to configure
182 and present you with a warning at startup. In this case, you need to configure
178 your system not to use the proxy for the server's address.
183 your system not to use the proxy for the server's address.
179
184
180 For example, in Firefox, go to the Preferences panel, Advanced section,
185 For example, in Firefox, go to the Preferences panel, Advanced section,
181 Network tab, click 'Settings...', and add the address of the notebook server
186 Network tab, click 'Settings...', and add the address of the notebook server
182 to the 'No proxy for' field.
187 to the 'No proxy for' field.
General Comments 0
You need to be logged in to leave comments. Login now