##// END OF EJS Templates
Proofreading finished up to connecting to existing kernel
David P. Sanders -
Show More
@@ -1,658 +1,646 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 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 212 ``matplotlib`` figures and HTML tables (as used, for example, in the
213 213 ``pandas`` data analyis package). This is known as IPython's *rich display* capability.
214 214
215 215
216 216 Rich text using Markdown
217 217 ~~~~~~~~~~~~~~~~~~~~~~~~
218 218 You can document the computational process in a literate way, alternating descriptive text with code, using *rich text*. In IPython this is accomplished by marking up text using the Markdown markup language, in *Markdown input cells*. The Markdown language is a simple way to specify that parts of the text should be emphasized (italics), bold, form lists, etc.
219 219
220 220 When a Markdown input cell is executed, the Markdown code is converted into the corresponding formatted rich text. This output then *replaces* the original Markdown input cell, leaving just the visually-significant marked up rich text. Markdown allows arbitrary HTML code for formatting.
221 221
222 222 Within Markdown cells, you can also include *mathematics* in a straightforward way, using standar LaTeX notation: ``$...$`` for inline mathematics and ``$$...$$`` for displayed mathematics. Mathematics is rendered natively in the browser by MathJax. Standard LaTeX and AMS-math environments, such as
223 223 ``\begin{equation}...\end{equation}``, and ``\begin{align}...\end{align}`` also work, and new LaTeX macros may be defined using standard LaTeX methods, such as ``\newcommand``, by placing them anywhere in a Markdown cell.
224 224
225 225 Raw cells
226 226 ~~~~~~~~~
227 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 228 require to be included when the notebook is exported to a certain format.
229 229
230 230 Heading cells
231 231 ˜˜˜˜˜˜˜˜˜˜˜˜˜
232 232 You can provide a conceptual structure for your computational document using
233 233 different levels of headings; there are 6 levels available, from level 1 (main
234 234 title) down to level 6 (paragraph). These can be used later for constructing
235 235 tables of contents, etc.
236 236
237 237 As with Markdown cells, a heading input cell is replaced by a rich text rendering of the heading when the cell is executed.
238 238
239 239
240 240 Magic commands
241 241 --------------
242 242 Magic commands, or *magics*, are commands for controlling IPython itself.
243 243 They all begin with ``%`` and are entered into code input cells. They are entered into standard code cells and executed as usual with :kbd:`Shift-Enter`.
244 244
245 245 There are two types of magics:
246 246
247 247 **line magics**:
248 248 These begin with a single ``%`` and take as arguments the rest of the *same line* of the code cell. Any other lines of the code cell are treated as a standard code cell.
249 249
250 250 **cell magics**:
251 251 These begin with ``%%`` and operate on the *entire* remaining contents of the code cell.
252 252
253 253 Line magics
254 254 ˜˜˜˜˜˜˜˜˜˜˜
255 255 Some of the available line magics are the following:
256 256
257 257 * ``%load filename``:
258 258 Loads the contents of the file ``filename`` into a new code cell.
259 259
260 260 * ``%timeit code``:
261 261 A simple way to time how long the single line of code ``code`` takes to run
262 262
263 263 * ``%config``:
264 264 Configuration of the IPython Notebook
265 265
266 266 * ``%load_ext``:
267 267 Loads an *extension* of the IPython Notebook. Extensions provide extra functionality beyond that of the basic Notebook
268 268
269 269 * ``%lsmagic``:
270 270 Provides a list of all available magic commands
271 271
272 272 Cell magics
273 273 ˜˜˜˜˜˜˜˜˜˜˜
274 274 * ``%%latex``:
275 275 The entire contents of the cell is rendered in LaTeX, as in a Markdown cell but without needing LaTeX delimiters.
276 276
277 277 * ``%%bash``:
278 278 When the code cell is executed, its contents are sent to be executed by ``bash``.
279 279
280 280 * ``%%file filename``:
281 281 Writes the contents of the cell to the file ``filename``.
282 **Caution**: The file is ovewritten!
282 **Caution**: The file is over-written!
283 283
284 284 * ``%%R``:
285 285 Execute the contents of the cell using the R language.
286 286
287 287 * ``%%cython``:
288 288 Execute the contents of the cell using ``Cython``.
289 289
290 290
291 291 Several of the cell magics provide functionality to manipulate the filesystem of a remote server to which you otherwise do not have access.
292 292
293 293
294 294 Plotting
295 295 --------
296 296 One major feature of the Notebook is the ability to capture the result of plots as *inline* output, thus displaying the result of running some code right next to the code itself. IPython is designed to work seamlessly with the ``matplotlib`` plotting library to attain this functionality.
297 297
298 298 To set this up, before any plotting is performed you must execute the
299 299 ``%matplotlib`` magic command. This performs the necessary behind-the-scenes setup for IPython to work correctly hand in hand with ``matplotlib``; it does
300 300 *not*, however, actually execute any Python ``import`` commands, that is, no names are added to the namespace.
301 301
302 302 For more agile *interactive* use of the notebook space, an alternative magic, ``%pylab``, is provided. This does the same work as the ``%matplotlib`` magic, but *in addition* it automatically executes a standard sequence of ``import`` statements required to work with the ``%matplotlib`` library:
303 303
304 304 It will import at the top level `numpy` as `np`, `pyplot` as `plt`, `matplotlib`, `pylab` and `mlab` from `matplotlib`, as well as *all names* from ``numpy`` and ``pylab``. A less invasive, but less interactive, option is ``%pylab --no-import-all``, which does not do these ``import *`` imports.
305 305
306 306 When the default ``%matplotlib`` or ``%pylab`` magics are used, the output of a plotting command is captured in a *separate* window. An alternative is to use::
307 307
308 ``%matplotlib inline``
308 %matplotlib inline
309 309
310 310 or ``%pylab inline``.
311 311
312 312 These capture the output *inline* within the notebook format. This has the benefit that the resulting plots will also stored in the notebook document.
313 313
314 314
315 315 Converting notebooks to other formats
316 316 -------------------------------------
317 317 Newly added in the 1.0 release of IPython is the ``nbconvert`` tool, which allows you to convert an ``.ipynb`` notebook document file into another static format.
318 318
319 Currently, only a command line tool is provided; at present, this functionality is not available for direct exports from within the Notebook app. The syntax is::
319 Currently, only a command line tool is provided; in the future, it will also be possible to export from within the Notebook app. The command line syntax is::
320 320
321 321 $ ipython nbconvert --format=FORMAT notebook.ipynb
322 322
323 which will convert the IPython document file `notebook.ipynb` into the output format specified by the `FORMAT` string.
323 This will convert the IPython document file `notebook.ipynb` into the output format specified by the ``FORMAT`` string.
324 324
325 325 The default output format is HTML, for which the `--format`` modifier is not required::
326 326
327 327 $ ipython nbconvert notebook.ipynb
328 328
329 Otherwise, the following `FORMAT`
330
331 where ``FORMAT`` is the desired export format. The currently export format options available are the following:
329 The currently supported export formats are the following:
332 330
333 331 * HTML:
334 332
335 333 - ``full_html``:
336 334 Standard HTML
337 335
338 336 - ``simple_html``:
339 337 Simplified HTML
340 338
341 339 - ``reveal``:
342 340 HTML slideshow presentation for use with the ``reveal.js`` package
343 341
344 342 * PDF:
345 343
346 344 - ``sphinx_howto``:
347 The format for Sphinx HOWTOs; similar to `article` in LaTeX
345 The format for Sphinx HOWTOs; similar to an ``article`` in LaTeX
348 346
349 347 - ``sphinx_manual``:
350 The format for Sphinx manuals; similar to `book` in LaTeX
348 The format for Sphinx manuals; similar to a `book`` in LaTeX
351 349
352 350 - ``latex``:
353 351 LaTeX article
354 352
355 353 * Markup:
356 354
357 355 - ``rst``:
358 reStructuredText
356 reStructuredText markup
359 357
360 358 - ``markdown``:
361 Markdown
359 Markdown markup
362 360
363 361 * Python:
364 362
365 Produces a standard ``.py`` script, with the non-Python code commented out.
363 Comments out all the non-Python code to produce a standard Python ``.py`` script with just the code content.
366 364
367 The output files are currently placed in a new subdirectory called
368 ``nbconvert_build``.
365 The files output by `nbconvert` are currently placed in a new subdirectory called ``nbconvert_build``.
369 366
370 The PDF options produce a root LaTeX `.tex` file with the same name as the notebook, as well as individual files for each figure, and `.text` files with textual output from running code cells; all of these files are located together in the `nbconvert_build` subdirectory.
367 The PDF options produce a root LaTeX ``.tex`` file with the same name as the notebook, as well as individual files for each figure, and `.text` files with textual output from running code cells; all of these files are located together in the ``nbconvert_build`` subdirectory.
371 368
372 To actually produce the final PDF file, simply run::
369 To actually produce the final PDF file, change into the ``nbconvert_build`` directory and run the following command::
373 370
374 371 $ pdflatex notebook
375 372
376 which produces `notebook.pdf`, also inside the `nbconvert_build` subdirectory.
373 This requires a local installation of LaTeX on your machine.
374 It produes the output PDF file ``notebook.pdf``, also inside the
375 ``nbconvert_build`` subdirectory.
377 376
378 Alternatively, the output may be piped to standard output `stdout` with::
377 Alternatively, the output may be piped to standard output, ``stdout``, with::
379 378
380 379 $ ipython nbconvert mynotebook.ipynb --stdout
381 380
382 Multiple notebooks can be specified at the command line in a couple of
383 different ways::
381 Multiple notebooks can be specified at the command line::
384 382
385 383 $ ipython nbconvert notebook*.ipynb
386 384 $ ipython nbconvert notebook1.ipynb notebook2.ipynb
387 385
388 or via a list in a configuration file, containing::
386 or via a list in a configuration file, say ``mycfg.py``, containing the text::
389 387
390 388 c.NbConvertApp.notebooks = ["notebook1.ipynb", "notebook2.ipynb"]
391 389
392 390 and using the command::
393 391
394 392 > ipython nbconvert --config mycfg.py
395 393
396 394
397 Configuration
398 -------------
395 Configuring the IPython Notebook
396 --------------------------------
399 397 The IPython Notebook can be run with a variety of command line arguments.
400 398 To see a list of available options enter::
401 399
402 400 $ ipython notebook --help
403 401
404 402 Defaults for these options can also be set by creating a file named
405 `ipython_notebook_config.py`` in your IPython *profile folder*. The profile folder is a subfolder of your IPython directory; to find out where it is located, run::
403 ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile folder is a subfolder of your IPython directory; to find out where it is located, run::
406 404
407 405 $ ipython locate
408 406
409 407 To create a new set of default configuration files, with lots of information on available options, use::
410 408
411 409 $ ipython profile create
412 410
413 411 .. seealso:
414 412
415 413 :ref:`config_overview`, in particular :ref:`Profiles`.
416 414
417 415
418 416 Extracting standard Python files from notebooks
419 417 -----------------------------------------------
418 ``.ipynb`` notebook document files are plain text files which store a representation in JSON format of the contents of a notebook space. As such, it is not a valid ``.py`` Python script, and so can be neither ``import``ed in Python, nor executed as a standard Python script.
420 419
421 The native format of the notebook, a file with a ``.ipynb`` `extension, is a
422 JSON container of all the input and output of the notebook, and therefore not
423 valid Python by itself. This means that by default, you cannot directly
424 import a notebook from Python, nor execute it as a normal python script.
420 To extract just the Python code from within a notebook document, one option is to use ``ipython nbconvert``, as described above.
425 421
426 But if you want to be able to use notebooks also as regular Python files, you can start the notebook server with::
422 An alternative is to run the IPython Notebook, specifying that whenever it saves an ``.ipynb`` notebook document, at the same time it should save the corresponding standard ``.py`` script. To do so, you can execute the following command::
427 423
428 424 ipython notebook --script
429 425
430 426 or you can set this option permanently in your configuration file with::
431 427
432 428 c.NotebookManager.save_script=True
433 429
434 This will instruct the notebook server to save the ``.py`` export of each
435 notebook, in addition to the ``.ipynb``, at every save. These are standard
436 ``.py`` files, and so they can be ``%run``, imported from regular IPython
437 sessions or other notebooks, or executed at the command line. Since we export
438 the raw code you have typed, for these files to be importable from other code,
439 you will have to avoid using syntax such as ``%magic``s and other IPython-specific extensions to the language.
430 The result is that standard ``.py`` files are also now generated, and so they can be ``%run``, imported from regular IPython sessions or other notebooks, or executed at the command line, as usual. Since we export the raw code you have typed, for these files to be importable from other code, you will have to avoid using syntax such as IPython magics and other IPython-specific extensions to the language.
440 431
441 In regular practice, the standard way to differentiate importable code from the
442 'executable' part of a script is to put at the bottom::
432 In normal Python practice, the standard way to differentiate importable code in a Python script from the "executable" part of a script is to use the following idiom at the start of the executable part of the code::
443 433
444 if __name__ == '__main__':
445 # rest of the code...
434 if __name__ == '__main__'
446 435
436 # rest of the code...
437
447 438 Since all cells in the notebook are run as top-level code, you will need to
448 439 similarly protect *all* cells that you do not want executed when other scripts
449 440 try to import your notebook. A convenient shortand for this is to define early
450 441 on::
451 442
452 443 script = __name__ == '__main__'
453 444
454 and then on any cell that you need to protect, use::
445 Then in any cell that you need to protect, use::
455 446
456 447 if script:
457 448 # rest of the cell...
458 449
459 450
460 451 .. _notebook_security:
461 452
462 453 Security
463 454 --------
464 455
465 You can protect your Notebook server with a simple singlepassword by
456 You can protect your Notebook server with a simple single password by
466 457 setting the :attr:`NotebookApp.password` configurable. You can prepare a
467 458 hashed password using the function :func:`IPython.lib.security.passwd`:
468 459
469 460 .. sourcecode:: ipython
470 461
471 462 In [1]: from IPython.lib import passwd
472 463 In [2]: passwd()
473 464 Enter password:
474 465 Verify password:
475 466 Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
476 467
477 468 .. note::
478 469
479 470 :func:`~IPython.lib.security.passwd` can also take the password as a string
480 471 argument. **Do not** pass it as an argument inside an IPython session, as it
481 472 will be saved in your input history.
482 473
483 474 You can then add this to your :file:`ipython_notebook_config.py`, e.g.::
484 475
485 476 # Password to use for web authentication
486 477 c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
487 478
488 479 When using a password, it is a good idea to also use SSL, so that your password
489 480 is not sent unencrypted by your browser. You can start the notebook to
490 481 communicate via a secure protocol mode using a self-signed certificate with the command::
491 482
492 483 $ ipython notebook --certfile=mycert.pem
493 484
494 485 .. note::
495 486
496 487 A self-signed certificate can be generated with ``openssl``. For example, the following command will create a certificate valid for 365 days with both the key and certificate data written to the same file::
497 488
498 489 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
499 490
500 491 Your browser will warn you of a dangerous certificate because it is
501 492 self-signed. If you want to have a fully compliant certificate that will not
502 raise warnings, it is possible (but rather involved) to obtain one for free,
493 raise warnings, it is possible (but rather involved) to obtain one,
503 494 `as explained in detailed in this tutorial`__.
504 495
505 496 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars
506 497
507 Keep in mind that when you enable SSL support, you'll need to access the
498 Keep in mind that when you enable SSL support, you will need to access the
508 499 notebook server over ``https://``, not over plain ``http://``. The startup
509 message from the server prints this, but it's easy to overlook and think the
500 message from the server prints this, but it is easy to overlook and think the
510 501 server is for some reason non-responsive.
511 502
512 503
513 504 Connecting to an existing kernel
514 505 ---------------------------------
515 506
516 507 The notebook server always prints to the terminal the full details of
517 how to connect to each kernel, with lines like::
508 how to connect to each kernel, with messages such as the following::
518 509
519 510 [IPKernelApp] To connect another client to this kernel, use:
520 511 [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
521 512
522 This is the name of a JSON file that contains all the port and
523 validation information necessary to connect to the kernel. You can
524 manually start a Qt console with::
513 This long string is the name of a JSON file that contains all the port and
514 validation information necessary to connect to the kernel. You can then, for example, manually start a Qt console connected to the *same* kernel with::
525 515
526 516 ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
527 517
528 and if you only have a single kernel running, simply typing::
518 If you have only a single kernel running, simply typing::
529 519
530 520 ipython qtconsole --existing
531 521
532 will automatically find it (it will always find the most recently
533 started kernel if there is more than one). You can also request this
522 will automatically find it. (It will always find the most recently
523 started kernel if there is more than one.) You can also request this
534 524 connection data by typing ``%connect_info``; this will print the same
535 525 file information as well as the content of the JSON data structure it contains.
536 526
537 527
538 528 Running a public notebook server
539 529 --------------------------------
540 530
541 531 If you want to access your notebook server remotely with just a web browser,
542 here is a quick set of instructions. Start by creating a certificate file and
543 a hashed password as explained above. Then, create a custom profile for the
544 notebook. At the command line, type::
532 you can do the following.
533
534 Start by creating a certificate file and a hashed password, as explained above. Then, create a custom profile for the notebook: at the command line, type::
545 535
546 536 ipython profile create nbserver
547 537
548 In the profile directory, edit the file ``ipython_notebook_config.py``. By
549 default the file has all fields commented, the minimum set you need to
550 uncomment and edit is here::
538 In the profile directory just created, edit the file ``ipython_notebook_config.py``. By default, the file has all fields commented; the minimum set you need to uncomment and edit is the following::
551 539
552 540 c = get_config()
553 541
554 542 # Kernel config
555 543 c.IPKernelApp.pylab = 'inline' # if you want plotting support always
556 544
557 545 # Notebook config
558 546 c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
559 547 c.NotebookApp.ip = '*'
560 548 c.NotebookApp.open_browser = False
561 c.NotebookApp.password = u'sha1:bcd259ccf...your hashed password here'
562 # It's a good idea to put it on a known, fixed port
549 c.NotebookApp.password = u'sha1:bcd259ccf...[your hashed password here]'
550 # It is a good idea to put it on a known, fixed port
563 551 c.NotebookApp.port = 9999
564 552
565 553 You can then start the notebook and access it later by pointing your browser to
566 554 ``https://your.host.com:9999`` with ``ipython notebook --profile=nbserver``.
567 555
568 556 Running with a different URL prefix
569 557 -----------------------------------
570 558
571 559 The notebook dashboard (i.e. the default landing page with an overview
572 560 of all your notebooks) typically lives at a URL path of
573 561 "http://localhost:8888/". If you want to have it, and the rest of the
574 562 notebook, live under a sub-directory,
575 563 e.g. "http://localhost:8888/ipython/", you can do so with
576 564 configuration options like these (see above for instructions about
577 565 modifying ``ipython_notebook_config.py``)::
578 566
579 567 c.NotebookApp.base_project_url = '/ipython/'
580 568 c.NotebookApp.base_kernel_url = '/ipython/'
581 569 c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'}
582 570
583 571 Using a different notebook store
584 572 --------------------------------
585 573
586 574 By default the notebook server stores notebooks as files in the working
587 575 directory of the notebook server, also known as the ``notebook_dir``. This
588 576 logic is implemented in the :class:`FileNotebookManager` class. However, the
589 577 server can be configured to use a different notebook manager class, which can
590 578 store the notebooks in a different format. Currently, we ship a
591 579 :class:`AzureNotebookManager` class that stores notebooks in Azure blob
592 580 storage. This can be used by adding the following lines to your
593 581 ``ipython_notebook_config.py`` file::
594 582
595 583 c.NotebookApp.notebook_manager_class = 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager'
596 584 c.AzureNotebookManager.account_name = u'paste_your_account_name_here'
597 585 c.AzureNotebookManager.account_key = u'paste_your_account_key_here'
598 586 c.AzureNotebookManager.container = u'notebooks'
599 587
600 588 In addition to providing your Azure Blob Storage account name and key, you will
601 589 have to provide a container name; you can use multiple containers to organize
602 590 your Notebooks.
603 591
604 592 .. _notebook_format:
605 593
606 594 Notebook JSON format
607 595 ====================
608 596
609 597 Notebooks are JSON files with an ``.ipynb`` extension, formatted
610 598 as legibly as possible with minimal extra indentation and cell content broken
611 599 across lines to make them reasonably friendly to use in version-control
612 600 workflows. You should be very careful if you ever manually edit this JSON
613 601 data, as it is extremely easy to corrupt its internal structure and make the
614 602 file impossible to load. In general, you should consider the notebook as a
615 603 file meant only to be edited by the IPython Notebook app itself, not for hand-editing.
616 604
617 605 .. note::
618 606
619 607 Binary data such as figures are directly saved in the JSON file. This
620 608 provides convenient single-file portability, but means that the files can
621 609 be large; ``diff``s of binary data also are not very meaningful. Since the
622 610 binary blobs are encoded in a single line, they affect only one line of
623 611 the ``diff`` output, but they are typically very long lines. You can use the ``Cell -> All Output -> Clear`` menu option to remove all output from a notebook prior to committing it to version control, if this is a concern.
624 612
625 613 The notebook server can also generate a pure Python version of your notebook,
626 614 using the ``File -> Download as`` menu option. The resulting ``.py`` file will
627 615 contain all the code cells from your notebook verbatim, and all text cells
628 616 prepended with a comment marker. The separation between code and text
629 617 cells is indicated with special comments and there is a header indicating the
630 618 format version. All output is stripped out when exporting to Python.
631 619
632 620 Here is an example of the Python output from a simple notebook with one text cell and one code input cell::
633 621
634 622 # <nbformat>2</nbformat>
635 623
636 624 # <markdowncell>
637 625
638 626 # A text cell
639 627
640 628 # <codecell>
641 629
642 630 print "Hello, IPython!"
643 631
644 632
645 633 Known issues
646 634 ============
647 635
648 636 When behind a proxy, especially if your system or browser is set to autodetect
649 637 the proxy, the Notebook app might fail to connect to the server's websockets,
650 638 and present you with a warning at startup. In this case, you need to configure
651 639 your system not to use the proxy for the server's address.
652 640
653 641 In Firefox, for example, go to the Preferences panel, Advanced section,
654 642 Network tab, click 'Settings...', and add the address of the notebook server
655 643 to the 'No proxy for' field.
656 644
657 645
658 646 .. _Markdown: http://daringfireball.net/projects/markdown/basics
General Comments 0
You need to be logged in to leave comments. Login now