##// END OF EJS Templates
Merge pull request #3705 from dpsanders/notebook-docs...
Min RK -
r11618:c06a81ce merge
parent child Browse files
Show More
This diff has been collapsed as it changes many lines, (1035 lines changed) Show them Hide them
@@ -1,198 +1,238 b''
1 .. _htmlnotebook:
1 .. _htmlnotebook:
2
2
3 =========================
3 The IPython Notebook
4 An HTML Notebook IPython
4 ====================
5 =========================
5
6 The IPython Notebook is part of the IPython package, which aims to provide a
7 powerful, interactive approach to scientific computation.
8 The IPython Notebook extends the previous text-console-based approach, and the
9 later Qt console, in a qualitatively new diretion, providing a web-based
10 application suitable for capturing the whole scientific computation process.
6
11
7 .. seealso::
12 .. seealso::
8
13
9 :ref:`Installation requirements <installnotebook>` for the Notebook.
14 :ref:`Installation requirements <installnotebook>` for the Notebook.
10
15
11 The IPython Notebook consists of two related components:
12
16
13 * An JSON based Notebook document format for recording and distributing
17 Basic structure
14 Python code and rich text.
18 ---------------
15 * A web-based user interface for authoring and running notebook documents.
16
19
17 The Notebook can be used by starting the Notebook server with the
20 The IPython Notebook combines two components:
18 command::
19
21
20 $ ipython notebook
22 * **The IPython Notebook web application**:
21
23
22 Note that by default, the notebook doesn't load pylab, it's just a normal
24 The *IPython Notebook web app* is a browser-based tool for interactive
23 IPython session like any other. If you want pylab support, you must use::
25 authoring of literate computations, in which explanatory text, mathematics,
24
26 computations and rich media output may be combined. Input and output are
25 $ ipython notebook --pylab
27 stored in persistent cells that may be edited in-place.
26
28
27 which will behave similar to the terminal and Qt console versions, using your
29 * **Notebook documents**:
28 default matplotlib backend and providing floating interactive plot windows. If
29 you want inline figures, you must manually select the ``inline`` backend::
30
30
31 $ ipython notebook --pylab inline
31 *Notebook documents*, or *notebooks*, are plain text documents which record
32 all inputs and outputs of the computations, interspersed with text,
33 mathematics and HTML 5 representations of objects, in a literate style.
32
34
33 This server uses the same ZeroMQ-based two process kernel architecture as
35 Since the similarity in names can lead to some confusion, in this documentation
34 the QT Console as well Tornado for serving HTTP/S requests. Some of the main
36 we will use capitalization of the word "notebook" to distinguish the
35 features of the Notebook include:
37 *N*otebook app and *n*otebook documents, thinking of the Notebook app as being
38 a proper noun. We will also always refer to the "Notebook app" when we are
39 referring to the browser-based interface, and usually to "notebook documents",
40 instead of "notebooks", for added precision.
36
41
37 * Display rich data (png/html/latex/svg) in the browser as a result of
42 We refer to the current state of the computational process taking place in the
38 computations.
43 Notebook app, i.e. the (numbered) sequence of input and output cells, as the
39 * Compose text cells using HTML and Markdown.
44 *notebook space*. Notebook documents provide an *exact*, *one-to-one* record
40 * Import and export notebook documents in range of formats (.ipynb, .py).
45 of all the content in the notebook space, as a plain text file in JSON format.
41 * In browser syntax highlighting, tab completion and autoindentation.
46 The Notebook app automatically saves, at certain intervals, the contents of
42 * Inline matplotlib plots that can be stored in Notebook documents and opened
47 the notebook space to a notebook document stored on disk, with the same name
43 later.
48 as the title of the notebook space, and the file extension ``.ipynb``. For
49 this reason, there is no confusion about using the same word "notebook" for
50 both the notebook space and the corresonding notebook document, since they are
51 really one and the same concept (we could say that they are "isomorphic").
44
52
45 See :ref:`our installation documentation <install_index>` for directions on
46 how to install the notebook and its dependencies.
47
53
48 .. note::
54 Main features of the IPython Notebook web app
55 ---------------------------------------------
49
56
50 You can start more than one notebook server at the same time, if you want to
57 The main features of the IPython Notebook app include:
51 work on notebooks in different directories. By default the first notebook
52 server starts in port 8888, later notebooks search for random ports near
53 that one. You can also manually specify the port with the ``--port``
54 option.
55
56
58
57 Basic Usage
59 * In-browser editing for code, with automatic syntax highlighting and indentation and tab completion/introspection.
58 ===========
59
60
60 The landing page of the notebook server application, which we call the IPython
61 * Literate combination of code with rich text using the Markdown_ markup language.
61 Notebook *dashboard*, shows the notebooks currently available in the directory
62 in which the application was started, and allows you to create new notebooks.
63
62
64 A notebook is a combination of two things:
63 * Mathematics is easily included within the Markdown using LaTeX notation, and rendered natively by MathJax_.
65
64
66 1. An interactive session connected to an IPython kernel, controlled by a web
65 * Displays rich data representations (e.g. HTML / LaTeX / SVG) as the result of computations.
67 application that can send input to the console and display many types of
68 output (text, graphics, mathematics and more). This is the same kernel used
69 by the :ref:`Qt console <qtconsole>`, but in this case the web console sends
70 input in persistent cells that you can edit in-place instead of the
71 vertically scrolling terminal style used by the Qt console.
72
66
73 2. A document that can save the inputs and outputs of the session as well as
67 * Publication-quality figures in a range of formats (SVG / PNG), rendered by the matplotlib_ library, may be included inline and exported.
74 additional text that accompanies the code but is not meant for execution.
75 In this way, notebook files serve as a complete computational record of a
76 session including explanatory text and mathematics, code and resulting
77 figures. These documents are internally JSON files and are saved with the
78 ``.ipynb`` extension.
79
68
80 If you have ever used the Mathematica or Sage notebooks (the latter is also
81 web-based__) you should feel right at home. If you have not, you should be
82 able to learn how to use it in just a few minutes.
83
69
84 .. __: http://sagenb.org
70 .. _MathJax: http://www.mathjax.org/
71 .. _matplotlib: http://matplotlib.org/
72 .. _Markdown: http://daringfireball.net/projects/markdown/syntax
85
73
86
74
87 Creating and editing notebooks
75 Notebook documents
88 ------------------------------
76 ------------------
89
77
90 You can create new notebooks from the dashboard with the ``New Notebook``
78 Notebook document files are just standard, ASCII-coded text files with the extension ``.ipynb``, stored in the working directory on your computer. Since the contents of the files are just plain text, they can be easily version-controlled and shared with colleagues.
91 button or open existing ones by clicking on their name. Once in a notebook,
92 your browser tab will reflect the name of that notebook (prefixed with "IPy:").
93 The URL for that notebook is not meant to be human-readable and is *not*
94 persistent across invocations of the notebook server.
95
96 You can also drag and drop into the area listing files any python file: it
97 will be imported into a notebook with the same name (but ``.ipynb`` extension)
98 located in the directory where the notebook server was started. This notebook
99 will consist of a single cell with all the code in the file, which you can
100 later manually partition into individual cells for gradual execution, add text
101 and graphics, etc.
102
103
104 Workflow and limitations
105 ------------------------
106
107 The normal workflow in a notebook is quite similar to a normal IPython session,
108 with the difference that you can edit a cell in-place multiple times until you
109 obtain the desired results rather than having to rerun separate scripts with
110 the ``%run`` magic (though magics also work in the notebook). Typically
111 you'll work on a problem in pieces, organizing related pieces into cells and
112 moving forward as previous parts work correctly. This is much more convenient
113 for interactive exploration than breaking up a computation into scripts that
114 must be executed together, especially if parts of them take a long time to run
115 (In the traditional terminal-based IPython, you can use tricks with namespaces
116 and ``%run -i`` to achieve this capability, but we think the notebook is a more
117 natural solution for that kind of problem).
118
119 The only significant limitation the notebook currently has, compared to the qt
120 console, is that it can not run any code that expects input from the kernel
121 (such as scripts that call :func:`raw_input`). Very importantly, this means
122 that the ``%debug`` magic does *not* work in the notebook! We intend to
123 correct this limitation, but in the meantime, there is a way to debug problems
124 in the notebook: you can attach a Qt console to your existing notebook kernel,
125 and run ``%debug`` from the Qt console. If your notebook is running on a local
126 computer (i.e. if you are accessing it via your localhost address at
127 127.0.0.1), you can just type ``%qtconsole`` in the notebook and a Qt console
128 will open up connected to that same kernel.
129
130 In general, the notebook server prints the full details of how to connect to
131 each kernel at the terminal, with lines like::
132
79
133 [IPKernelApp] To connect another client to this kernel, use:
80 Internally, notebook document files use the JSON_ format, allowing them to
134 [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
81 store a *complete*, *reproducible*, *one-to-one* copy of the state of the computational state as it is inside the Notebook app.
82 All computations carried out, and the corresponding results obtained, can be
83 combined in a literate way, interleaving executable code with rich text, mathematics, and HTML 5 representations of objects.
135
84
136 This is the name of a JSON file that contains all the port and validation
85 .. _JSON: http://en.wikipedia.org/wiki/JSON
137 information necessary to connect to the kernel. You can manually start a
138 qt console with::
139
86
140 ipython qtconsole --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
87 Notebooks may easily be exported to a range of static formats, including
88 HTML (for example, for blog posts), PDF and slide shows, via the newly-included `nbconvert script`_ functionality.
141
89
142 and if you only have a single kernel running, simply typing::
90 Furthermore, any ``.ipynb`` notebook document with a publicly-available URL can be shared via the `IPython Notebook Viewer`_ service. This service loads the notebook document from the URL which will
91 provide it as a static web page. The results may thus be shared with a colleague, or as a public blog post, without other users needing to install IPython themselves.
143
92
144 ipython qtconsole --existing
93 See the :ref:`installation documentation <install_index>` for directions on
94 how to install the notebook and its dependencies.
145
95
146 will automatically find it (it will always find the most recently started
96 .. _`Ipython Notebook Viewer`: http://nbviewer.ipython.org
147 kernel if there is more than one). You can also request this connection data
97
148 by typing ``%connect_info``; this will print the same file information as well
98 .. note::
149 as the content of the JSON data structure it contains.
99
100 You can start more than one notebook server at the same time, if you want to
101 work on notebooks in different directories. By default the first notebook
102 server starts on port 8888, and later notebook servers search for ports
103 near that one. You can also manually specify the port with the ``--port``
104 option.
105
106
107 Starting up the IPython Notebook web app
108 ----------------------------------------
109
110 You can start running the Notebook web app using the following command::
111
112 $ ipython notebook
113
114 (Here, and in the sequel, the initial ``$`` represents the shell prompt, indicating that the command is to be run from the command line in a shell.)
115
116 The landing page of the notebook server application, the *dashboard*, shows
117 the notebooks currently available in the *working directory* (the directory
118 from which the notebook was started).
119 You can create new notebooks from the dashboard with the ``New Notebook``
120 button, or open existing ones by clicking on their name.
121 You can also drag and drop ``.ipynb`` notebooks and standard ``.py`` Python
122 source code files into the notebook list area.
123
124 ``.py`` files will be imported into the IPython Notebook as a notebook with
125 the same name, but an ``.ipynb`` extension, located in the working directory.
126 The notebook created will have just one cell, which will contain all the
127 code in the ``.py`` file. You can later manually partition this into
128 individual cells using the ``Edit | Split Cell`` menu option, or the
129 :kbd:`Ctrl-m -` keyboard shortcut.
130
131 .. Alternatively, prior to importing the ``.py``, you can manually add ``# <
132 nbformat>2</nbformat>`` at the start of the file, and then add separators for
133 text and code cells, to get a cleaner import with the file already broken into
134 individual cells.
135
136 When you open or create a new notebook, your browser tab will reflect the name
137 of that notebook, prefixed by the "IPy" icon denoting that the tab corresponds to the IPython Notebook.
138 The URL is currently not meant to be human-readable and is not persistent
139 across invocations of the notebook server; however, this will change in a
140 future version of IPython.
141
142
143 The IPython Notebook web app is based on a server-client structure.
144 This server uses a two-process kernel architecture based on ZeroMQ, as well as
145 Tornado for serving HTTP requests. Other clients may connect to the same
146 underlying IPython kernel; see below.
147
148
149 Notebook user interface
150 -----------------------
151
152 When you open a new notebook document in the Notebook, you will be presented
153 with the title associated to the notebook space/document, a *menu bar*, a
154 *toolbar* and an empty *input cell*.
155
156 Notebook title
157 ~~~~~~~~~~~~~~
158 The title of the notebook document that is currently being edited is displayed
159 at the top of the page, next to the ``IP[y]: Notebook`` logo. This title may
160 be edited directly by clicking on it. The title is reflected in the name of
161 the ``.ipynb`` notebook document file that is saved.
162
163 Menu bar
164 ~~~~~~~~
165 The menu bar presents different options that may be used to manipulate the way
166 the Notebook functions.
167
168 Toolbar
169 ~~~~~~~
170 The tool bar gives a quick way of accessing the most-used operations within
171 the Notebook, by clicking on an icon.
172
173 Creating a new notebook document
174 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
175
176 A new notebook space/document may be created at any time, either from the dashboard, or using the `File | New` menu option from within an active notebook. The new notebook is created within the same working directory and will open in a new browser tab. It will also be reflected as a new entry in the notebook list on the dashboard.
177
178
179 Input cells
180 -----------
181 Input cells are at the core of the functionality of the IPython Notebook.
182 They are regions in the document in which you can enter different types of
183 text and commands. To *execute* or *run* the *current cell*, i.e. the cell
184 under the cursor, you can use the :kbd:`Shift-Enter` key combination.
185 This tells the Notebook app to perform the relevant operation for each type of
186 cell (see below), and then to display the resulting output.
187
188 The notebook consists of a sequence of input cells, labelled ``In[n]``, which
189 may be executed in a non-linear way, and outputs ``Out[n]``, where ``n`` is a
190 number which denotes the order in which the cells were executed over the
191 history of the computational process. The contents of all of these cells are
192 accessible as Python variables with the same names, forming a complete record
193 of the history of the computation.
194
195
196 Basic workflow
197 --------------
198 The normal workflow in a notebook is, then, quite similar to a standard
199 IPython session, with the difference that you can edit cells in-place multiple
200 times until you obtain the desired results, rather than having to
201 rerun separate scripts with the ``%run`` magic command. (Magic commands do,
202 however, also work in the notebook; see below).
203
204 Typically, you will work on a computational problem in pieces, organizing
205 related ideas into cells and moving forward once previous parts work
206 correctly. This is much more convenient for interactive exploration than
207 breaking up a computation into scripts that must be executed together, as was
208 previously necessary, especially if parts of them take a long time to run
209
210 The only significant limitation that the Notebook currently has, compared to
211 the Qt console, is that it cannot run any code that expects input from the
212 kernel (such as scripts that call :func:`raw_input`). Very importantly, this
213 means that the ``%debug`` magic does *not* currently work in the notebook!
214
215 This limitation will be overcome in the future, but in the meantime, there is
216 a simple solution for debugging: you can attach a Qt console to your existing
217 notebook kernel, and run ``%debug`` from the Qt console.
218 If your notebook is running on a local computer (i.e. if you are accessing it
219 via your localhost address at ``127.0.0.1``), then you can just type
220 ``%qtconsole`` in the notebook and a Qt console will open up, connected to
221 that same kernel.
222
223 At certain moments, it may be necessary to interrupt a calculation which is
224 taking too long to complete. This may be done with the ``Kernel | Interrupt``
225 menu option, or the :kbd:``Ctrl-i`` keyboard shortcut.
226 Similarly, it may be necessary or desirable to restart the whole computational
227 process, with the ``Kernel | Restart`` menu option or :kbd:``Ctrl-.`` shortcut.
228 This gives an equivalent state to loading the notebook document afresh.
150
229
151
152 Text input
153 ----------
154
155 In addition to code cells and the output they produce (such as figures), you
156 can also type text not meant for execution. To type text, change the type of a
157 cell from ``Code`` to ``Markdown`` by using the button or the :kbd:`Ctrl-m m`
158 keybinding (see below). You can then type any text in Markdown_ syntax, as
159 well as mathematical expressions if you use ``$...$`` for inline math or
160 ``$$...$$`` for displayed math.
161
162
163 Exporting a notebook and importing existing scripts
164 ---------------------------------------------------
165
166 If you want to provide others with a static HTML or PDF view of your notebook,
167 use the ``Print`` button. This opens a static view of the document, which you
168 can print to PDF using your operating system's facilities, or save to a file
169 with your web browser's 'Save' option (note that typically, this will create
170 both an html file *and* a directory called `notebook_name_files` next to it
171 that contains all the necessary style information, so if you intend to share
172 this, you must send the directory along with the main html file).
173
174 The `Download` button lets you save a notebook file to the Download area
175 configured by your web browser (particularly useful if you are running the
176 notebook server on a remote host and need a file locally). The notebook is
177 saved by default with the ``.ipynb`` extension and the files contain JSON data
178 that is not meant for human editing or consumption. But you can always export
179 the input part of a notebook to a plain python script by choosing Python format
180 in the `Download` drop list. This removes all output and saves the text cells
181 in comment areas. See ref:`below <notebook_format>` for more details on the
182 notebook format.
183
184 The notebook can also *import* ``.py`` files as notebooks, by dragging and
185 dropping the file into the notebook dashboard file list area. By default, the
186 entire contents of the file will be loaded into a single code cell. But if
187 prior to import, you manually add the ``# <nbformat>2</nbformat>`` marker at
188 the start and then add separators for text/code cells, you can get a cleaner
189 import with the file broken into individual cells.
190
230
191 .. warning::
231 .. warning::
192
232
193 While in simple cases you can roundtrip a notebook to Python, edit the
233 While in simple cases you can "roundtrip" a notebook to Python, edit the
194 python file and import it back without loss of main content, this is in
234 Python file, and then import it back without loss of main content, this is
195 general *not guaranteed to work at all*. First, there is extra metadata
235 in general *not guaranteed to work*. First, there is extra metadata
196 saved in the notebook that may not be saved to the ``.py`` format. And as
236 saved in the notebook that may not be saved to the ``.py`` format. And as
197 the notebook format evolves in complexity, there will be attributes of the
237 the notebook format evolves in complexity, there will be attributes of the
198 notebook that will not survive a roundtrip through the Python form. You
238 notebook that will not survive a roundtrip through the Python form. You
@@ -201,61 +241,355 b' import with the file broken into individual cells.'
201 notebook started. But the Python version is *not* an alternate notebook
241 notebook started. But the Python version is *not* an alternate notebook
202 format.
242 format.
203
243
244
245 Keyboard shortcuts
246 ------------------
247 All actions in the notebook can be achieved with the mouse, but keyboard
248 shortcuts are also available for the most common ones, so that productive use
249 of the notebook can be achieved with minimal mouse usage. The main shortcuts
250 to remember are the following:
251
252 * :kbd:`Shift-Enter`:
253
254 Execute the current cell, show output (if any), and jump to the next cell
255 below. If :kbd:`Shift-Enter` is invoked on the last input cell, a new code
256 cell will also be created. Note that in the notebook, typing :kbd:`Enter`
257 on its own *never* forces execution, but rather just inserts a new line in
258 the current input cell. In the Notebook it is thus always necessary to use
259 :kbd:`Shift-Enter` to execute the cell (or use the ``Cell | Run`` menu
260 item).
261
262 * :kbd:`Ctrl-Enter`:
263 Execute the current cell as if it were in "terminal mode", where any
264 output is shown, but the cursor *remains* in the current cell. This is
265 convenient for doing quick experiments in place, or for querying things
266 like filesystem content, without needing to create additional cells that
267 you may not want to be saved in the notebook.
268
269 * :kbd:`Alt-Enter`:
270 Executes the current cell, shows the output, and inserts a *new* input
271 cell between the current cell and the adjacent cell (if one exists). This
272 is thus a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`.
273 (:kbd:`Ctrl-m a` adds a new cell above the current one.)
274
275 * :kbd:`Ctrl-m`:
276 This is the prefix for *all* other shortcuts, which consist of :kbd:`Ctrl-m` followed by a single letter or character. For example, if you type :kbd:`Ctrl-m h` (that is, the sole letter :kbd:`h` after :kbd:`Ctrl-m`), IPython will show you all the available keyboard shortcuts.
204
277
205 Importing or executing a notebook as a normal Python file
206 ---------------------------------------------------------
207
278
208 The native format of the notebook, a file with a ``.ipynb`` extension, is a
279 Input cell types
209 JSON container of all the input and output of the notebook, and therefore not
280 ----------------
210 valid Python by itself. This means that by default, you can not import a
281 Each IPython input cell has a *cell type*, of which there is a restricted
211 notebook or execute it as a normal python script. But if you want use
282 number. The type of a cell may be set by using the cell type dropdown on the
212 notebooks as regular Python files, you can start the notebook server with::
283 toolbar, or via the following keyboard shortcuts:
213
284
214 ipython notebook --script
285 * **code**: :kbd:`Ctrl-m y`
286 * **markdown**: :kbd:`Ctrl-m m`
287 * **raw**: :kbd:`Ctrl-m t`
288 * **heading**: :kbd:`Ctrl-m 1` - :kbd:`Ctrl-m 6`
215
289
216 or you can set this option permanently in your configuration file with::
290 Upon initial creation, each input cell is by default a code cell.
217
291
218 c.NotebookManager.save_script=True
219
292
220 This will instruct the notebook server to save the ``.py`` export of each
293 Code cells
221 notebook adjacent to the ``.ipynb`` at every save. These files can be
294 ~~~~~~~~~~
222 ``%run``, imported from regular IPython sessions or other notebooks, or
295 A *code input cell* allows you to edit code inline within the cell, with full
223 executed at the command-line as normal Python files. Since we export the raw
296 syntax highlighting and autocompletion/introspection. By default, the language
224 code you have typed, for these files to be importable from other code you will
297 associated to a code cell is Python, but other languages, such as ``julia``
225 have to avoid using syntax such as ``%magics`` and other IPython-specific
298 and ``R``, can be handled using magic commands (see below).
226 extensions to the language.
227
299
228 In regular practice, the standard way to differentiate importable code from the
300 When a code cell is executed with :kbd:`Shift-Enter`, the code that it
229 'executable' part of a script is to put at the bottom::
301 contains is transparently exported and run in that language (with automatic
302 compiling, etc., if necessary). The result that is returned from this
303 computation is then displayed in the notebook space as the cell's
304 *output*. If this output is of a textual nature, it is placed into a
305 numbered *output cell*. However, many other possible forms of output are also
306 possible, including ``matplotlib`` figures and HTML tables (as used, for
307 example, in the ``pandas`` data analyis package). This is known as IPython's
308 *rich display* capability.
230
309
231 if __name__ == '__main__':
232 # rest of the code...
233
310
234 Since all cells in the notebook are run as top-level code, you'll need to
311 Rich text using Markdown
235 similarly protect *all* cells that you do not want executed when other scripts
312 ~~~~~~~~~~~~~~~~~~~~~~~~
236 try to import your notebook. A convenient shortand for this is to define early
313 You can document the computational process in a literate way, alternating
237 on::
314 descriptive text with code, using *rich text*. In IPython this is accomplished
315 by marking up text with the Markdown language. The corresponding cells are
316 called *Markdown input cells*. The Markdown language provides a simple way to
317 perform this text markup, that is, to specify which parts of the text should
318 be emphasized (italics), bold, form lists, etc.
238
319
239 script = __name__ == '__main__'
240
320
241 and then on any cell that you need to protect, use::
321 When a Markdown input cell is executed, the Markdown code is converted into
322 the corresponding formatted rich text. This output then *replaces* the
323 original Markdown input cell, leaving just the visually-significant marked up
324 rich text. Markdown allows arbitrary HTML code for formatting.
242
325
243 if script:
326 Within Markdown cells, you can also include *mathematics* in a straightforward
244 # rest of the cell...
327 way, using standard LaTeX notation: ``$...$`` for inline mathematics and
328 ``$$...$$`` for displayed mathematics. When the Markdown cell is executed, the LaTeX portions are automatically rendered in the HTML output as equations with high quality typography. This is made possible by MathJax_, which supports a `large subset`_ of LaTeX functionality
329
330 .. _`large subset`: http://docs.mathjax.org/en/latest/tex.html
331
332 Standard mathematics environments defined by LaTeX and AMS-LaTeX (the `amsmath` package) also work, such as
333 ``\begin{equation}...\end{equation}``, and ``\begin{align}...\end{align}``.
334 New LaTeX macros may be defined using standard methods,
335 such as ``\newcommand``, by placing them anywhere *between math delimiters* in a Markdown cell. These definitions are then available throughout the rest of the IPython session. (Note, however, that more care must be taken when using the `nbconvert script`_ to output to LaTeX).
336
337 Raw input cells
338 ~~~~~~~~~~~~~~~
339 *Raw* input cells provide a place in which you can put additional information
340 which you do not want to evaluated by the Notebook. This can be used, for
341 example, to include extra information that is needed when exporting to a
342 certain format. The output after evaluating a raw cell is just a verbatim copy
343 of the input.
344
345 Heading cells
346 ~~~~~~~~~~~~~
347 You can provide a conceptual structure for your computational document as a
348 whole using different levels of headings; there are 6 levels available, from
349 level 1 (top level) down to level 6 (paragraph). These can be used later for
350 constructing tables of contents, etc.
351
352 As with Markdown cells, a heading input cell is replaced by a rich text
353 rendering of the heading when the cell is executed.
354
355
356 Magic commands
357 ~~~~~~~~~~~~~~
358 Magic commands, or *magics*, are commands for controlling IPython itself.
359 They all begin with ``%`` and are entered into code input cells; the code
360 cells are executed as usual with :kbd:`Shift-Enter`.
361
362 The magic commands call special functions defined by IPython which manipulate
363 the computational state in certain ways.
364
365 There are two types of magics:
366
367 - **line magics**:
368
369 These begin with a single ``%`` and take as arguments the rest of the
370 *same line* of the code cell. Any other lines of the code cell are
371 treated as if they were part of a standard code cell.
372
373 - **cell magics**:
374
375 These begin with ``%%`` and operate on the *entire* remaining contents of
376 the code cell.
377
378 Line magics
379 ~~~~~~~~~~~
380 Some of the available line magics are the following:
381
382 * ``%load filename``:
383
384 Loads the contents of the file ``filename`` into a new code cell. This
385 can be a URL for a remote file.
386
387 * ``%timeit code``:
388
389 An easy way to time how long the single line of code ``code`` takes to run
390
391 * ``%config``:
392
393 Configuration of the IPython Notebook
394
395 * ``%lsmagic``:
396
397 Provides a list of all available magic commands
398
399 Cell magics
400 ~~~~~~~~~~~
401
402 * ``%%latex``:
403
404 Renders the entire contents of the cell in LaTeX, without needing to use
405 explicit LaTeX delimiters.
406
407 * ``%%bash``:
408
409 The code cell is executed by sending it to be executed by ``bash``. The
410 output of the ``bash`` commands is captured and displayed in the notebook.
411
412 * ``%%file filename``:
413
414 Writes the contents of the cell to the file ``filename``.
415 **Caution**: The file is over-written without warning!
416
417 * ``%%R``:
418
419 Execute the contents of the cell using the R language.
420
421 * ``%%timeit``:
422
423 Version of ``%timeit`` which times the entire block of code in the current code cell.
424
425
426
427 Several of the cell magics provide functionality to manipulate the filesystem
428 of a remote server to which you otherwise do not have access.
429
430
431 Plotting
432 --------
433 One major feature of the Notebook is the ability to interact with
434 plots that are the output of running code cells. IPython is designed to work
435 seamlessly with the ``matplotlib`` plotting library to provide this
436 functionality.
437
438 To set this up, before any plotting is performed you must execute the
439 ``%matplotlib`` magic command. This performs the necessary behind-the-scenes
440 setup for IPython to work correctly hand in hand with ``matplotlib``; it does
441 *not*, however, actually execute any Python ``import`` commands, that is, no
442 names are added to the namespace.
443
444 For more agile *interactive* use of the notebook space, an alternative magic,
445 ``%pylab``, is provided. This does the same work as the ``%matplotlib`` magic,
446 but *in addition* it automatically executes a standard sequence of ``import``
447 statements required to work with the ``%matplotlib`` library, importing the
448 following names into the namespace:
449
450 ``numpy`` as ``np``; ``matplotlib.pyplot`` as ``plt``;
451 ``matplotlib``, ``pylab`` and ``mlab`` from ``matplotlib``; and *all names*
452 from within ``numpy`` and ``pylab``.
453
454 However, the use of ``%pylab`` is discouraged, since names coming from
455 different packages may collide. In general, the use of ``from package import
456 *`` is discouraged. A better option is then::
457
458 %pylab --no-import-all
459
460 which imports the names listed above, but does *not* perform this ``import *``
461 imports.
462
463 If the ``%matplotlib`` or ``%pylab` magics are called without an argument, the
464 output of a plotting command is displayed using the default ``matplotlib``
465 backend in a separate window. Alternatively, the backend can be explicitly
466 requested using, for example::
467
468 %matplotlib gtk
469
470 A particularly interesting backend is the ``inline`` backend.
471 This is applicable only for the IPython Notebook and the IPython Qtconsole.
472 It can be invoked as follows::
245
473
246 Configuration
474 %matplotlib inline
247 -------------
248
475
249 The IPython notebook server can be run with a variety of command line arguments.
476 With this backend, output of plotting commands is displayed *inline* within
250 To see a list of available options enter:
477 the notebook format, directly below the input cell that produced it. The resulting plots will then also be stored in the notebook document. This provides a key part of the functionality for reproducibility_ that the IPython Notebook provides.
478
479 .. _reproducibility: https://en.wikipedia.org/wiki/Reproducibility
480
481 .. _`nbconvert script`:
482
483 Converting notebooks to other formats
484 -------------------------------------
485 Newly added in the 1.0 release of IPython is the ``nbconvert`` tool, which
486 allows you to convert an ``.ipynb`` notebook document file into various static
487 formats.
488
489 Currently, ``nbconvert`` is provided as a command line tool, run as a script using IPython. In the future, a direct export capability from within the IPython Notebook web app is planned.
490
491 The command-line syntax to run the ``nbconvert`` script is::
492
493 $ ipython nbconvert --format=FORMAT notebook.ipynb
494
495 This will convert the IPython document file ``notebook.ipynb`` into the output
496 format given by the ``FORMAT`` string.
497
498 The default output format is HTML, for which the ``--format`` modifier may be omitted::
499
500 $ ipython nbconvert notebook.ipynb
501
502 The currently supported export formats are the following:
503
504 * HTML:
505
506 - **full_html**:
507 Standard HTML
508
509 - **simple_html**:
510 Simplified HTML
511
512 - **reveal**:
513 HTML slideshow presentation for use with the ``reveal.js`` package
514
515 * PDF:
516
517 - **sphinx_howto**:
518 The format for Sphinx_ HOWTOs; similar to an ``article`` in LaTeX
519
520 - **sphinx_manual**:
521 The format for Sphinx_ manuals; similar to a ``book`` in LaTeX
522
523 - **latex**:
524 An article formatted completely using LaTeX
525
526 * Markup:
527
528 - **rst**:
529 reStructuredText_ markup
530
531 - **markdown**:
532 Markdown_ markup
533
534 .. _Sphinx: http://sphinx-doc.org/
535 .. _reStructuredText: http://docutils.sourceforge.net/rst.html
536
537 * Python:
538
539 Comments out all the non-Python code to produce a ``.py`` Python
540 script with just the code content. Currently the output includes IPython magics, and so can be run with ``ipython``, after changing the extension of the script to ``.ipy``.
541
542 The files output by ``nbconvert`` are all placed in a new subdirectory
543 called ``nbconvert_build``.
544
545 Each of the options for PDF export produces as an intermediate step a LaTeX
546 ``.tex`` file with the same basename as the notebook, as well as individual
547 files for each figure, and ``.text` files with textual output from running
548 code cells.
549
550 To actually produce the final PDF file, run the following commands::
551
552 $ cd nbconvert_build
553 $ pdflatex notebook
554
555 This requires a local installation of LaTeX on your machine.
556 The output is a PDF file ``notebook.pdf``, also placed inside the ``nbconvert_build`` subdirectory.
557
558 Alternatively, the output may be sent to standard output with::
559
560 $ ipython nbconvert mynotebook.ipynb --stdout
561
562 Multiple notebooks can be specified from the command line::
563
564 $ ipython nbconvert notebook*.ipynb
565 $ ipython nbconvert notebook1.ipynb notebook2.ipynb
566
567 or via a list in a configuration file, say ``mycfg.py``, containing the text::
568
569 c = get_config()
570 c.NbConvertApp.notebooks = ["notebook1.ipynb", "notebook2.ipynb"]
571
572 and using the command::
573
574 $ ipython nbconvert --config mycfg.py
575
576
577 Configuring the IPython Notebook
578 --------------------------------
579 The IPython Notebook can be run with a variety of command line arguments.
580 To see a list of available options enter::
251
581
252 $ ipython notebook --help
582 $ ipython notebook --help
253
583
254 Defaults for these options can also be set by creating a file named
584 Defaults for these options can also be set by creating a file named
255 ipython_notebook_config.py in your IPython profile folder. The profile folder is
585 ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile
256 a subfolder of your IPython directory (`ipython locate` will show you where that
586 folder is a subfolder of your IPython directory; to find out where it is
257 is). To create default configuration files (with lots of info on available
587 located, run::
258 options) use:
588
589 $ ipython locate
590
591 To create a new set of default configuration files, with lots of information
592 on available options, use::
259
593
260 $ ipython profile create
594 $ ipython profile create
261
595
@@ -264,46 +598,66 b' options) use:'
264 :ref:`config_overview`, in particular :ref:`Profiles`.
598 :ref:`config_overview`, in particular :ref:`Profiles`.
265
599
266
600
267 Keyboard use
601 Extracting standard Python files from notebooks
268 ------------
602 -----------------------------------------------
603 ``.ipynb`` notebook document files are plain text files which store a
604 representation in JSON format of the contents of a notebook space. As such,
605 they are not valid ``.py`` Python scripts, and so can be neither imported
606 directly with ``import`` in Python, nor run directly as a standard Python
607 script (though both of these are possible with simple workarounds).
269
608
270 All actions in the notebook can be achieved with the mouse, but we have also
609
271 added keyboard shortcuts for the most common ones, so that productive use of
610 To extract the Python code from within a notebook document, the simplest method is to use the ``File | Download as | Python (.py)`` menu item; the resulting ``.py`` script will be downloaded to your browser's default download location.
272 the notebook can be achieved with minimal mouse intervention. The main
611
273 key bindings you need to remember are:
612 An alternative is to pass an argument to the IPython Notebook, from the moment
274
613 when it is originally started, specifying that whenever it saves an ``.ipynb``
275 * :kbd:`Shift-Enter`: execute the current cell (similar to the Qt console),
614 notebook document, it should, at the same time, save the corresponding
276 show output (if any) and jump to the next cell below. If :kbd:`Shift-Enter`
615 ``.py`` script. To do so, you can execute the following command::
277 was invoked on the last input line, a new code cell will also be created. Note
616
278 that in the notebook, simply using :kbd:`Enter` *never* forces execution,
617 $ ipython notebook --script
279 it simply inserts a new line in the current cell. Therefore, in the notebook
618
280 you must always use :kbd:`Shift-Enter` to get execution (or use the mouse and
619 or you can set this option permanently in your configuration file with::
281 click on the ``Run Selected`` button).
620
282
621 c = get_config()
283 * :kbd:`Alt-Enter`: this combination is similar to the previous one, with the
622 c.NotebookManager.save_script=True
284 exception that, if the next cell below is not empty, a new code cell will be
623
285 added to the notebook, even if the cell execution happens not in the last cell.
624 The result is that standard ``.py`` files are also now generated, which
286 In this regard, :kbd:`Alt-Enter`: is simply a shortcut for the :kbd:`Shift-Enter`,
625 can be ``%run``, imported from regular IPython sessions or other notebooks, or
287 :kbd:`Ctrl-m a` sequence.
626 executed at the command line, as usual. Since the raw code you have typed is
627 exported, you must avoid using syntax such as IPython magics and other IPython-
628 specific extensions to the language for the files to be able to be
629 successfully imported; or you can change the script's extension to ``.ipy`` and run it with::
630
631 $ ipython script.ipy
632
633 In normal Python practice, the standard way to differentiate importable code
634 in a Python script from the "executable" part of a script is to use the
635 following idiom at the start of the executable part of the code::
636
637
638 if __name__ == '__main__'
639
640 # rest of the code...
288
641
289 * :kbd:`Ctrl-Enter`: execute the current cell in "terminal mode", where any
642 Since all cells in the notebook are run as top-level code, you will need to
290 output is shown but the cursor stays in the current cell, whose input
643 similarly protect *all* cells that you do not want executed when other scripts
291 area is flushed empty. This is convenient to do quick in-place experiments
644 try to import your notebook. A convenient shortand for this is to define early
292 or query things like filesystem content without creating additional cells you
645 on::
293 may not want saved in your notebook.
294
646
295 * :kbd:`Ctrl-m`: this is the prefix for all other keybindings, which consist
647 script = __name__ == '__main__'
296 of an additional single letter. Type :kbd:`Ctrl-m h` (that is, the sole
648
297 letter :kbd:`h` after :kbd:`Ctrl-m`) and IPython will show you the remaining
649 Then in any cell that you need to protect, use::
298 available keybindings.
650
651 if script:
652 # rest of the cell...
299
653
300
654
301 .. _notebook_security:
655 .. _notebook_security:
302
656
303 Security
657 Security
304 ========
658 --------
305
659
306 You can protect your notebook server with a simple single-password by
660 You can protect your Notebook server with a simple single password by
307 setting the :attr:`NotebookApp.password` configurable. You can prepare a
661 setting the :attr:`NotebookApp.password` configurable. You can prepare a
308 hashed password using the function :func:`IPython.lib.security.passwd`:
662 hashed password using the function :func:`IPython.lib.security.passwd`:
309
663
@@ -324,51 +678,81 b' hashed password using the function :func:`IPython.lib.security.passwd`:'
324 You can then add this to your :file:`ipython_notebook_config.py`, e.g.::
678 You can then add this to your :file:`ipython_notebook_config.py`, e.g.::
325
679
326 # Password to use for web authentication
680 # Password to use for web authentication
327 c.NotebookApp.password = u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
681 c = get_config()
682 c.NotebookApp.password =
683 u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
328
684
329 When using a password, it is a good idea to also use SSL, so that your password
685 When using a password, it is a good idea to also use SSL, so that your password
330 is not sent unencrypted by your browser. You can start the notebook to
686 is not sent unencrypted by your browser. You can start the notebook to
331 communicate via a secure protocol mode using a self-signed certificate by
687 communicate via a secure protocol mode using a self-signed certificate with
332 typing::
688 the command::
333
689
334 $ ipython notebook --certfile=mycert.pem
690 $ ipython notebook --certfile=mycert.pem
335
691
336 .. note::
692 .. note::
337
693
338 A self-signed certificate can be generated with openssl. For example, the
694 A self-signed certificate can be generated with ``openssl``. For example,
339 following command will create a certificate valid for 365 days with both
695 the following command will create a certificate valid for 365 days with
340 the key and certificate data written to the same file::
696 both the key and certificate data written to the same file::
341
697
342 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
698 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.
699 pem -out mycert.pem
343
700
344 Your browser will warn you of a dangerous certificate because it is
701 Your browser will warn you of a dangerous certificate because it is
345 self-signed. If you want to have a fully compliant certificate that will not
702 self-signed. If you want to have a fully compliant certificate that will not
346 raise warnings, it is possible (but rather involved) to obtain one for free,
703 raise warnings, it is possible (but rather involved) to obtain one,
347 `as explained in detailed in this tutorial`__.
704 `as explained in detailed in this tutorial`__.
348
705
349 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-secure-sertificate-for-free.ars
706 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-
707 secure-sertificate-for-free.ars
350
708
351 Keep in mind that when you enable SSL support, you'll need to access the
709 Keep in mind that when you enable SSL support, you will need to access the
352 notebook server over ``https://``, not over plain ``http://``. The startup
710 notebook server over ``https://``, not over plain ``http://``. The startup
353 message from the server prints this, but it's easy to overlook and think the
711 message from the server prints this, but it is easy to overlook and think the
354 server is for some reason non-responsive.
712 server is for some reason non-responsive.
355
713
356 Quick how to's
714
357 ==============
715 Connecting to an existing kernel
716 ---------------------------------
717
718 The notebook server always prints to the terminal the full details of
719 how to connect to each kernel, with messages such as the following::
720
721 [IPKernelApp] To connect another client to this kernel, use:
722 [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
723
724 This long string is the name of a JSON file that contains all the port and
725 validation information necessary to connect to the kernel. You can then, for
726 example, manually start a Qt console connected to the *same* kernel with::
727
728 $ ipython qtconsole --existing
729 kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
730
731 If you have only a single kernel running, simply typing::
732
733 $ ipython qtconsole --existing
734
735 will automatically find it. (It will always find the most recently
736 started kernel if there is more than one.) You can also request this
737 connection data by typing ``%connect_info``; this will print the same
738 file information as well as the content of the JSON data structure it contains.
739
358
740
359 Running a public notebook server
741 Running a public notebook server
360 --------------------------------
742 --------------------------------
361
743
362 If you want to access your notebook server remotely with just a web browser,
744 If you want to access your notebook server remotely via a web browser,
363 here is a quick set of instructions. Start by creating a certificate file and
745 you can do the following.
364 a hashed password as explained above. Then, create a custom profile for the
365 notebook. At the command line, type::
366
746
367 ipython profile create nbserver
747 Start by creating a certificate file and a hashed password, as explained
748 above. Then create a custom profile for the notebook, with the following
749 command line, type::
368
750
369 In the profile directory, edit the file ``ipython_notebook_config.py``. By
751 $ ipython profile create nbserver
370 default the file has all fields commented, the minimum set you need to
752
371 uncomment and edit is here::
753 In the profile directory just created, edit the file
754 ``ipython_notebook_config.py``. By default, the file has all fields
755 commented; the minimum set you need to uncomment and edit is the following::
372
756
373 c = get_config()
757 c = get_config()
374
758
@@ -379,8 +763,8 b' uncomment and edit is here::'
379 c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
763 c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
380 c.NotebookApp.ip = '*'
764 c.NotebookApp.ip = '*'
381 c.NotebookApp.open_browser = False
765 c.NotebookApp.open_browser = False
382 c.NotebookApp.password = u'sha1:bcd259ccf...your hashed password here'
766 c.NotebookApp.password = u'sha1:bcd259ccf...[your hashed password here]'
383 # It's a good idea to put it on a known, fixed port
767 # It is a good idea to put it on a known, fixed port
384 c.NotebookApp.port = 9999
768 c.NotebookApp.port = 9999
385
769
386 You can then start the notebook and access it later by pointing your browser to
770 You can then start the notebook and access it later by pointing your browser to
@@ -389,12 +773,12 b' You can then start the notebook and access it later by pointing your browser to'
389 Running with a different URL prefix
773 Running with a different URL prefix
390 -----------------------------------
774 -----------------------------------
391
775
392 The notebook dashboard (i.e. the default landing page with an overview
776 The notebook dashboard (the landing page with an overview
393 of all your notebooks) typically lives at a URL path of
777 of the notebooks in your working directory) typically lives at the URL
394 "http://localhost:8888/". If you want to have it, and the rest of the
778 ``http://localhost:8888/``. If you prefer that it lives, together with the
395 notebook, live under a sub-directory,
779 rest of the notebook, under a sub-directory,
396 e.g. "http://localhost:8888/ipython/", you can do so with
780 e.g. ``http://localhost:8888/ipython/``, you can do so with
397 configuration options like these (see above for instructions about
781 configuration options like the following (see above for instructions about
398 modifying ``ipython_notebook_config.py``)::
782 modifying ``ipython_notebook_config.py``)::
399
783
400 c.NotebookApp.base_project_url = '/ipython/'
784 c.NotebookApp.base_project_url = '/ipython/'
@@ -404,77 +788,128 b' modifying ``ipython_notebook_config.py``)::'
404 Using a different notebook store
788 Using a different notebook store
405 --------------------------------
789 --------------------------------
406
790
407 By default the notebook server stores notebooks as files in the working
791 By default, the Notebook app stores the notebook documents that it saves as
408 directory of the notebook server, also known as the ``notebook_dir``. This
792 files in the working directory of the Notebook app, also known as the
409 logic is implemented in the :class:`FileNotebookManager` class. However, the
793 ``notebook_dir``. This logic is implemented in the
410 server can be configured to use a different notebook manager class, which can
794 :class:`FileNotebookManager` class. However, the server can be configured to
411 store the notebooks in a different format. Currently, we ship a
795 use a different notebook manager class, which can
412 :class:`AzureNotebookManager` class that stores notebooks in Azure blob
796 store the notebooks in a different format.
413 storage. This can be used by adding the following lines to your
797
798 Currently, we ship a :class:`AzureNotebookManager` class that stores notebooks
799 in Azure blob storage. This can be used by adding the following lines to your
414 ``ipython_notebook_config.py`` file::
800 ``ipython_notebook_config.py`` file::
415
801
416 c.NotebookApp.notebook_manager_class = 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager'
802 c.NotebookApp.notebook_manager_class =
803 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager'
417 c.AzureNotebookManager.account_name = u'paste_your_account_name_here'
804 c.AzureNotebookManager.account_name = u'paste_your_account_name_here'
418 c.AzureNotebookManager.account_key = u'paste_your_account_key_here'
805 c.AzureNotebookManager.account_key = u'paste_your_account_key_here'
419 c.AzureNotebookManager.container = u'notebooks'
806 c.AzureNotebookManager.container = u'notebooks'
420
807
421 In addition to providing your Azure Blob Storage account name and key, you will
808 In addition to providing your Azure Blob Storage account name and key, you
422 have to provide a container name; you can use multiple containers to organize
809 will have to provide a container name; you can use multiple containers to
423 your Notebooks.
810 organize your notebooks.
424
811
425 .. _notebook_format:
812 .. _notebook_format:
426
813
427 The notebook format
814 Notebook JSON file format
428 ===================
815 -------------------------
429
816 Notebook documents are JSON files with an ``.ipynb`` extension, formatted
430 The notebooks themselves are JSON files with an ``ipynb`` extension, formatted
431 as legibly as possible with minimal extra indentation and cell content broken
817 as legibly as possible with minimal extra indentation and cell content broken
432 across lines to make them reasonably friendly to use in version-control
818 across lines to make them reasonably friendly to use in version-control
433 workflows. You should be very careful if you ever edit manually this JSON
819 workflows. You should be very careful if you ever manually edit this JSON
434 data, as it is extremely easy to corrupt its internal structure and make the
820 data, as it is extremely easy to corrupt its internal structure and make the
435 file impossible to load. In general, you should consider the notebook as a
821 file impossible to load. In general, you should consider the notebook as a
436 file meant only to be edited by IPython itself, not for hand-editing.
822 file meant only to be edited by the IPython Notebook app itself, not for
823 hand-editing.
437
824
438 .. note::
825 .. note::
439
826
440 Binary data such as figures are directly saved in the JSON file. This
827 Binary data such as figures are also saved directly in the JSON file.
441 provides convenient single-file portability but means the files can be
828 This provides convenient single-file portability, but means that the
442 large and diffs of binary data aren't very meaningful. Since the binary
829 files can be large; a ``diff`` of binary data is also not very
443 blobs are encoded in a single line they only affect one line of the diff
830 meaningful. Since the binary blobs are encoded in a single line, they
444 output, but they are typically very long lines. You can use the
831 affect only one line of the ``diff`` output, but they are typically very
445 'ClearAll' button to remove all output from a notebook prior to
832 long lines. You can use the ``Cell | All Output | Clear`` menu option to
446 committing it to version control, if this is a concern.
833 remove all output from a notebook prior to committing it to version
447
834 control, if this is a concern.
448 The notebook server can also generate a pure-python version of your notebook,
835
449 by clicking on the 'Download' button and selecting ``py`` as the format. This
836 The notebook server can also generate a pure Python version of your notebook,
450 file will contain all the code cells from your notebook verbatim, and all text
837 using the ``File | Download as`` menu option. The resulting ``.py`` file will
451 cells prepended with a comment marker. The separation between code and text
838 contain all the code cells from your notebook verbatim, and all Markdown cells
839 prepended with a comment marker. The separation between code and Markdown
452 cells is indicated with special comments and there is a header indicating the
840 cells is indicated with special comments and there is a header indicating the
453 format version. All output is stripped out when exporting to python.
841 format version. All output is removed when exporting to Python.
454
842
455 Here is an example of a simple notebook with one text cell and one code input
843 As an example, consider a simple notebook called ``simple.ipynb`` which
456 cell, when exported to python format::
844 contains one Markdown cell, with the content ``The simplest notebook.``, one
457
845 code input cell with the content ``print "Hello, IPython!"``, and the
458 # <nbformat>2</nbformat>
846 corresponding output.
459
847
460 # <markdowncell>
848 The contents of the notebook document ``simple.ipynb`` is the following JSON
461
849 container::
462 # A text cell
850
463
851 {
464 # <codecell>
852 "metadata": {
465
853 "name": "simple"
466 print "hello IPython"
854 },
855 "nbformat": 3,
856 "nbformat_minor": 0,
857 "worksheets": [
858 {
859 "cells": [
860 {
861 "cell_type": "markdown",
862 "metadata": {},
863 "source": "The simplest notebook."
864 },
865 {
866 "cell_type": "code",
867 "collapsed": false,
868 "input": "print \"Hello, IPython\"",
869 "language": "python",
870 "metadata": {},
871 "outputs": [
872 {
873 "output_type": "stream",
874 "stream": "stdout",
875 "text": "Hello, IPython\n"
876 }
877 ],
878 "prompt_number": 1
879 }
880 ],
881 "metadata": {}
882 }
883 ]
884 }
885
886
887 The corresponding Python script is::
888
889 # -*- coding: utf-8 -*-
890 # <nbformat>3.0</nbformat>
891
892 # <markdowncell>
893
894 # The simplest notebook.
895
896 # <codecell>
897
898 print "Hello, IPython"
899
900 Note that indeed the output of the code cell, which is present in the JSON
901 container, has been removed in the ``.py`` script.
467
902
468
903
469 Known issues
904 Known issues
470 ============
905 ------------
471
906
472 When behind a proxy, especially if your system or browser is set to autodetect
907 When behind a proxy, especially if your system or browser is set to autodetect
473 the proxy, the html notebook might fail to connect to the server's websockets,
908 the proxy, the Notebook app might fail to connect to the server's websockets,
474 and present you with a warning at startup. In this case, you need to configure
909 and present you with a warning at startup. In this case, you need to configure
475 your system not to use the proxy for the server's address.
910 your system not to use the proxy for the server's address.
476
911
477 In Firefox, for example, go to the Preferences panel, Advanced section,
912 For example, in Firefox, go to the Preferences panel, Advanced section,
478 Network tab, click 'Settings...', and add the address of the notebook server
913 Network tab, click 'Settings...', and add the address of the notebook server
479 to the 'No proxy for' field.
914 to the 'No proxy for' field.
480
915
General Comments 0
You need to be logged in to leave comments. Login now