##// END OF EJS Templates
Magic commands section
David P. Sanders -
Show More
@@ -1,532 +1,541 b''
1 .. _htmlnotebook:
1 .. _htmlnotebook:
2
2
3 The IPython Notebook
3 The IPython Notebook
4 ====================
4 ====================
5
5
6 .. seealso::
6 .. seealso::
7
7
8 :ref:`Installation requirements <installnotebook>` for the Notebook.
8 :ref:`Installation requirements <installnotebook>` for the Notebook.
9
9
10 The IPython Notebook combines two components:
10 The IPython Notebook combines two components:
11
11
12 * A web application, called the *IPython Notebook web app*, 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.
12 * A web application, called the *IPython Notebook web app*, 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.
13
13
14 * Plain text documents, called *notebook documents*, or *notebooks*, for recording and distributing the results of the rich computations.
14 * Plain text documents, called *notebook documents*, or *notebooks*, for recording and distributing the results of the rich computations.
15
15
16 In the documentation, the distinction between the *N*otebook app and *n*otebook documents is made by capitalization.
16 In the documentation, the distinction between the *N*otebook app and *n*otebook documents is made by capitalization.
17
17
18 The Notebook app automatically saves the current state of the computation in the web browser to the corresponding notebook document.
18 The Notebook app automatically saves the current state of the computation in the web browser to the corresponding notebook document.
19
19
20 It is also common to refer to the current state of the computation, as represented by the sequence of input cells in the Notebook app, as a
20 It is also common to refer to the current state of the computation, as represented by the sequence of input cells in the Notebook app, as a
21 *notebook*. There is no problem with confounding these two concepts, since
21 *notebook*. There is no problem with confounding these two concepts, since
22 there is actually a one-to-one correspondence between what you see on the
22 there is actually a one-to-one correspondence between what you see on the
23 screen inside the app, and what is stored in the corresponding ``.ipynb`` notebook document.
23 screen inside the app, and what is stored in the corresponding ``.ipynb`` notebook document.
24
24
25
25
26
26
27 Features of the IPython Notebook web app
27 Features of the IPython Notebook web app
28 ----------------------------------------
28 ----------------------------------------
29
29
30 Some of the main
30 Some of the main
31 features of the IPython Notebook app include:
31 features of the IPython Notebook app include:
32
32
33 * Display rich data representations (e.g. HTML / LaTeX / SVG) in the browser as a result of computations.
33 * Display rich data representations (e.g. HTML / LaTeX / SVG) in the browser as a result of computations.
34 * Compose text cells using Markdown and HTML.
34 * Compose text cells using Markdown and HTML.
35 * Include mathematical equations, rendered directly in the browser by MathJax.
35 * Include mathematical equations, rendered directly in the browser by MathJax.
36 * Import standard Python scripts
36 * Import standard Python scripts
37 * In-browser editing, syntax highlighting, tab completion and autoindentation.
37 * In-browser editing, syntax highlighting, tab completion and autoindentation.
38 * Inline figures rendered by the ``matplotlib`` library with publication quality, in a range of formats (SVG / PDF / PNG).
38 * Inline figures rendered by the ``matplotlib`` library with publication quality, in a range of formats (SVG / PDF / PNG).
39
39
40 If you have ever used the Mathematica or SAGE notebooks (the latter is also
40 If you have ever used the Mathematica or SAGE notebooks (the latter is also
41 web-based__) you should feel right at home. If you have not, you will be
41 web-based__) you should feel right at home. If you have not, you will be
42 able to learn how to use the IPython Notebook in just a few minutes.
42 able to learn how to use the IPython Notebook in just a few minutes.
43
43
44 .. __: http://sagenb.org
44 .. __: http://sagenb.org
45
45
46
46
47 Notebook documents
47 Notebook documents
48 ------------------
48 ------------------
49
49
50 Notebook document files are just standard text files with the extension
50 Notebook document files are just standard text files with the extension
51 ``.ipynb``, stored in the working directory on your computer. This file can be easily put under version control and shared with colleagues.
51 ``.ipynb``, stored in the working directory on your computer. This file can be easily put under version control and shared with colleagues.
52
52
53 Despite the fact that the notebook documents are plain text files, they use
53 Despite the fact that the notebook documents are plain text files, they use
54 the JSON format in order to store a *complete*, *reproducible* copy of the
54 the JSON format in order to store a *complete*, *reproducible* copy of the
55 state of the computation as it is inside the Notebook app.
55 state of the computation as it is inside the Notebook app.
56 That is, they record all computations carried out and the results obtained in a literate way; inputs and outputs of computations can be freely mixed with descriptive text, mathematics, and HTML 5 objects.
56 That is, they record all computations carried out and the results obtained in a literate way; inputs and outputs of computations can be freely mixed with descriptive text, mathematics, and HTML 5 objects.
57
57
58 Notebooks may easily be exported to a range of static formats, including HTML (for example, for blog posts), PDF and slide shows.
58 Notebooks may easily be exported to a range of static formats, including HTML (for example, for blog posts), PDF and slide shows.
59 Furthermore, any publicly
59 Furthermore, any publicly
60 available notebook may be shared via the `IPython Notebook Viewer
60 available notebook may be shared via the `IPython Notebook Viewer
61 <http://nbviewer.ipython.org>`_ service, which will provide it as a static web
61 <http://nbviewer.ipython.org>`_ service, which will provide it as a static web
62 page. The results may thus be shared without having to install anything.
62 page. The results may thus be shared without having to install anything.
63
63
64
64
65 See :ref:`our installation documentation <install_index>` for directions on
65 See :ref:`our installation documentation <install_index>` for directions on
66 how to install the notebook and its dependencies.
66 how to install the notebook and its dependencies.
67
67
68 .. note::
68 .. note::
69
69
70 You can start more than one notebook server at the same time, if you want to
70 You can start more than one notebook server at the same time, if you want to
71 work on notebooks in different directories. By default the first notebook
71 work on notebooks in different directories. By default the first notebook
72 server starts on port 8888, and later notebook servers search for ports near
72 server starts on port 8888, and later notebook servers search for ports near
73 that one. You can also manually specify the port with the ``--port``
73 that one. You can also manually specify the port with the ``--port``
74 option.
74 option.
75
75
76
76
77 Running the IPython Notebook web app
77 Running the IPython Notebook web app
78 ====================================
78 ====================================
79
79
80 The Notebook web app is started with the command::
80 The Notebook web app is started with the command::
81
81
82 $ ipython notebook
82 $ ipython notebook
83
83
84 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 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).
85 You can create new notebooks from the dashboard with the ``New Notebook``
85 You can create new notebooks from the dashboard with the ``New Notebook``
86 button, or open existing ones by clicking on their name.
86 button, or open existing ones by clicking on their name.
87 You can also drag and drop ``.ipynb`` notebooks and standard ``.py`` Python source code files into the notebook list area.
87 You can also drag and drop ``.ipynb`` notebooks and standard ``.py`` Python source code files into the notebook list area.
88
88
89 ``.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 ``.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
90 code in the ``.py`` file, which you can later manually partition into individual cells.
90 code in the ``.py`` file, which you can later manually partition into individual cells.
91
91
92 .. 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 .. 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.
93
93
94
94
95 The IPython Notebook web app is based on a server-client structure.
95 The IPython Notebook web app is based on a server-client structure.
96 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.
96 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.
97
97
98
98
99 When you open or create a new notebook, your browser tab will reflect the name of that notebook, prefixed with "IPy".
99 When you open or create a new notebook, your browser tab will reflect the name of that notebook, prefixed with "IPy".
100 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.
100 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.
101
101
102
102
103 Basic concepts in the Notebook app
103 Basic concepts in the Notebook app
104 ----------------------------------
104 ----------------------------------
105
105
106 When you finally start editing a notebook document in the Notebook, you will be presented with the title of the notebook, a *menu bar*, a *toolbar* and an empty *input cell*.
106 When you finally start editing a notebook document in the Notebook, you will be presented with the title of the notebook, a *menu bar*, a *toolbar* and an empty *input cell*.
107
107
108 Notebook title
108 Notebook title
109 ~~~~~~~~~~~~~~
109 ~~~~~~~~~~~~~~
110 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.
110 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
111
112 Menu bar
112 Menu bar
113 ~~~~~~~~
113 ~~~~~~~~
114 The menu bar presents different options that may be used to manipulate the way the Notebook functions.
114 The menu bar presents different options that may be used to manipulate the way the Notebook functions.
115
115
116 Toolbar
116 Toolbar
117 ~~~~~~~
117 ~~~~~~~
118 The tool bar gives handy icons for the most-used operations within the Notebook.
118 The tool bar gives handy icons for the most-used operations within the Notebook.
119
119
120
120
121 Input cells
121 Input cells
122 -----------
122 -----------
123 Input cells are the core of the functionality of the IPython Notebook.
123 Input cells are the core of the functionality of the IPython Notebook.
124 They are regions in the document where you can enter different types of text and commands. These regions are then executed using :kbd:`Shift-Enter`, at which point the Notebook executes the current input cell, displays the resulting output beneath it, and adds a new input cell below.
124 They are regions in the document where you can enter different types of text and commands. These regions are then executed using :kbd:`Shift-Enter`, at which point the Notebook executes the current input cell, displays the resulting output beneath it, and adds a new input cell below.
125
125
126 The notebook consists of a sequence of input cells,
126 The notebook consists of a sequence of input cells,
127 providing the means to direct the computational process.
127 providing the means to direct the computational process.
128
128
129
129
130 Basic workflow
130 Basic workflow
131 --------------
131 --------------
132 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
132 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 times until you obtain the desired results, rather than having to
133 times until you obtain the desired results, rather than having to
134 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,
134 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 organizing related pieces into cells and moving forward as previous
135 organizing related pieces into cells and moving forward as previous
136 parts work correctly. This is much more convenient for interactive exploration than breaking up a computation into scripts that must be
136 parts work correctly. This is much more convenient for interactive exploration than breaking up a computation into scripts that must be
137 executed together, especially if parts of them take a long time to run
137 executed together, 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
139 The only significant limitation that the notebook currently has, compared to the Qt console, is that it cannot run any code that
140 expects input from the kernel (such as scripts that call
140 expects input from the kernel (such as scripts that call
141 :func:`raw_input`). Very importantly, this means that the ``%debug``
141 :func:`raw_input`). Very importantly, this means that the ``%debug``
142 magic does *not* currently work in the notebook! This limitation will
142 magic does *not* currently work in the notebook! This limitation will
143 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.
143 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 If your notebook is running on a local
144 If your notebook is running on a local
145 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.
145 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
146
147
147
148 Cell types
148 Cell types
149 ----------
149 ----------
150 Each IPython input cell has a *cell type*.
150 Each IPython input cell has a *cell type*.
151 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:
151 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:
152
152
153 * code :kbd:`Ctrl-m y`
153 * code :kbd:`Ctrl-m y`
154 * markdown :kbd:`Ctrl-m m`
154 * markdown :kbd:`Ctrl-m m`
155 * raw :kbd:`Ctrl-m t`
155 * raw :kbd:`Ctrl-m t`
156 * heading :kbd:`Ctrl-m 1` - :kbd:`Ctrl-m 6`
156 * heading :kbd:`Ctrl-m 1` - :kbd:`Ctrl-m 6`
157
157
158
158
159 Code cells
159 Code cells
160 ~~~~~~~~~~
160 ~~~~~~~~~~
161 Code cells contain code written in some computer language, which is Python by default. When the cell is executed with :kbd:`Shift-Enter`, this code is executed, and the result returned by Python (or the corresponding language) after running the code will be displayed as its output.
161 Code cells contain code written in some computer language, which is Python by default. When the cell is executed with :kbd:`Shift-Enter`, this code is executed, and the result returned by Python (or the corresponding language) after running the code will be displayed as its output.
162
162
163 Code may be edited inline in the cell, with full syntax highlighting.
163 Code may be edited inline in the cell, with full syntax highlighting.
164
164
165
165
166 Rich text using Markdown
166 Rich text using Markdown
167 ~~~~~~~~~~~~~~~~~~~~~~~~
167 ~~~~~~~~~~~~~~~~~~~~~~~~
168 The computational process may be documented in a literate way using rich text.
168 The computational process may be documented in a literate way using rich text.
169 For this purpose, the Notebook provides markdown cells. Text is entered using Markdown_ syntax, allowing for italics, bold, ordered and unordered lists, etc. This is rendered using Markdown syntax to a rich HTML representation when the cell is executed. In this case, the output *replaces* the input cell.
169 For this purpose, the Notebook provides markdown cells. Text is entered using Markdown_ syntax, allowing for italics, bold, ordered and unordered lists, etc. This is rendered using Markdown syntax to a rich HTML representation when the cell is executed. In this case, the output *replaces* the input cell.
170
170
171 Within markdown cells, mathematics can be included in a straightforward manner using LaTeX notation: ``$...$`` for inline math and ``$$...$$`` for displayed math. Standard LaTeX environments, such as ``\begin{equation}...\end{equation}``, also work. New commands may be defined using standard LaTeX commands, placed anywhere in a markdown cell.
171 Within markdown cells, mathematics can be included in a straightforward manner using LaTeX notation: ``$...$`` for inline math and ``$$...$$`` for displayed math. Standard LaTeX environments, such as ``\begin{equation}...\end{equation}``, also work. New commands may be defined using standard LaTeX commands, placed anywhere in a markdown cell.
172
172
173 Raw cells
173 Raw cells
174 ~~~~~~~~~
174 ~~~~~~~~~
175 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 to be used when the notebook is exported to a certain format.
175 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 to be used when the notebook is exported to a certain format.
176
176
177
177
178 Plotting
178 Plotting
179 --------
179 --------
180
180 One major feature of the Notebook is the ability to capture the result of plots as inline output.
181 The Notebook allows
182
183 `%matplotlib` and `%pylab` magics
181 `%matplotlib` and `%pylab` magics
184
182
185 Inline versus non inline
183 Inline versus non inline
186
184
187 %config
185 %config
188
186
189
187
190 Magic commands
188 Magic commands
191 --------------
189 --------------
190 Magic commands, or *magics*, are one-word commands starting with `%`, which give commands to IPython itself (as opposed to standard Python commands which are exported to be run in a Python interpreter).
191
192 Magics control different elements of the way that the IPython notebook operates. They are entered into standard code cells and executed as usual with :kbd:`Shift-Enter`.
193
194 Some of the main magics are the following:
195
196 ``%lsmagic``
197 Lists all the magic commands available
198
199 See CROSS-REF for a complete list of magics
192
200
193
201
194 Exporting a notebook and importing existing scripts
202 Exporting a notebook and importing existing scripts
195 ---------------------------------------------------
203 ---------------------------------------------------
196
204
205 Need to talk about ipython nbconvert
206
197 If you want to provide others with a static HTML or PDF view of your notebook,
207 If you want to provide others with a static HTML or PDF view of your notebook,
198 use the ``Print`` button. This opens a static view of the document, which you
208 use the ``Print`` button. This opens a static view of the document, which you
199 can print to PDF using your operating system's facilities, or save to a file
209 can print to PDF using your operating system's facilities, or save to a file
200 with your web browser's 'Save' option (note that typically, this will create
210 with your web browser's 'Save' option (note that typically, this will create
201 both an html file *and* a directory called `notebook_name_files` next to it
211 both an html file *and* a directory called `notebook_name_files` next to it
202 that contains all the necessary style information, so if you intend to share
212 that contains all the necessary style information, so if you intend to share
203 this, you must send the directory along with the main html file).
213 this, you must send the directory along with the main html file).
204
214
205 The `Download` button lets you save a notebook file to the Download area
215 The `Download` button lets you save a notebook file to the Download area
206 configured by your web browser (particularly useful if you are running the
216 configured by your web browser (particularly useful if you are running the
207 notebook server on a remote host and need a file locally). The notebook is
217 notebook server on a remote host and need a file locally).
208 saved by default with the ``.ipynb`` extension and the files contain JSON data
218 But you can always export
209 that is not meant for human editing or consumption. But you can always export
210 the input part of a notebook to a plain python script by choosing Python format
219 the input part of a notebook to a plain python script by choosing Python format
211 in the `Download` drop list. This removes all output and saves the text cells
220 in the `Download` drop list. This removes all output and saves the text cells
212 in comment areas. See ref:`below <notebook_format>` for more details on the
221 in comment areas. See ref:`below <notebook_format>` for more details on the
213 notebook format.
222 notebook format.
214
223
215
224
216 .. warning::
225 .. warning::
217
226
218 While in simple cases you can roundtrip a notebook to Python, edit the
227 While in simple cases you can roundtrip a notebook to Python, edit the
219 python file and import it back without loss of main content, this is in
228 python file and import it back without loss of main content, this is in
220 general *not guaranteed to work at all*. First, there is extra metadata
229 general *not guaranteed to work at all*. First, there is extra metadata
221 saved in the notebook that may not be saved to the ``.py`` format. And as
230 saved in the notebook that may not be saved to the ``.py`` format. And as
222 the notebook format evolves in complexity, there will be attributes of the
231 the notebook format evolves in complexity, there will be attributes of the
223 notebook that will not survive a roundtrip through the Python form. You
232 notebook that will not survive a roundtrip through the Python form. You
224 should think of the Python format as a way to output a script version of a
233 should think of the Python format as a way to output a script version of a
225 notebook and the import capabilities as a way to load existing code to get a
234 notebook and the import capabilities as a way to load existing code to get a
226 notebook started. But the Python version is *not* an alternate notebook
235 notebook started. But the Python version is *not* an alternate notebook
227 format.
236 format.
228
237
229
238
230 Importing or executing a notebook as a normal Python file
239 Importing or executing a notebook as a normal Python file
231 ---------------------------------------------------------
240 ---------------------------------------------------------
232
241
233 The native format of the notebook, a file with a ``.ipynb`` `extension, is a
242 The native format of the notebook, a file with a ``.ipynb`` `extension, is a
234 JSON container of all the input and output of the notebook, and therefore not
243 JSON container of all the input and output of the notebook, and therefore not
235 valid Python by itself. This means that by default, you cannot directly
244 valid Python by itself. This means that by default, you cannot directly
236 import a notebook from Python, nor execute it as a normal python script.
245 import a notebook from Python, nor execute it as a normal python script.
237
246
238 But if you want to be able to use notebooks also as regular Python files, you can start the notebook server with::
247 But if you want to be able to use notebooks also as regular Python files, you can start the notebook server with::
239
248
240 ipython notebook --script
249 ipython notebook --script
241
250
242 or you can set this option permanently in your configuration file with::
251 or you can set this option permanently in your configuration file with::
243
252
244 c.NotebookManager.save_script=True
253 c.NotebookManager.save_script=True
245
254
246 This will instruct the notebook server to save the ``.py`` export of each
255 This will instruct the notebook server to save the ``.py`` export of each
247 notebook, in addition to the ``.ipynb``, at every save. These are standard ``.py`` files, and so they can be
256 notebook, in addition to the ``.ipynb``, at every save. These are standard ``.py`` files, and so they can be
248 ``%run``, imported from regular IPython sessions or other notebooks, or
257 ``%run``, imported from regular IPython sessions or other notebooks, or
249 executed at the command-line. Since we export the raw
258 executed at the command-line. Since we export the raw
250 code you have typed, for these files to be importable from other code you will
259 code you have typed, for these files to be importable from other code you will
251 have to avoid using syntax such as ``%magics`` and other IPython-specific
260 have to avoid using syntax such as ``%magics`` and other IPython-specific
252 extensions to the language.
261 extensions to the language.
253
262
254 In regular practice, the standard way to differentiate importable code from the
263 In regular practice, the standard way to differentiate importable code from the
255 'executable' part of a script is to put at the bottom::
264 'executable' part of a script is to put at the bottom::
256
265
257 if __name__ == '__main__':
266 if __name__ == '__main__':
258 # rest of the code...
267 # rest of the code...
259
268
260 Since all cells in the notebook are run as top-level code, you'll need to
269 Since all cells in the notebook are run as top-level code, you'll need to
261 similarly protect *all* cells that you do not want executed when other scripts
270 similarly protect *all* cells that you do not want executed when other scripts
262 try to import your notebook. A convenient shortand for this is to define early
271 try to import your notebook. A convenient shortand for this is to define early
263 on::
272 on::
264
273
265 script = __name__ == '__main__'
274 script = __name__ == '__main__'
266
275
267 and then on any cell that you need to protect, use::
276 and then on any cell that you need to protect, use::
268
277
269 if script:
278 if script:
270 # rest of the cell...
279 # rest of the cell...
271
280
272 Configuration
281 Configuration
273 -------------
282 -------------
274
283
275 The IPython notebook server can be run with a variety of command line arguments.
284 The IPython notebook server can be run with a variety of command line arguments.
276 To see a list of available options enter::
285 To see a list of available options enter::
277
286
278 $ ipython notebook --help
287 $ ipython notebook --help
279
288
280 Defaults for these options can also be set by creating a file named
289 Defaults for these options can also be set by creating a file named
281 ``ipython_notebook_config.py`` in your IPython profile folder. The profile folder is a subfolder of your IPython directory; ``ipython locate`` will show you where it is located.
290 ``ipython_notebook_config.py`` in your IPython profile folder. The profile folder is a subfolder of your IPython directory; ``ipython locate`` will show you where it is located.
282
291
283 To create a new set of default configuration files, with lots of information on available options, use::
292 To create a new set of default configuration files, with lots of information on available options, use::
284
293
285 $ ipython profile create
294 $ ipython profile create
286
295
287 .. seealso:
296 .. seealso:
288
297
289 :ref:`config_overview`, in particular :ref:`Profiles`.
298 :ref:`config_overview`, in particular :ref:`Profiles`.
290
299
291
300
292 Keyboard shortcuts
301 Keyboard shortcuts
293 ------------------
302 ------------------
294
303
295 All actions in the notebook can be achieved with the mouse, but we have also
304 All actions in the notebook can be achieved with the mouse, but we have also
296 added keyboard shortcuts for the most common ones, so that productive use of
305 added keyboard shortcuts for the most common ones, so that productive use of
297 the notebook can be achieved with minimal mouse intervention. The main
306 the notebook can be achieved with minimal mouse intervention. The main
298 key bindings you need to remember are:
307 key bindings you need to remember are:
299
308
300 * :kbd:`Shift-Enter`: execute the current cell (similar to the Qt console),
309 * :kbd:`Shift-Enter`: execute the current cell (similar to the Qt console),
301 show output (if any) and jump to the next cell below. If :kbd:`Shift-Enter`
310 show output (if any) and jump to the next cell below. If :kbd:`Shift-Enter`
302 was invoked on the last input line, a new code cell will also be created. Note
311 was invoked on the last input line, a new code cell will also be created. Note
303 that in the notebook, simply using :kbd:`Enter` *never* forces execution,
312 that in the notebook, simply using :kbd:`Enter` *never* forces execution,
304 it simply inserts a new line in the current cell. Therefore, in the notebook
313 it simply inserts a new line in the current cell. Therefore, in the notebook
305 you must always use :kbd:`Shift-Enter` to get execution (or use the mouse and
314 you must always use :kbd:`Shift-Enter` to get execution (or use the mouse and
306 click on the ``Run Selected`` button).
315 click on the ``Run Selected`` button).
307
316
308 * :kbd:`Alt-Enter`: this combination is similar to the previous one, with the
317 * :kbd:`Alt-Enter`: this combination is similar to the previous one, with the
309 exception that, if the next cell below is not empty, a new code cell will be
318 exception that, if the next cell below is not empty, a new code cell will be
310 added to the notebook, even if the cell execution happens not in the last cell.
319 added to the notebook, even if the cell execution happens not in the last cell.
311 In this regard, :kbd:`Alt-Enter`: is simply a shortcut for the :kbd:`Shift-Enter`,
320 In this regard, :kbd:`Alt-Enter`: is simply a shortcut for the :kbd:`Shift-Enter`,
312 :kbd:`Ctrl-m a` sequence.
321 :kbd:`Ctrl-m a` sequence.
313
322
314 * :kbd:`Ctrl-Enter`: execute the current cell in "terminal mode", where any
323 * :kbd:`Ctrl-Enter`: execute the current cell in "terminal mode", where any
315 output is shown but the cursor stays in the current cell, whose input
324 output is shown but the cursor stays in the current cell, whose input
316 area is flushed empty. This is convenient to do quick in-place experiments
325 area is flushed empty. This is convenient to do quick in-place experiments
317 or query things like filesystem content without creating additional cells you
326 or query things like filesystem content without creating additional cells you
318 may not want saved in your notebook.
327 may not want saved in your notebook.
319
328
320 * :kbd:`Ctrl-m`: this is the prefix for all other keybindings, which consist
329 * :kbd:`Ctrl-m`: this is the prefix for all other keybindings, which consist
321 of an additional single letter. Type :kbd:`Ctrl-m h` (that is, the sole
330 of an additional single letter. Type :kbd:`Ctrl-m h` (that is, the sole
322 letter :kbd:`h` after :kbd:`Ctrl-m`) and IPython will show you the remaining
331 letter :kbd:`h` after :kbd:`Ctrl-m`) and IPython will show you the remaining
323 available keybindings.
332 available keybindings.
324
333
325
334
326 .. _notebook_security:
335 .. _notebook_security:
327
336
328 Security
337 Security
329 ========
338 ========
330
339
331 You can protect your notebook server with a simple single-password by
340 You can protect your notebook server with a simple single-password by
332 setting the :attr:`NotebookApp.password` configurable. You can prepare a
341 setting the :attr:`NotebookApp.password` configurable. You can prepare a
333 hashed password using the function :func:`IPython.lib.security.passwd`:
342 hashed password using the function :func:`IPython.lib.security.passwd`:
334
343
335 .. sourcecode:: ipython
344 .. sourcecode:: ipython
336
345
337 In [1]: from IPython.lib import passwd
346 In [1]: from IPython.lib import passwd
338 In [2]: passwd()
347 In [2]: passwd()
339 Enter password:
348 Enter password:
340 Verify password:
349 Verify password:
341 Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
350 Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
342
351
343 .. note::
352 .. note::
344
353
345 :func:`~IPython.lib.security.passwd` can also take the password as a string
354 :func:`~IPython.lib.security.passwd` can also take the password as a string
346 argument. **Do not** pass it as an argument inside an IPython session, as it
355 argument. **Do not** pass it as an argument inside an IPython session, as it
347 will be saved in your input history.
356 will be saved in your input history.
348
357
349 You can then add this to your :file:`ipython_notebook_config.py`, e.g.::
358 You can then add this to your :file:`ipython_notebook_config.py`, e.g.::
350
359
351 # Password to use for web authentication
360 # Password to use for web authentication
352 c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
361 c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
353
362
354 When using a password, it is a good idea to also use SSL, so that your password
363 When using a password, it is a good idea to also use SSL, so that your password
355 is not sent unencrypted by your browser. You can start the notebook to
364 is not sent unencrypted by your browser. You can start the notebook to
356 communicate via a secure protocol mode using a self-signed certificate by
365 communicate via a secure protocol mode using a self-signed certificate by
357 typing::
366 typing::
358
367
359 $ ipython notebook --certfile=mycert.pem
368 $ ipython notebook --certfile=mycert.pem
360
369
361 .. note::
370 .. note::
362
371
363 A self-signed certificate can be generated with openssl. For example, the
372 A self-signed certificate can be generated with openssl. For example, the
364 following command will create a certificate valid for 365 days with both
373 following command will create a certificate valid for 365 days with both
365 the key and certificate data written to the same file::
374 the key and certificate data written to the same file::
366
375
367 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
376 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
368
377
369 Your browser will warn you of a dangerous certificate because it is
378 Your browser will warn you of a dangerous certificate because it is
370 self-signed. If you want to have a fully compliant certificate that will not
379 self-signed. If you want to have a fully compliant certificate that will not
371 raise warnings, it is possible (but rather involved) to obtain one for free,
380 raise warnings, it is possible (but rather involved) to obtain one for free,
372 `as explained in detailed in this tutorial`__.
381 `as explained in detailed in this tutorial`__.
373
382
374 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars
383 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars
375
384
376 Keep in mind that when you enable SSL support, you'll need to access the
385 Keep in mind that when you enable SSL support, you'll need to access the
377 notebook server over ``https://``, not over plain ``http://``. The startup
386 notebook server over ``https://``, not over plain ``http://``. The startup
378 message from the server prints this, but it's easy to overlook and think the
387 message from the server prints this, but it's easy to overlook and think the
379 server is for some reason non-responsive.
388 server is for some reason non-responsive.
380
389
381 Quick how to's
390 Quick how to's
382 ==============
391 ==============
383
392
384 Connecting to an existing kernel
393 Connecting to an existing kernel
385 ---------------------------------
394 ---------------------------------
386
395
387 The notebook server always prints to the terminal the full details of
396 The notebook server always prints to the terminal the full details of
388 how to connect to each kernel, with lines like::
397 how to connect to each kernel, with lines like::
389
398
390 [IPKernelApp] To connect another client to this kernel, use:
399 [IPKernelApp] To connect another client to this kernel, use:
391 [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
400 [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
392
401
393 This is the name of a JSON file that contains all the port and
402 This is the name of a JSON file that contains all the port and
394 validation information necessary to connect to the kernel. You can
403 validation information necessary to connect to the kernel. You can
395 manually start a Qt console with::
404 manually start a Qt console with::
396
405
397 ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
406 ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
398
407
399 and if you only have a single kernel running, simply typing::
408 and if you only have a single kernel running, simply typing::
400
409
401 ipython qtconsole --existing
410 ipython qtconsole --existing
402
411
403 will automatically find it (it will always find the most recently
412 will automatically find it (it will always find the most recently
404 started kernel if there is more than one). You can also request this
413 started kernel if there is more than one). You can also request this
405 connection data by typing ``%connect_info``; this will print the same
414 connection data by typing ``%connect_info``; this will print the same
406 file information as well as the content of the JSON data structure it contains.
415 file information as well as the content of the JSON data structure it contains.
407
416
408
417
409 Running a public notebook server
418 Running a public notebook server
410 --------------------------------
419 --------------------------------
411
420
412 If you want to access your notebook server remotely with just a web browser,
421 If you want to access your notebook server remotely with just a web browser,
413 here is a quick set of instructions. Start by creating a certificate file and
422 here is a quick set of instructions. Start by creating a certificate file and
414 a hashed password as explained above. Then, create a custom profile for the
423 a hashed password as explained above. Then, create a custom profile for the
415 notebook. At the command line, type::
424 notebook. At the command line, type::
416
425
417 ipython profile create nbserver
426 ipython profile create nbserver
418
427
419 In the profile directory, edit the file ``ipython_notebook_config.py``. By
428 In the profile directory, edit the file ``ipython_notebook_config.py``. By
420 default the file has all fields commented, the minimum set you need to
429 default the file has all fields commented, the minimum set you need to
421 uncomment and edit is here::
430 uncomment and edit is here::
422
431
423 c = get_config()
432 c = get_config()
424
433
425 # Kernel config
434 # Kernel config
426 c.IPKernelApp.pylab = 'inline' # if you want plotting support always
435 c.IPKernelApp.pylab = 'inline' # if you want plotting support always
427
436
428 # Notebook config
437 # Notebook config
429 c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
438 c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
430 c.NotebookApp.ip = '*'
439 c.NotebookApp.ip = '*'
431 c.NotebookApp.open_browser = False
440 c.NotebookApp.open_browser = False
432 c.NotebookApp.password = u'sha1:bcd259ccf...your hashed password here'
441 c.NotebookApp.password = u'sha1:bcd259ccf...your hashed password here'
433 # It's a good idea to put it on a known, fixed port
442 # It's a good idea to put it on a known, fixed port
434 c.NotebookApp.port = 9999
443 c.NotebookApp.port = 9999
435
444
436 You can then start the notebook and access it later by pointing your browser to
445 You can then start the notebook and access it later by pointing your browser to
437 ``https://your.host.com:9999`` with ``ipython notebook --profile=nbserver``.
446 ``https://your.host.com:9999`` with ``ipython notebook --profile=nbserver``.
438
447
439 Running with a different URL prefix
448 Running with a different URL prefix
440 -----------------------------------
449 -----------------------------------
441
450
442 The notebook dashboard (i.e. the default landing page with an overview
451 The notebook dashboard (i.e. the default landing page with an overview
443 of all your notebooks) typically lives at a URL path of
452 of all your notebooks) typically lives at a URL path of
444 "http://localhost:8888/". If you want to have it, and the rest of the
453 "http://localhost:8888/". If you want to have it, and the rest of the
445 notebook, live under a sub-directory,
454 notebook, live under a sub-directory,
446 e.g. "http://localhost:8888/ipython/", you can do so with
455 e.g. "http://localhost:8888/ipython/", you can do so with
447 configuration options like these (see above for instructions about
456 configuration options like these (see above for instructions about
448 modifying ``ipython_notebook_config.py``)::
457 modifying ``ipython_notebook_config.py``)::
449
458
450 c.NotebookApp.base_project_url = '/ipython/'
459 c.NotebookApp.base_project_url = '/ipython/'
451 c.NotebookApp.base_kernel_url = '/ipython/'
460 c.NotebookApp.base_kernel_url = '/ipython/'
452 c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'}
461 c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'}
453
462
454 Using a different notebook store
463 Using a different notebook store
455 --------------------------------
464 --------------------------------
456
465
457 By default the notebook server stores notebooks as files in the working
466 By default the notebook server stores notebooks as files in the working
458 directory of the notebook server, also known as the ``notebook_dir``. This
467 directory of the notebook server, also known as the ``notebook_dir``. This
459 logic is implemented in the :class:`FileNotebookManager` class. However, the
468 logic is implemented in the :class:`FileNotebookManager` class. However, the
460 server can be configured to use a different notebook manager class, which can
469 server can be configured to use a different notebook manager class, which can
461 store the notebooks in a different format. Currently, we ship a
470 store the notebooks in a different format. Currently, we ship a
462 :class:`AzureNotebookManager` class that stores notebooks in Azure blob
471 :class:`AzureNotebookManager` class that stores notebooks in Azure blob
463 storage. This can be used by adding the following lines to your
472 storage. This can be used by adding the following lines to your
464 ``ipython_notebook_config.py`` file::
473 ``ipython_notebook_config.py`` file::
465
474
466 c.NotebookApp.notebook_manager_class = 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager'
475 c.NotebookApp.notebook_manager_class = 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager'
467 c.AzureNotebookManager.account_name = u'paste_your_account_name_here'
476 c.AzureNotebookManager.account_name = u'paste_your_account_name_here'
468 c.AzureNotebookManager.account_key = u'paste_your_account_key_here'
477 c.AzureNotebookManager.account_key = u'paste_your_account_key_here'
469 c.AzureNotebookManager.container = u'notebooks'
478 c.AzureNotebookManager.container = u'notebooks'
470
479
471 In addition to providing your Azure Blob Storage account name and key, you will
480 In addition to providing your Azure Blob Storage account name and key, you will
472 have to provide a container name; you can use multiple containers to organize
481 have to provide a container name; you can use multiple containers to organize
473 your Notebooks.
482 your Notebooks.
474
483
475 .. _notebook_format:
484 .. _notebook_format:
476
485
477 The notebook format
486 The notebook format
478 ===================
487 ===================
479
488
480 The notebooks themselves are JSON files with an ``ipynb`` extension, formatted
489 The notebooks themselves are JSON files with an ``ipynb`` extension, formatted
481 as legibly as possible with minimal extra indentation and cell content broken
490 as legibly as possible with minimal extra indentation and cell content broken
482 across lines to make them reasonably friendly to use in version-control
491 across lines to make them reasonably friendly to use in version-control
483 workflows. You should be very careful if you ever edit manually this JSON
492 workflows. You should be very careful if you ever edit manually this JSON
484 data, as it is extremely easy to corrupt its internal structure and make the
493 data, as it is extremely easy to corrupt its internal structure and make the
485 file impossible to load. In general, you should consider the notebook as a
494 file impossible to load. In general, you should consider the notebook as a
486 file meant only to be edited by IPython itself, not for hand-editing.
495 file meant only to be edited by IPython itself, not for hand-editing.
487
496
488 .. note::
497 .. note::
489
498
490 Binary data such as figures are directly saved in the JSON file. This
499 Binary data such as figures are directly saved in the JSON file. This
491 provides convenient single-file portability but means the files can be
500 provides convenient single-file portability but means the files can be
492 large and diffs of binary data aren't very meaningful. Since the binary
501 large and diffs of binary data aren't very meaningful. Since the binary
493 blobs are encoded in a single line they only affect one line of the diff
502 blobs are encoded in a single line they only affect one line of the diff
494 output, but they are typically very long lines. You can use the
503 output, but they are typically very long lines. You can use the
495 'ClearAll' button to remove all output from a notebook prior to
504 'ClearAll' button to remove all output from a notebook prior to
496 committing it to version control, if this is a concern.
505 committing it to version control, if this is a concern.
497
506
498 The notebook server can also generate a pure-python version of your notebook,
507 The notebook server can also generate a pure-python version of your notebook,
499 by clicking on the 'Download' button and selecting ``py`` as the format. This
508 by clicking on the 'Download' button and selecting ``py`` as the format. This
500 file will contain all the code cells from your notebook verbatim, and all text
509 file will contain all the code cells from your notebook verbatim, and all text
501 cells prepended with a comment marker. The separation between code and text
510 cells prepended with a comment marker. The separation between code and text
502 cells is indicated with special comments and there is a header indicating the
511 cells is indicated with special comments and there is a header indicating the
503 format version. All output is stripped out when exporting to python.
512 format version. All output is stripped out when exporting to python.
504
513
505 Here is an example of a simple notebook with one text cell and one code input
514 Here is an example of a simple notebook with one text cell and one code input
506 cell, when exported to python format::
515 cell, when exported to python format::
507
516
508 # <nbformat>2</nbformat>
517 # <nbformat>2</nbformat>
509
518
510 # <markdowncell>
519 # <markdowncell>
511
520
512 # A text cell
521 # A text cell
513
522
514 # <codecell>
523 # <codecell>
515
524
516 print "hello IPython"
525 print "hello IPython"
517
526
518
527
519 Known issues
528 Known issues
520 ============
529 ============
521
530
522 When behind a proxy, especially if your system or browser is set to autodetect
531 When behind a proxy, especially if your system or browser is set to autodetect
523 the proxy, the html notebook might fail to connect to the server's websockets,
532 the proxy, the html notebook might fail to connect to the server's websockets,
524 and present you with a warning at startup. In this case, you need to configure
533 and present you with a warning at startup. In this case, you need to configure
525 your system not to use the proxy for the server's address.
534 your system not to use the proxy for the server's address.
526
535
527 In Firefox, for example, go to the Preferences panel, Advanced section,
536 In Firefox, for example, go to the Preferences panel, Advanced section,
528 Network tab, click 'Settings...', and add the address of the notebook server
537 Network tab, click 'Settings...', and add the address of the notebook server
529 to the 'No proxy for' field.
538 to the 'No proxy for' field.
530
539
531
540
532 .. _Markdown: http://daringfireball.net/projects/markdown/basics
541 .. _Markdown: http://daringfireball.net/projects/markdown/basics
General Comments 0
You need to be logged in to leave comments. Login now