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