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