##// END OF EJS Templates
Minor formatting modifications (mainly where there was one backtick instead of two)
David P. Sanders -
Show More
@@ -1,674 +1,676 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 (we could say that they are "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 (we could say that they are "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 and indentation and tab completion/introspection.
39 * In-browser editing for code, with automatic syntax highlighting and indentation and tab completion/introspection.
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 the :ref:`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 You can start running the Notebook web app using the following command::
79 You can start running the Notebook web app using the following 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 created will have just one cell, which will contain 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 created will have just one cell, which will contain all the
89 code in the ``.py`` file. You can later manually partition this into individual cells using the ``Edit | Split Cell`` menu option, or the :kbd:`Ctrl-m -` keyboard shortcut.
89 code in the ``.py`` file. You can later manually partition this into individual cells using the ``Edit | Split Cell`` menu option, or the :kbd:`Ctrl-m -` keyboard shortcut.
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 outputs ``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 contents of all of these cells are accessible as Python variables with the same names, forming a complete record of the history of the computation.
128 The notebook consists of a sequence of input cells, labelled ``In[n]``, which may be executed in a non-linear way, and outputs ``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 contents of all of these cells are accessible as Python variables with the same names, forming a complete record of the history of the computation.
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).
135 rerun separate scripts with the ``%run`` magic command. (Magic commands do, however, also work in the notebook; see below).
136
136
137 Typically, you will work on a computational problem in pieces, organizing related ideas into cells and moving forward once previous parts work correctly. This is much more convenient for interactive exploration than breaking up a computation into scripts that must be executed together, as was previously necessary, especially if parts of them take a long time to run
137 Typically, you will work on a computational problem in pieces, organizing related ideas into cells and moving forward once previous parts work correctly. This is much more convenient for interactive exploration than breaking up a computation into scripts that must be executed together, as was previously necessary, especially if parts of them take a long time to run
138
138
139 The only significant limitation that the Notebook currently has, compared to the Qt console, is that it cannot run any code that expects input from the kernel (such as scripts that call :func:`raw_input`). Very importantly, this means that the ``%debug`` magic does *not* currently work in the notebook!
139 The only significant limitation that the Notebook currently has, compared to the Qt console, is that it cannot run any code that expects input from the kernel (such as scripts that call :func:`raw_input`). Very importantly, this means that the ``%debug`` magic does *not* currently work in the notebook!
140
140
141 This limitation will be overcome in the future, but in the meantime, there is a simple solution for debugging: you can attach a Qt console to your existing notebook kernel, and run ``%debug`` from the Qt console.
141 This limitation will be overcome in the future, but in the meantime, there is a simple solution for debugging: you can attach a Qt console to your existing notebook kernel, and run ``%debug`` from the Qt console.
142 If your notebook is running on a local computer (i.e. if you are accessing it via your localhost address at ``127.0.0.1``), then you can just type
142 If your notebook is running on a local computer (i.e. if you are accessing it via your localhost address at ``127.0.0.1``), then you can just type
143 ``%qtconsole`` in the notebook and a Qt console will open up, connected to that same kernel.
143 ``%qtconsole`` in the notebook and a Qt console will open up, connected to that same kernel.
144
144
145 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.
145 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.
146 After a kernel restart, it as if the notebook were loaded afresh.
146 After a kernel restart, it as if the notebook were loaded afresh.
147
147
148
148
149 A notebook may be downloaded in either ``.ipynb`` or raw ``.py`` form from the menu option ``File | Download as``.
149 A notebook may be downloaded in either ``.ipynb`` or raw ``.py`` form from the menu option ``File | Download as``.
150 Choosing the ``.py`` option downloads a Python ``.py`` script, in which all output has been removed and the content of Markdown cells
150 Choosing the ``.py`` option downloads a Python ``.py`` script, in which all output has been removed and the content of Markdown cells
151 in comment areas. See ref:`below <notebook_format>` for more details on the
151 in comment areas. See ref:`below <notebook_format>` for more details on the
152 notebook format.
152 notebook format.
153
153
154
154
155 .. warning::
155 .. warning::
156
156
157 While in simple cases you can "roundtrip" a notebook to Python, edit the
157 While in simple cases you can "roundtrip" a notebook to Python, edit the
158 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
158 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
159 saved in the notebook that may not be saved to the ``.py`` format. And as
159 saved in the notebook that may not be saved to the ``.py`` format. And as
160 the notebook format evolves in complexity, there will be attributes of the
160 the notebook format evolves in complexity, there will be attributes of the
161 notebook that will not survive a roundtrip through the Python form. You
161 notebook that will not survive a roundtrip through the Python form. You
162 should think of the Python format as a way to output a script version of a
162 should think of the Python format as a way to output a script version of a
163 notebook and the import capabilities as a way to load existing code to get a
163 notebook and the import capabilities as a way to load existing code to get a
164 notebook started. But the Python version is *not* an alternate notebook
164 notebook started. But the Python version is *not* an alternate notebook
165 format.
165 format.
166
166
167
167
168 Keyboard shortcuts
168 Keyboard shortcuts
169 ------------------
169 ------------------
170 All actions in the notebook can be achieved with the mouse, but
170 All actions in the notebook can be achieved with the mouse, but
171 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:
171 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:
172
172
173 * :kbd:`Shift-Enter`:
173 * :kbd:`Shift-Enter`:
174 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).
174 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).
175
175
176 * :kbd:`Ctrl-Enter`:
176 * :kbd:`Ctrl-Enter`:
177 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.
177 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.
178
178
179 * :kbd:`Alt-Enter`:
179 * :kbd:`Alt-Enter`:
180 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`.
180 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`.
181 (:kbd:`Ctrl-m a` adds a new cell below the current one.)
181 (:kbd:`Ctrl-m a` adds a new cell below the current one.)
182
182
183
183
184
184
185 * :kbd:`Ctrl-m`:
185 * :kbd:`Ctrl-m`:
186 This is the prefix for *all* other shortcuts, which consist of :kbd:`Ctrl-m` followed by a single letter or character. For example, if you type :kbd:`Ctrl-m h` (that is, the sole letter :kbd:`h` after :kbd:`Ctrl-m`), IPython will show you all the available keyboard shortcuts.
186 This is the prefix for *all* other shortcuts, which consist of :kbd:`Ctrl-m` followed by a single letter or character. For example, if you type :kbd:`Ctrl-m h` (that is, the sole letter :kbd:`h` after :kbd:`Ctrl-m`), IPython will show you all the available keyboard shortcuts.
187
187
188
188
189 Input cell types
189 Input cell types
190 ----------------
190 ----------------
191 Each IPython input cell has a *cell type*, of which there is a restricted number. The type of a cell may be set by using the cell type dropdown on the toolbar, or via the following keyboard shortcuts:
191 Each IPython input cell has a *cell type*, of which there is a restricted number. The type of a cell may be set by using the cell type dropdown on the toolbar, or via the following keyboard shortcuts:
192
192
193 * **code**: :kbd:`Ctrl-m y`
193 * **code**: :kbd:`Ctrl-m y`
194 * **markdown**: :kbd:`Ctrl-m m`
194 * **markdown**: :kbd:`Ctrl-m m`
195 * **raw**: :kbd:`Ctrl-m t`
195 * **raw**: :kbd:`Ctrl-m t`
196 * **heading**: :kbd:`Ctrl-m 1` - :kbd:`Ctrl-m 6`
196 * **heading**: :kbd:`Ctrl-m 1` - :kbd:`Ctrl-m 6`
197
197
198 Upon initial creation, each input cell is by default a code cell.
198 Upon initial creation, each input cell is by default a code cell.
199
199
200
200
201 Code cells
201 Code cells
202 ~~~~~~~~~~
202 ~~~~~~~~~~
203 A *code input cell* allows you to edit code inline within the cell, with full syntax highlighting and autocompletion/introspection. By default, the language associated to a code cell is Python, but other languages, such as ``julia`` and ``R``, can be handled using magic commands (see below).
203 A *code input cell* allows you to edit code inline within the cell, with full syntax highlighting and autocompletion/introspection. By default, the language associated to a code cell is Python, but other languages, such as ``julia`` and ``R``, can be handled using magic commands (see below).
204
204
205 When a code cell is executed with :kbd:`Shift-Enter`, the code that it contains is transparently exported and run in that language
205 When a code cell is executed with :kbd:`Shift-Enter`, the code that it contains is transparently exported and run in that language
206 (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
206 (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
207 *output*. If this output is of a textual nature, then it is placed into a numbered *output cell*.
207 *output*. If this output is of a textual nature, then it is placed into a numbered *output cell*.
208 However, many other possible forms of output are also possible, including
208 However, many other possible forms of output are also possible, including
209 ``matplotlib`` figures and HTML tables (as used, for example, in the
209 ``matplotlib`` figures and HTML tables (as used, for example, in the
210 ``pandas`` data analyis package). This is known as IPython's *rich display* capability.
210 ``pandas`` data analyis package). This is known as IPython's *rich display* capability.
211
211
212
212
213 Rich text using Markdown
213 Rich text using Markdown
214 ~~~~~~~~~~~~~~~~~~~~~~~~
214 ~~~~~~~~~~~~~~~~~~~~~~~~
215 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 with the Markdown language. The corresponding cells are called *Markdown input cells*. The Markdown language provides a simple way to perform this text markup, that is, to specify which parts of the text should be emphasized (italics), bold, form lists, etc.
215 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 with the Markdown language. The corresponding cells are called *Markdown input cells*. The Markdown language provides a simple way to perform this text markup, that is, to specify which parts of the text should be emphasized (italics), bold, form lists, etc.
216
216
217 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.
217 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.
218
218
219 Within Markdown cells, you can also include *mathematics* in a straightforward way, using standard 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
219 Within Markdown cells, you can also include *mathematics* in a straightforward way, using standard 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
220 ``\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.
220 ``\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.
221
221
222 Raw input cells
222 Raw input cells
223 ~~~~~~~~~~~~~~~
223 ~~~~~~~~~~~~~~~
224 *Raw* input cells provide a place in which you can put additional information which you do not want to evaluated by the Notebook. This can be used, for example, to include extra information that is needed when exporting to a certain format. The output after evaluating a raw cell is just a verbatim copy of the input.
224 *Raw* input cells provide a place in which you can put additional information which you do not want to evaluated by the Notebook. This can be used, for example, to include extra information that is needed when exporting to a certain format. The output after evaluating a raw cell is just a verbatim copy of the input.
225
225
226 Heading cells
226 Heading cells
227 ~~~~~~~~~~~~~
227 ~~~~~~~~~~~~~
228 You can provide a conceptual structure for your computational document as a whole using different levels of headings; there are 6 levels available, from level 1 (main title) down to level 6 (paragraph). These can be used later for constructing tables of contents, etc.
228 You can provide a conceptual structure for your computational document as a whole using different levels of headings; there are 6 levels available, from level 1 (main title) down to level 6 (paragraph). These can be used later for constructing tables of contents, etc.
229
229
230 As with Markdown cells, a heading input cell is replaced by a rich text rendering of the heading when the cell is executed.
230 As with Markdown cells, a heading input cell is replaced by a rich text rendering of the heading when the cell is executed.
231
231
232
232
233 Magic commands
233 Magic commands
234 ~~~~~~~~~~~~~~
234 ~~~~~~~~~~~~~~
235 Magic commands, or *magics*, are commands for controlling IPython itself.
235 Magic commands, or *magics*, are commands for controlling IPython itself.
236 They all begin with ``%`` and are entered into code input cells; the code cells are executed as usual with :kbd:`Shift-Enter`.
236 They all begin with ``%`` and are entered into code input cells; the code cells are executed as usual with :kbd:`Shift-Enter`.
237
237
238 The magic commands call special functions defined by IPython which manipulate the computational state in certain ways.
238 The magic commands call special functions defined by IPython which manipulate the computational state in certain ways.
239
239
240 There are two types of magics:
240 There are two types of magics:
241
241
242 **line magics**:
242 **line magics**:
243 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 if they were part of a standard code cell.
243 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 if they were part of a standard code cell.
244
244
245 **cell magics**:
245 **cell magics**:
246 These begin with ``%%`` and operate on the *entire* remaining contents of the code cell.
246 These begin with ``%%`` and operate on the *entire* remaining contents of the code cell.
247
247
248 Line magics
248 Line magics
249 ~~~~~~~~~~~
249 ~~~~~~~~~~~
250 Some of the available line magics are the following:
250 Some of the available line magics are the following:
251
251
252 * ``%load filename``:
252 * ``%load filename``:
253 Loads the contents of the file ``filename`` into a new code cell.
253 Loads the contents of the file ``filename`` into a new code cell.
254
254
255 * ``%timeit code``:
255 * ``%timeit code``:
256 A simple way to time how long the single line of code ``code`` takes to run
256 A simple way to time how long the single line of code ``code`` takes to run
257
257
258 * ``%config``:
258 * ``%config``:
259 Configuration of the IPython Notebook
259 Configuration of the IPython Notebook
260
260
261 * ``%lsmagic``:
261 * ``%lsmagic``:
262 Provides a list of all available magic commands
262 Provides a list of all available magic commands
263
263
264 Cell magics
264 Cell magics
265 ~~~~~~~~~~~
265 ~~~~~~~~~~~
266
266
267 * ``%%latex``:
267 * ``%%latex``:
268 Renders the entire contents of the cell in LaTeX, without needing to use explicit LaTeX delimiters.
268 Renders the entire contents of the cell in LaTeX, without needing to use explicit LaTeX delimiters.
269
269
270 * ``%%bash``:
270 * ``%%bash``:
271 The code cell is executed by sending it to be executed by ``bash``. The output of the ``bash`` commands is captured and displayed in the notebook.
271 The code cell is executed by sending it to be executed by ``bash``. The output of the ``bash`` commands is captured and displayed in the notebook.
272
272
273 * ``%%file filename``:
273 * ``%%file filename``:
274 Writes the contents of the cell to the file ``filename``.
274 Writes the contents of the cell to the file ``filename``.
275 **Caution**: The file is over-written without warning!
275 **Caution**: The file is over-written without warning!
276
276
277 * ``%%R``:
277 * ``%%R``:
278 Execute the contents of the cell using the R language.
278 Execute the contents of the cell using the R language.
279
279
280
280
281 Several of the cell magics provide functionality to manipulate the filesystem of a remote server to which you otherwise do not have access.
281 Several of the cell magics provide functionality to manipulate the filesystem of a remote server to which you otherwise do not have access.
282
282
283
283
284 Plotting
284 Plotting
285 --------
285 --------
286 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.
286 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.
287
287
288 To set this up, before any plotting is performed you must execute the
288 To set this up, before any plotting is performed you must execute the
289 ``%matplotlib`` magic command. This performs the necessary behind-the-scenes setup for IPython to work correctly hand in hand with ``matplotlib``; it does
289 ``%matplotlib`` magic command. This performs the necessary behind-the-scenes setup for IPython to work correctly hand in hand with ``matplotlib``; it does
290 *not*, however, actually execute any Python ``import`` commands, that is, no names are added to the namespace.
290 *not*, however, actually execute any Python ``import`` commands, that is, no names are added to the namespace.
291
291
292 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:
292 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:
293
293
294 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.
294 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.
295
295
296 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::
296 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::
297
297
298 %matplotlib inline
298 %matplotlib inline
299
299
300 or::
300 or::
301
301
302 %pylab inline
302 %pylab inline
303
303
304 These instead capture the output of the plot and display it *inline* within the notebook format, below the input cell that produced it. The resulting plots will then also be stored in the notebook document.
304 These instead capture the output of the plot and display it *inline* within the notebook format, below the input cell that produced it. The resulting plots will then also be stored in the notebook document.
305
305
306
306
307 Converting notebooks to other formats
307 Converting notebooks to other formats
308 -------------------------------------
308 -------------------------------------
309 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.
309 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.
310
310
311 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::
311 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::
312
312
313 $ ipython nbconvert --format=FORMAT notebook.ipynb
313 $ ipython nbconvert --format=FORMAT notebook.ipynb
314
314
315 This will convert the IPython document file ``notebook.ipynb`` into the output format given by the ``FORMAT`` string.
315 This will convert the IPython document file ``notebook.ipynb`` into the output format given by the ``FORMAT`` string.
316
316
317 The default output format is HTML, for which the `--format`` modifier is not actually required::
317 The default output format is HTML, for which the `--format`` modifier is not actually required::
318
318
319 $ ipython nbconvert notebook.ipynb
319 $ ipython nbconvert notebook.ipynb
320
320
321 The currently supported export formats are the following:
321 The currently supported export formats are the following:
322
322
323 * HTML:
323 * HTML:
324
324
325 - ``full_html``:
325 - ``full_html``:
326 Standard HTML
326 Standard HTML
327
327
328 - ``simple_html``:
328 - ``simple_html``:
329 Simplified HTML
329 Simplified HTML
330
330
331 - ``reveal``:
331 - ``reveal``:
332 HTML slideshow presentation for use with the ``reveal.js`` package
332 HTML slideshow presentation for use with the ``reveal.js`` package
333
333
334 * PDF:
334 * PDF:
335
335
336 - ``sphinx_howto``:
336 - ``sphinx_howto``:
337 The format for Sphinx HOWTOs; similar to an ``article`` in LaTeX
337 The format for Sphinx HOWTOs; similar to an ``article`` in LaTeX
338
338
339 - ``sphinx_manual``:
339 - ``sphinx_manual``:
340 The format for Sphinx manuals; similar to a `book`` in LaTeX
340 The format for Sphinx manuals; similar to a `book`` in LaTeX
341
341
342 - ``latex``:
342 - ``latex``:
343 An article formatted completely using LaTeX
343 An article formatted completely using LaTeX
344
344
345 * Markup:
345 * Markup:
346
346
347 - ``rst``:
347 - ``rst``:
348 reStructuredText markup
348 reStructuredText markup
349
349
350 - ``markdown``:
350 - ``markdown``:
351 Markdown markup
351 Markdown markup
352
352
353 * Python:
353 * Python:
354
354
355 Comments out all the non-Python code to produce a standard Python ``.py`` script with just the code content.
355 Comments out all the non-Python code to produce a standard Python ``.py`` script with just the code content.
356
356
357 The files output by ``nbconvert` are currently placed in a new subdirectory called ``nbconvert_build``.
357 The files output by ``nbconvert` are currently placed in a new subdirectory called ``nbconvert_build``.
358
358
359 Each of the options for PDF export produces as an intermediate step 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.
359 Each of the options for PDF export produces as an intermediate step 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.
360
360
361 To actually produce the final PDF file, change into the ``nbconvert_build`` directory and run the following command::
361 To actually produce the final PDF file, run the following commands::
362
362
363 $ cd nbconvert_build
363 $ pdflatex notebook
364 $ pdflatex notebook
364
365
365 This requires a local installation of LaTeX on your machine.
366 This requires a local installation of LaTeX on your machine;
366 It produes the output PDF file ``notebook.pdf``, also inside the
367 it outputs the PDF file ``notebook.pdf``, also inside the
367 ``nbconvert_build`` subdirectory.
368 ``nbconvert_build`` subdirectory.
368
369
369 Alternatively, the output may be piped to standard output, ``stdout``, with::
370 Alternatively, the output may be piped to standard output, ``stdout``, with::
370
371
371 $ ipython nbconvert mynotebook.ipynb --stdout
372 $ ipython nbconvert mynotebook.ipynb --stdout
372
373
373 Multiple notebooks can be specified from the command line::
374 Multiple notebooks can be specified from the command line::
374
375
375 $ ipython nbconvert notebook*.ipynb
376 $ ipython nbconvert notebook*.ipynb
376 $ ipython nbconvert notebook1.ipynb notebook2.ipynb
377 $ ipython nbconvert notebook1.ipynb notebook2.ipynb
377
378
378 or via a list in a configuration file, say ``mycfg.py``, containing the text::
379 or via a list in a configuration file, say ``mycfg.py``, containing the text::
379
380
380 c = get_config()
381 c = get_config()
381 c.NbConvertApp.notebooks = ["notebook1.ipynb", "notebook2.ipynb"]
382 c.NbConvertApp.notebooks = ["notebook1.ipynb", "notebook2.ipynb"]
382
383
383 and using the command::
384 and using the command::
384
385
385 $ ipython nbconvert --config mycfg.py
386 $ ipython nbconvert --config mycfg.py
386
387
387
388
388 Configuring the IPython Notebook
389 Configuring the IPython Notebook
389 --------------------------------
390 --------------------------------
390 The IPython Notebook can be run with a variety of command line arguments.
391 The IPython Notebook can be run with a variety of command line arguments.
391 To see a list of available options enter::
392 To see a list of available options enter::
392
393
393 $ ipython notebook --help
394 $ ipython notebook --help
394
395
395 Defaults for these options can also be set by creating a file named
396 Defaults for these options can also be set by creating a file named
396 ``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::
397 ``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::
397
398
398 $ ipython locate
399 $ ipython locate
399
400
400 To create a new set of default configuration files, with lots of information on available options, use::
401 To create a new set of default configuration files, with lots of information on available options, use::
401
402
402 $ ipython profile create
403 $ ipython profile create
403
404
404 .. seealso:
405 .. seealso:
405
406
406 :ref:`config_overview`, in particular :ref:`Profiles`.
407 :ref:`config_overview`, in particular :ref:`Profiles`.
407
408
408
409
409 Extracting standard Python files from notebooks
410 Extracting standard Python files from notebooks
410 -----------------------------------------------
411 -----------------------------------------------
411 ``.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 imported with
412 ``.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 imported with
412 ``import`` in Python, nor run as a standard Python script.
413 ``import`` in Python, nor run as a standard Python script.
413
414
414 To extract just the Python code from within a notebook document, one option is to use ``ipython nbconvert``, as described above.
415 To extract just the Python code from within a notebook document, one option is to use ``ipython nbconvert``, as described above.
415 An alternative is to pass an argument to the IPython Notebook, from the moment when it is originally started, specifying that whenever it saves an ``.ipynb`` notebook document, it should, at the same time, save the corresponding standard ``.py`` script. To do so, you can execute the following command::
416 An alternative is to pass an argument to the IPython Notebook, from the moment when it is originally started, specifying that whenever it saves an ``.ipynb`` notebook document, it should, at the same time, save the corresponding standard ``.py`` script. To do so, you can execute the following command::
416
417
417 ipython notebook --script
418 ipython notebook --script
418
419
419 or you can set this option permanently in your configuration file with::
420 or you can set this option permanently in your configuration file with::
421
420 c = get_config()
422 c = get_config()
421 c.NotebookManager.save_script=True
423 c.NotebookManager.save_script=True
422
424
423 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 the raw code you have typed is exported you must avoid using syntax such as IPython magics and other IPython-specific extensions to the language for the files to be able to be successfully imported.
425 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 the raw code you have typed is exported you must avoid using syntax such as IPython magics and other IPython-specific extensions to the language for the files to be able to be successfully imported.
424
426
425 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::
427 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::
426
428
427 if __name__ == '__main__'
429 if __name__ == '__main__'
428
430
429 # rest of the code...
431 # rest of the code...
430
432
431 Since all cells in the notebook are run as top-level code, you will need to
433 Since all cells in the notebook are run as top-level code, you will need to
432 similarly protect *all* cells that you do not want executed when other scripts
434 similarly protect *all* cells that you do not want executed when other scripts
433 try to import your notebook. A convenient shortand for this is to define early
435 try to import your notebook. A convenient shortand for this is to define early
434 on::
436 on::
435
437
436 script = __name__ == '__main__'
438 script = __name__ == '__main__'
437
439
438 Then in any cell that you need to protect, use::
440 Then in any cell that you need to protect, use::
439
441
440 if script:
442 if script:
441 # rest of the cell...
443 # rest of the cell...
442
444
443
445
444 .. _notebook_security:
446 .. _notebook_security:
445
447
446 Security
448 Security
447 --------
449 --------
448
450
449 You can protect your Notebook server with a simple single password by
451 You can protect your Notebook server with a simple single password by
450 setting the :attr:`NotebookApp.password` configurable. You can prepare a
452 setting the :attr:`NotebookApp.password` configurable. You can prepare a
451 hashed password using the function :func:`IPython.lib.security.passwd`:
453 hashed password using the function :func:`IPython.lib.security.passwd`:
452
454
453 .. sourcecode:: ipython
455 .. sourcecode:: ipython
454
456
455 In [1]: from IPython.lib import passwd
457 In [1]: from IPython.lib import passwd
456 In [2]: passwd()
458 In [2]: passwd()
457 Enter password:
459 Enter password:
458 Verify password:
460 Verify password:
459 Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
461 Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
460
462
461 .. note::
463 .. note::
462
464
463 :func:`~IPython.lib.security.passwd` can also take the password as a string
465 :func:`~IPython.lib.security.passwd` can also take the password as a string
464 argument. **Do not** pass it as an argument inside an IPython session, as it
466 argument. **Do not** pass it as an argument inside an IPython session, as it
465 will be saved in your input history.
467 will be saved in your input history.
466
468
467 You can then add this to your :file:`ipython_notebook_config.py`, e.g.::
469 You can then add this to your :file:`ipython_notebook_config.py`, e.g.::
468
470
469 # Password to use for web authentication
471 # Password to use for web authentication
470 c = get_config()
472 c = get_config()
471 c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
473 c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
472
474
473 When using a password, it is a good idea to also use SSL, so that your password
475 When using a password, it is a good idea to also use SSL, so that your password
474 is not sent unencrypted by your browser. You can start the notebook to
476 is not sent unencrypted by your browser. You can start the notebook to
475 communicate via a secure protocol mode using a self-signed certificate with the command::
477 communicate via a secure protocol mode using a self-signed certificate with the command::
476
478
477 $ ipython notebook --certfile=mycert.pem
479 $ ipython notebook --certfile=mycert.pem
478
480
479 .. note::
481 .. note::
480
482
481 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::
483 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::
482
484
483 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
485 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
484
486
485 Your browser will warn you of a dangerous certificate because it is
487 Your browser will warn you of a dangerous certificate because it is
486 self-signed. If you want to have a fully compliant certificate that will not
488 self-signed. If you want to have a fully compliant certificate that will not
487 raise warnings, it is possible (but rather involved) to obtain one,
489 raise warnings, it is possible (but rather involved) to obtain one,
488 `as explained in detailed in this tutorial`__.
490 `as explained in detailed in this tutorial`__.
489
491
490 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars
492 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars
491
493
492 Keep in mind that when you enable SSL support, you will need to access the
494 Keep in mind that when you enable SSL support, you will need to access the
493 notebook server over ``https://``, not over plain ``http://``. The startup
495 notebook server over ``https://``, not over plain ``http://``. The startup
494 message from the server prints this, but it is easy to overlook and think the
496 message from the server prints this, but it is easy to overlook and think the
495 server is for some reason non-responsive.
497 server is for some reason non-responsive.
496
498
497
499
498 Connecting to an existing kernel
500 Connecting to an existing kernel
499 ---------------------------------
501 ---------------------------------
500
502
501 The notebook server always prints to the terminal the full details of
503 The notebook server always prints to the terminal the full details of
502 how to connect to each kernel, with messages such as the following::
504 how to connect to each kernel, with messages such as the following::
503
505
504 [IPKernelApp] To connect another client to this kernel, use:
506 [IPKernelApp] To connect another client to this kernel, use:
505 [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
507 [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
506
508
507 This long string is the name of a JSON file that contains all the port and
509 This long string is the name of a JSON file that contains all the port and
508 validation information necessary to connect to the kernel. You can then, for example, manually start a Qt console connected to the *same* kernel with::
510 validation information necessary to connect to the kernel. You can then, for example, manually start a Qt console connected to the *same* kernel with::
509
511
510 $ ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
512 $ ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
511
513
512 If you have only a single kernel running, simply typing::
514 If you have only a single kernel running, simply typing::
513
515
514 $ ipython qtconsole --existing
516 $ ipython qtconsole --existing
515
517
516 will automatically find it. (It will always find the most recently
518 will automatically find it. (It will always find the most recently
517 started kernel if there is more than one.) You can also request this
519 started kernel if there is more than one.) You can also request this
518 connection data by typing ``%connect_info``; this will print the same
520 connection data by typing ``%connect_info``; this will print the same
519 file information as well as the content of the JSON data structure it contains.
521 file information as well as the content of the JSON data structure it contains.
520
522
521
523
522 Running a public notebook server
524 Running a public notebook server
523 --------------------------------
525 --------------------------------
524
526
525 If you want to access your notebook server remotely via a web browser,
527 If you want to access your notebook server remotely via a web browser,
526 you can do the following.
528 you can do the following.
527
529
528 Start by creating a certificate file and a hashed password, as explained above. Then create a custom profile for the notebook, with the following command line, type::
530 Start by creating a certificate file and a hashed password, as explained above. Then create a custom profile for the notebook, with the following command line, type::
529
531
530 $ ipython profile create nbserver
532 $ ipython profile create nbserver
531
533
532 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::
534 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::
533
535
534 c = get_config()
536 c = get_config()
535
537
536 # Kernel config
538 # Kernel config
537 c.IPKernelApp.pylab = 'inline' # if you want plotting support always
539 c.IPKernelApp.pylab = 'inline' # if you want plotting support always
538
540
539 # Notebook config
541 # Notebook config
540 c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
542 c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
541 c.NotebookApp.ip = '*'
543 c.NotebookApp.ip = '*'
542 c.NotebookApp.open_browser = False
544 c.NotebookApp.open_browser = False
543 c.NotebookApp.password = u'sha1:bcd259ccf...[your hashed password here]'
545 c.NotebookApp.password = u'sha1:bcd259ccf...[your hashed password here]'
544 # It is a good idea to put it on a known, fixed port
546 # It is a good idea to put it on a known, fixed port
545 c.NotebookApp.port = 9999
547 c.NotebookApp.port = 9999
546
548
547 You can then start the notebook and access it later by pointing your browser to
549 You can then start the notebook and access it later by pointing your browser to
548 ``https://your.host.com:9999`` with ``ipython notebook --profile=nbserver``.
550 ``https://your.host.com:9999`` with ``ipython notebook --profile=nbserver``.
549
551
550 Running with a different URL prefix
552 Running with a different URL prefix
551 -----------------------------------
553 -----------------------------------
552
554
553 The notebook dashboard (the landing page with an overview
555 The notebook dashboard (the landing page with an overview
554 of the notebooks in your working directory) typically lives at the URL
556 of the notebooks in your working directory) typically lives at the URL
555 ``http://localhost:8888/``. If you prefer that it lives, together with the rest of the notebook, under a sub-directory,
557 ``http://localhost:8888/``. If you prefer that it lives, together with the rest of the notebook, under a sub-directory,
556 e.g. ``http://localhost:8888/ipython/``, you can do so with
558 e.g. ``http://localhost:8888/ipython/``, you can do so with
557 configuration options like the following (see above for instructions about
559 configuration options like the following (see above for instructions about
558 modifying ``ipython_notebook_config.py``)::
560 modifying ``ipython_notebook_config.py``)::
559
561
560 c.NotebookApp.base_project_url = '/ipython/'
562 c.NotebookApp.base_project_url = '/ipython/'
561 c.NotebookApp.base_kernel_url = '/ipython/'
563 c.NotebookApp.base_kernel_url = '/ipython/'
562 c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'}
564 c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'}
563
565
564 Using a different notebook store
566 Using a different notebook store
565 --------------------------------
567 --------------------------------
566
568
567 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
569 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
568 store the notebooks in a different format.
570 store the notebooks in a different format.
569
571
570 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
572 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
571 ``ipython_notebook_config.py`` file::
573 ``ipython_notebook_config.py`` file::
572
574
573 c.NotebookApp.notebook_manager_class = 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager'
575 c.NotebookApp.notebook_manager_class = 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager'
574 c.AzureNotebookManager.account_name = u'paste_your_account_name_here'
576 c.AzureNotebookManager.account_name = u'paste_your_account_name_here'
575 c.AzureNotebookManager.account_key = u'paste_your_account_key_here'
577 c.AzureNotebookManager.account_key = u'paste_your_account_key_here'
576 c.AzureNotebookManager.container = u'notebooks'
578 c.AzureNotebookManager.container = u'notebooks'
577
579
578 In addition to providing your Azure Blob Storage account name and key, you will
580 In addition to providing your Azure Blob Storage account name and key, you will
579 have to provide a container name; you can use multiple containers to organize
581 have to provide a container name; you can use multiple containers to organize
580 your notebooks.
582 your notebooks.
581
583
582 .. _notebook_format:
584 .. _notebook_format:
583
585
584 Notebook JSON file format
586 Notebook JSON file format
585 -------------------------
587 -------------------------
586 Notebook documents are JSON files with an ``.ipynb`` extension, formatted
588 Notebook documents are JSON files with an ``.ipynb`` extension, formatted
587 as legibly as possible with minimal extra indentation and cell content broken
589 as legibly as possible with minimal extra indentation and cell content broken
588 across lines to make them reasonably friendly to use in version-control
590 across lines to make them reasonably friendly to use in version-control
589 workflows. You should be very careful if you ever manually edit this JSON
591 workflows. You should be very careful if you ever manually edit this JSON
590 data, as it is extremely easy to corrupt its internal structure and make the
592 data, as it is extremely easy to corrupt its internal structure and make the
591 file impossible to load. In general, you should consider the notebook as a
593 file impossible to load. In general, you should consider the notebook as a
592 file meant only to be edited by the IPython Notebook app itself, not for hand-editing.
594 file meant only to be edited by the IPython Notebook app itself, not for hand-editing.
593
595
594 .. note::
596 .. note::
595
597
596 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; a ``diff`` of binary data is also 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 concern.
598 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; a ``diff`` of binary data is also 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 concern.
597
599
598 The notebook server can also generate a pure Python version of your notebook,
600 The notebook server can also generate a pure Python version of your notebook,
599 using the ``File | Download as`` menu option. The resulting ``.py`` file will
601 using the ``File | Download as`` menu option. The resulting ``.py`` file will
600 contain all the code cells from your notebook verbatim, and all Markdown cells
602 contain all the code cells from your notebook verbatim, and all Markdown cells
601 prepended with a comment marker. The separation between code and Markdown
603 prepended with a comment marker. The separation between code and Markdown
602 cells is indicated with special comments and there is a header indicating the
604 cells is indicated with special comments and there is a header indicating the
603 format version. All output is removed when exporting to Python.
605 format version. All output is removed when exporting to Python.
604
606
605 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 corresponding output.
607 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 corresponding output.
606
608
607 The contents of the notebook document ``simple.ipynb`` is the following JSON container::
609 The contents of the notebook document ``simple.ipynb`` is the following JSON container::
608
610
609 {
611 {
610 "metadata": {
612 "metadata": {
611 "name": "simple"
613 "name": "simple"
612 },
614 },
613 "nbformat": 3,
615 "nbformat": 3,
614 "nbformat_minor": 0,
616 "nbformat_minor": 0,
615 "worksheets": [
617 "worksheets": [
616 {
618 {
617 "cells": [
619 "cells": [
618 {
620 {
619 "cell_type": "markdown",
621 "cell_type": "markdown",
620 "metadata": {},
622 "metadata": {},
621 "source": "The simplest notebook."
623 "source": "The simplest notebook."
622 },
624 },
623 {
625 {
624 "cell_type": "code",
626 "cell_type": "code",
625 "collapsed": false,
627 "collapsed": false,
626 "input": "print \"Hello, IPython\"",
628 "input": "print \"Hello, IPython\"",
627 "language": "python",
629 "language": "python",
628 "metadata": {},
630 "metadata": {},
629 "outputs": [
631 "outputs": [
630 {
632 {
631 "output_type": "stream",
633 "output_type": "stream",
632 "stream": "stdout",
634 "stream": "stdout",
633 "text": "Hello, IPython\n"
635 "text": "Hello, IPython\n"
634 }
636 }
635 ],
637 ],
636 "prompt_number": 1
638 "prompt_number": 1
637 }
639 }
638 ],
640 ],
639 "metadata": {}
641 "metadata": {}
640 }
642 }
641 ]
643 ]
642 }
644 }
643
645
644
646
645 The corresponding Python script is::
647 The corresponding Python script is::
646
648
647 # -*- coding: utf-8 -*-
649 # -*- coding: utf-8 -*-
648 # <nbformat>3.0</nbformat>
650 # <nbformat>3.0</nbformat>
649
651
650 # <markdowncell>
652 # <markdowncell>
651
653
652 # The simplest notebook.
654 # The simplest notebook.
653
655
654 # <codecell>
656 # <codecell>
655
657
656 print "Hello, IPython"
658 print "Hello, IPython"
657
659
658 Note that indeed the output of the code cell, which is present in the JSON container, has been removed in the ``.py`` script.
660 Note that indeed the output of the code cell, which is present in the JSON container, has been removed in the ``.py`` script.
659
661
660
662
661 Known issues
663 Known issues
662 ------------
664 ------------
663
665
664 When behind a proxy, especially if your system or browser is set to autodetect
666 When behind a proxy, especially if your system or browser is set to autodetect
665 the proxy, the Notebook app might fail to connect to the server's websockets,
667 the proxy, the Notebook app might fail to connect to the server's websockets,
666 and present you with a warning at startup. In this case, you need to configure
668 and present you with a warning at startup. In this case, you need to configure
667 your system not to use the proxy for the server's address.
669 your system not to use the proxy for the server's address.
668
670
669 For example, in Firefox, go to the Preferences panel, Advanced section,
671 For example, in Firefox, go to the Preferences panel, Advanced section,
670 Network tab, click 'Settings...', and add the address of the notebook server
672 Network tab, click 'Settings...', and add the address of the notebook server
671 to the 'No proxy for' field.
673 to the 'No proxy for' field.
672
674
673
675
674 .. _Markdown: http://daringfireball.net/projects/markdown/basics
676 .. _Markdown: http://daringfireball.net/projects/markdown/basics
General Comments 0
You need to be logged in to leave comments. Login now