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