##// END OF EJS Templates
notebook docs touchup...
David P. Sanders -
Show More
@@ -1,212 +1,212 b''
1 1 .. _nbconvert:
2 2
3 3 Converting notebooks to other formats
4 4 =====================================
5 5
6 6 Newly added in the 1.0 release of IPython is the ``nbconvert`` tool, which
7 7 allows you to convert an ``.ipynb`` notebook document file into various static
8 8 formats.
9 9
10 10 Currently, ``nbconvert`` is provided as a command line tool, run as a script
11 11 using IPython. A direct export capability from within the
12 12 IPython Notebook web app is planned.
13 13
14 14 The command-line syntax to run the ``nbconvert`` script is::
15 15
16 16 $ ipython nbconvert --to FORMAT notebook.ipynb
17 17
18 18 This will convert the IPython document file ``notebook.ipynb`` into the output
19 19 format given by the ``FORMAT`` string.
20 20
21 21 The default output format is html, for which the ``--to`` argument may be
22 22 omitted::
23 23
24 24 $ ipython nbconvert notebook.ipynb
25 25
26 26 IPython provides a few templates for some output formats, and these can be
27 27 specified via an additional ``--template`` argument.
28 28
29 29 The currently supported export formats are:
30 30
31 31 * ``--to html``
32 32
33 33 - ``--template full`` (default)
34 34
35 35 A full static HTML render of the notebook.
36 36 This looks very similar to the interactive view.
37 37
38 38 - ``--template basic``
39 39
40 40 Simplified HTML, useful for embedding in webpages, blogs, etc.
41 41 This excludes HTML headers.
42 42
43 43 * ``--to latex``
44 44
45 45 Latex export. This generates ``NOTEBOOK_NAME.tex`` file,
46 46 ready for export. You can automatically run latex on it to generate a PDF
47 47 by adding ``--post PDF``.
48 48
49 49 - ``--template article`` (default)
50 50
51 51 Latex article, derived from Sphinx's howto template.
52 52
53 53 - ``--template book``
54 54
55 55 Latex book, derived from Sphinx's manual template.
56 56
57 57 - ``--template basic``
58 58
59 59 Very basic latex output - mainly meant as a starting point for custom templates.
60 60
61 61 * ``--to slides``
62 62
63 63 This generates a Reveal.js HTML slideshow.
64 64 It must be served by an HTTP server. The easiest way to get this is to add
65 65 ``--post serve`` on the command-line.
66 66
67 67 * ``--to markdown``
68 68
69 69 Simple markdown output. Markdown cells are unaffected,
70 and code cells are placed in triple-backtick (``\`\`\```) blocks.
70 and code cells are placed in triple-backtick (```````) blocks.
71 71
72 72 * ``--to rst``
73 73
74 74 Basic reStructuredText output. Useful as a starting point for embedding notebooks
75 75 in Sphinx docs.
76 76
77 77 * ``--to python``
78 78
79 79 Convert a notebook to an executable Python script.
80 80 This is the simplest way to get a Python script out of a notebook.
81 81 If there were any magics in the notebook, this may only be executable from
82 82 an IPython session.
83 83
84 84 .. note::
85 85
86 86 nbconvert uses pandoc_ to convert between various markup languages,
87 87 so pandoc is a dependency of most nbconvert transforms,
88 88 excluding Markdown and Python.
89 89
90 90 .. _pandoc: http://johnmacfarlane.net/pandoc/
91 91
92 92 The output file created by ``nbconvert`` will have the same base name as
93 93 the notebook and will be placed in the current working directory. Any
94 94 supporting files (graphics, etc) will be placed in a new directory with the
95 95 same base name as the notebook, suffixed with ``_files``::
96 96
97 97 $ ipython nbconvert notebook.ipynb
98 98 $ ls
99 99 notebook.ipynb notebook.html notebook_files/
100 100
101 101 For simple single-file output, such as html, markdown, etc.,
102 102 the output may be sent to standard output with::
103 103
104 104 $ ipython nbconvert --to markdown notebook.ipynb --stdout
105 105
106 106 Multiple notebooks can be specified from the command line::
107 107
108 108 $ ipython nbconvert notebook*.ipynb
109 109 $ ipython nbconvert notebook1.ipynb notebook2.ipynb
110 110
111 111 or via a list in a configuration file, say ``mycfg.py``, containing the text::
112 112
113 113 c = get_config()
114 114 c.NbConvertApp.notebooks = ["notebook1.ipynb", "notebook2.ipynb"]
115 115
116 116 and using the command::
117 117
118 118 $ ipython nbconvert --config mycfg.py
119 119
120 120
121 121 .. _notebook_format:
122 122
123 123 Notebook JSON file format
124 124 -------------------------
125 125
126 126 Notebook documents are JSON files with an ``.ipynb`` extension, formatted
127 127 as legibly as possible with minimal extra indentation and cell content broken
128 128 across lines to make them reasonably friendly to use in version-control
129 129 workflows. You should be very careful if you ever manually edit this JSON
130 130 data, as it is extremely easy to corrupt its internal structure and make the
131 131 file impossible to load. In general, you should consider the notebook as a
132 132 file meant only to be edited by the IPython Notebook app itself, not for
133 133 hand-editing.
134 134
135 135 .. note::
136 136
137 137 Binary data such as figures are also saved directly in the JSON file.
138 138 This provides convenient single-file portability, but means that the
139 139 files can be large; a ``diff`` of binary data is also not very
140 140 meaningful. Since the binary blobs are encoded in a single line, they
141 141 affect only one line of the ``diff`` output, but they are typically very
142 142 long lines. You can use the ``Cell | All Output | Clear`` menu option to
143 143 remove all output from a notebook prior to committing it to version
144 144 control, if this is a concern.
145 145
146 146 The notebook server can also generate a pure Python version of your notebook,
147 147 using the ``File | Download as`` menu option. The resulting ``.py`` file will
148 148 contain all the code cells from your notebook verbatim, and all Markdown cells
149 149 prepended with a comment marker. The separation between code and Markdown
150 150 cells is indicated with special comments and there is a header indicating the
151 151 format version. All output is removed when exporting to Python.
152 152
153 153 As an example, consider a simple notebook called ``simple.ipynb`` which
154 154 contains one Markdown cell, with the content ``The simplest notebook.``, one
155 155 code input cell with the content ``print "Hello, IPython!"``, and the
156 156 corresponding output.
157 157
158 158 The contents of the notebook document ``simple.ipynb`` is the following JSON
159 159 container::
160 160
161 161 {
162 162 "metadata": {
163 163 "name": "simple"
164 164 },
165 165 "nbformat": 3,
166 166 "nbformat_minor": 0,
167 167 "worksheets": [
168 168 {
169 169 "cells": [
170 170 {
171 171 "cell_type": "markdown",
172 172 "metadata": {},
173 173 "source": "The simplest notebook."
174 174 },
175 175 {
176 176 "cell_type": "code",
177 177 "collapsed": false,
178 178 "input": "print \"Hello, IPython\"",
179 179 "language": "python",
180 180 "metadata": {},
181 181 "outputs": [
182 182 {
183 183 "output_type": "stream",
184 184 "stream": "stdout",
185 185 "text": "Hello, IPython\n"
186 186 }
187 187 ],
188 188 "prompt_number": 1
189 189 }
190 190 ],
191 191 "metadata": {}
192 192 }
193 193 ]
194 194 }
195 195
196 196
197 197 The corresponding Python script is::
198 198
199 199 # -*- coding: utf-8 -*-
200 200 # <nbformat>3.0</nbformat>
201 201
202 202 # <markdowncell>
203 203
204 204 # The simplest notebook.
205 205
206 206 # <codecell>
207 207
208 208 print "Hello, IPython"
209 209
210 210 Note that indeed the output of the code cell, which is present in the JSON
211 211 container, has been removed in the ``.py`` script.
212 212
@@ -1,581 +1,590 b''
1 1 .. _htmlnotebook:
2 2
3 3 The IPython Notebook
4 4 ====================
5 5
6 6 The IPython Notebook is part of the IPython package, which aims to provide a
7 7 powerful, interactive approach to scientific computation.
8 8 The IPython Notebook extends the previous text-console-based approach, and the
9 9 later Qt console, in a qualitatively new diretion, providing a web-based
10 10 application suitable for capturing the whole scientific computation process.
11 11
12 12 .. seealso::
13 13
14 14 :ref:`Installation requirements <installnotebook>` for the Notebook.
15 15
16 16
17 17 .. Basic structure
18 18 .. ---------------
19 19
20 20 Introduction
21 21 ------------
22 22
23 23 The IPython Notebook combines two components:
24 24
25 25 * **The IPython Notebook web application**:
26 26
27 27 The *IPython Notebook web app* is a browser-based tool for interactive
28 28 authoring of literate computations, in which explanatory text,
29 29 mathematics, computations and rich media output may be combined. Input
30 30 and output are stored in persistent cells that may be edited in-place.
31 31
32 32 * **Notebook documents**:
33 33
34 34 *Notebook documents*, or *notebooks*, are plain text documents which
35 35 record all inputs and outputs of the computations, interspersed with
36 36 text, mathematics and HTML 5 representations of objects, in a literate
37 37 style.
38 38
39 39 Since the similarity in names can lead to some confusion, in this
40 40 documentation we will use capitalization of the word "notebook" to
41 41 distinguish the Notebook app and notebook documents, thinking of the
42 42 Notebook app as being a proper noun. We will also always refer to the
43 43 "Notebook app" when we are referring to the browser-based interface,
44 44 and usually to "notebook documents", instead of "notebooks", for added
45 45 precision.
46 46
47 47 We refer to the current state of the computational process taking place in the
48 48 Notebook app, i.e. the (numbered) sequence of input and output cells, as the
49 49 *notebook space*. Notebook documents provide an *exact*, *one-to-one* record
50 50 of all the content in the notebook space, as a plain text file in JSON format.
51 51 The Notebook app automatically saves, at certain intervals, the contents of
52 52 the notebook space to a notebook document stored on disk, with the same name
53 53 as the title of the notebook space, and the file extension ``.ipynb``. For
54 54 this reason, there is no confusion about using the same word "notebook" for
55 55 both the notebook space and the corresponding notebook document, since they are
56 56 really one and the same concept (we could say that they are "isomorphic").
57 57
58 58
59 59 Main features of the IPython Notebook web app
60 60 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61 61
62 62 The main features of the IPython Notebook app include:
63 63
64 64 * In-browser editing for code, with automatic syntax highlighting and
65 65 indentation and tab completion/introspection.
66 66
67 67 * Literate combination of code with rich text using the Markdown_ markup
68 68 language.
69 69
70 70 * Mathematics is easily included within the Markdown using LaTeX notation, and
71 71 rendered natively by MathJax_.
72 72
73 73 * Displays rich data representations (e.g. HTML / LaTeX / SVG) as the result
74 74 of computations.
75 75
76 76 * Publication-quality figures in a range of formats (SVG / PNG), rendered by
77 77 the matplotlib_ library, may be included inline and exported.
78 78
79 79
80 80 .. _MathJax: http://www.mathjax.org/
81 81 .. _matplotlib: http://matplotlib.org/
82 82 .. _Markdown: http://daringfireball.net/projects/markdown/syntax
83 83
84 84
85 85 Notebook documents
86 86 ~~~~~~~~~~~~~~~~~~
87 87
88 88 Notebook document files are simple JSON_ files with the
89 89 extension ``.ipynb``.
90 90 Since JSON is just plain text, they can be easily version-controlled and shared with colleagues.
91 91 The notebook stores a *complete*, *reproducible*, *one-to-one* copy of the state of the
92 92 computational state as it is inside the Notebook app. All computations
93 93 carried out, and the corresponding results obtained, can be combined in
94 94 a literate way, interleaving executable code with rich text, mathematics,
95 95 and rich representations of objects.
96 96
97 97 .. _JSON: http://en.wikipedia.org/wiki/JSON
98 98
99 99 Notebooks may easily be exported to a range of static formats, including
100 100 HTML (for example, for blog posts), PDF and slide shows,
101 101 via the new nbconvert_ command.
102 102
103 103 Furthermore, any ``.ipynb`` notebook document available from a public
104 104 URL can be shared via the `IPython Notebook Viewer <nbviewer>`_ service.
105 This service loads the notebook document from the URL and will
106 render it as a static web page. The results may thus be shared with a
105 This service loads the notebook document from the URL and renders
106 it as a static web page. The results may thus be shared with a
107 107 colleague, or as a public blog post, without other users needing to install
108 IPython themselves. NbViewer is simply NbConvert as a simple heroku webservice.
108 IPython themselves. NbViewer is simply nbconvert_ as a simple webservice.
109 109
110 110 See the :ref:`installation documentation <install_index>` for directions on
111 111 how to install the notebook and its dependencies.
112 112
113 .. _nbconvert: ./nbconvert.html
114
113 115 .. _nbviewer: http://nbviewer.ipython.org
114 116
115 117 .. note::
116 118
117 119 You can start more than one notebook server at the same time, if you want
118 120 to work on notebooks in different directories. By default the first
119 121 notebook server starts on port 8888, and later notebook servers search for
120 122 ports near that one. You can also manually specify the port with the
121 123 ``--port`` option.
122 124
123 125
124 126 Basic workflow in the IPython Notebook web app
125 127 ----------------------------------------------
126 128
127 129 Starting up
128 130 ~~~~~~~~~~~~
129 131
130 132 You can start running the Notebook web app using the following command::
131 133
132 134 $ ipython notebook
133 135
134 136 (Here, and in the sequel, the initial ``$`` represents the shell prompt,
135 137 indicating that the command is to be run from the command line in a shell.)
136 138
137 139 The landing page of the IPython Notebook application, the *dashboard*, shows
138 140 the notebooks currently available in the *notebook directory* (By default, the directory
139 141 from which the notebook was started).
140 142 You can create new notebooks from the dashboard with the ``New Notebook``
141 143 button, or open existing ones by clicking on their name.
142 144 You can also drag and drop ``.ipynb`` notebooks and standard ``.py`` Python
143 145 source code files into the notebook list area.
144 146
145 147
146 148 You can open an existing notebook directly, without having to go via the
147 dashboard, with:
149 dashboard, with::
148 150
149 151 ipython notebook my_notebook
150 152
151 The `.ipynb` extension is assumed if no extension is given.
153 The ``.ipynb`` extension is assumed if no extension is given.
152 154
153 155 The `File | Open...` menu option will open the dashboard in a new browser tab,
154 156 to allow you to select a current notebook
155 157 from the notebook directory or to create a new notebook.
156 158
157 159
158 160
159 161 Notebook user interface
160 162 ~~~~~~~~~~~~~~~~~~~~~~~
161 163
162 164 When you open a new notebook document in the Notebook, you will be presented
163 165 with the title associated to the notebook space/document, a *menu bar*, a
164 166 *toolbar* and an empty *input cell*.
165 167
166 168 Notebook title
167 169 ^^^^^^^^^^^^^^
168 170 The title of the notebook document that is currently being edited is displayed
169 171 at the top of the page, next to the ``IP[y]: Notebook`` logo. This title may
170 172 be edited directly by clicking on it. The title is reflected in the name of
171 173 the ``.ipynb`` notebook document file that is saved.
172 174
173 175 Menu bar
174 176 ^^^^^^^^
175 177 The menu bar presents different options that may be used to manipulate the way
176 178 the Notebook functions.
177 179
178 180 Toolbar
179 181 ^^^^^^^
180 182 The tool bar gives a quick way of accessing the most-used operations within
181 183 the Notebook, by clicking on an icon.
182 184
183 185
184 186 Creating a new notebook document
185 187 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
186 188
187 189 A new notebook space/document may be created at any time, either from the
188 190 dashboard, or using the `File | New` menu option from within an active
189 191 notebook. The new notebook is created within the same directory and
190 192 will open in a new browser tab. It will also be reflected as a new entry in
191 193 the notebook list on the dashboard.
192 194
193 195
194 196 Structure of a notebook document
195 197 --------------------------------
196 198
197 199 Input cells
198 200 ~~~~~~~~~~~
199 201 Input cells are at the core of the functionality of the IPython Notebook.
200 202 They are regions in the document in which you can enter different types of
201 203 text and commands. To *execute* or *run* the *current cell*, i.e. the cell
202 204 under the cursor, you can use the :kbd:`Shift-Enter` key combination.
203 205 This tells the Notebook app to perform the relevant operation for each type of
204 206 cell (see below), and then to display the resulting output.
205 207
206 208 The notebook consists of a sequence of input cells, labelled ``In[n]``, which
207 209 may be executed in a non-linear way, and outputs ``Out[n]``, where ``n`` is a
208 210 number which denotes the order in which the cells were executed over the
209 211 history of the computational process. The contents of all of these cells are
210 212 accessible as Python variables with the same names, forming a complete record
211 213 of the history of the computation.
212 214
213 215
214 216
215 217 Input cell types
216 218 ~~~~~~~~~~~~~~~~
217 219 Each IPython input cell has a *cell type*, of which there is a restricted
218 220 number. The type of a cell may be set by using the cell type dropdown on the
219 221 toolbar, or via the following keyboard shortcuts:
220 222
221 223 * **code**: :kbd:`Ctrl-m y`
222 224 * **markdown**: :kbd:`Ctrl-m m`
223 225 * **raw**: :kbd:`Ctrl-m t`
224 226 * **heading**: :kbd:`Ctrl-m 1` - :kbd:`Ctrl-m 6`
225 227
226 228 Upon initial creation, each input cell is by default a code cell.
227 229
228 230
229 231 Code cells
230 232 ^^^^^^^^^^
231 233 A *code input cell* allows you to edit code inline within the cell, with full
232 234 syntax highlighting and autocompletion/introspection. By default, the language
233 235 associated to a code cell is Python, but other languages, such as ``julia``
234 236 and ``R``, can be handled using magic commands (see below).
235 237
236 238 When a code cell is executed with :kbd:`Shift-Enter`, the code that it
237 239 contains is transparently exported and run in that language (with automatic
238 240 compiling, etc., if necessary). The result that is returned from this
239 241 computation is then displayed in the notebook space as the cell's
240 242 *output*. If this output is of a textual nature, it is placed into a
241 243 numbered *output cell*. However, many other possible forms of output are also
242 244 possible, including ``matplotlib`` figures and HTML tables (as used, for
243 245 example, in the ``pandas`` data analyis package). This is known as IPython's
244 246 *rich display* capability.
245 247
246 248
247 249 Markdown cells
248 250 ^^^^^^^^^^^^^^
249 251 You can document the computational process in a literate way, alternating
250 252 descriptive text with code, using *rich text*. In IPython this is accomplished
251 253 by marking up text with the Markdown language. The corresponding cells are
252 254 called *Markdown input cells*. The Markdown language provides a simple way to
253 255 perform this text markup, that is, to specify which parts of the text should
254 256 be emphasized (italics), bold, form lists, etc.
255 257
256 258
257 259 When a Markdown input cell is executed, the Markdown code is converted into
258 260 the corresponding formatted rich text. This output then *replaces* the
259 261 original Markdown input cell, leaving just the visually-significant marked up
260 262 rich text. Markdown allows arbitrary HTML code for formatting.
261 263
262 264 Within Markdown cells, you can also include *mathematics* in a straightforward
263 265 way, using standard LaTeX notation: ``$...$`` for inline mathematics and
264 266 ``$$...$$`` for displayed mathematics. When the Markdown cell is executed,
265 267 the LaTeX portions are automatically rendered in the HTML output as equations
266 268 with high quality typography. This is made possible by MathJax_, which
267 269 supports a `large subset <mathjax_tex>`_ of LaTeX functionality
268 270
269 271 .. _mathjax_tex: http://docs.mathjax.org/en/latest/tex.html
270 272
271 273 Standard mathematics environments defined by LaTeX and AMS-LaTeX (the
272 274 `amsmath` package) also work, such as
273 275 ``\begin{equation}...\end{equation}``, and ``\begin{align}...\end{align}``.
274 276 New LaTeX macros may be defined using standard methods,
275 277 such as ``\newcommand``, by placing them anywhere *between math delimiters* in
276 278 a Markdown cell. These definitions are then available throughout the rest of
277 279 the IPython session. (Note, however, that more care must be taken when using
278 280 nbconvert_ to output to LaTeX).
279 281
280 282 Raw input cells
281 283 ~~~~~~~~~~~~~~~
282 284
283 285 *Raw* input cells provide a place in which you can write *output* directly.
284 286 Raw cells are not evaluated by the Notebook, and have no output.
285 287 When passed through nbconvert, Raw cells arrive in the destination format unmodified,
286 288 allowing you to type full latex into a raw cell, which will only be rendered
287 289 by latex after conversion by nbconvert.
288 290
289 291 Heading cells
290 292 ~~~~~~~~~~~~~
291 293
292 294 You can provide a conceptual structure for your computational document as a
293 295 whole using different levels of headings; there are 6 levels available, from
294 296 level 1 (top level) down to level 6 (paragraph). These can be used later for
295 297 constructing tables of contents, etc.
296 298
297 299 As with Markdown cells, a heading input cell is replaced by a rich text
298 300 rendering of the heading when the cell is executed.
299 301
300 302
301 303 Basic workflow
302 304 --------------
303 305
304 306 The normal workflow in a notebook is, then, quite similar to a standard
305 307 IPython session, with the difference that you can edit cells in-place multiple
306 308 times until you obtain the desired results, rather than having to
307 309 rerun separate scripts with the ``%run`` magic command. (Magic commands do,
308 310 however, also work in the notebook; see below).
309 311
310 312 Typically, you will work on a computational problem in pieces, organizing
311 313 related ideas into cells and moving forward once previous parts work
312 314 correctly. This is much more convenient for interactive exploration than
313 315 breaking up a computation into scripts that must be executed together, as was
314 316 previously necessary, especially if parts of them take a long time to run
315 317
316 The only significant limitation that the Notebook currently has, compared to
317 the Qt console, is that it cannot run any code that expects input from the
318 kernel (such as scripts that call :func:`raw_input`). Very importantly, this
319 means that the ``%debug`` magic does *not* currently work in the notebook!
320
321 This limitation will be overcome in the future, but in the meantime, there is
322 a simple solution for debugging: you can attach a Qt console to your existing
323 notebook kernel, and run ``%debug`` from the Qt console.
324 If your notebook is running on a local computer (i.e. if you are accessing it
325 via your localhost address at ``127.0.0.1``), then you can just type
326 ``%qtconsole`` in the notebook and a Qt console will open up, connected to
327 that same kernel.
328
329 318 At certain moments, it may be necessary to interrupt a calculation which is
330 319 taking too long to complete. This may be done with the ``Kernel | Interrupt``
331 320 menu option, or the :kbd:``Ctrl-i`` keyboard shortcut.
332 321 Similarly, it may be necessary or desirable to restart the whole computational
333 322 process, with the ``Kernel | Restart`` menu option or :kbd:``Ctrl-.``
334 323 shortcut. This gives an equivalent state to loading the notebook document
335 324 afresh.
336 325
337
326 A notebook may be downloaded in either ``.ipynb`` or raw ``.py`` form from the
327 menu option ``File | Download as``. Choosing the ``.py`` option downloads a
328 Python ``.py`` script, in which all output has been removed and the content of
329 Markdown cells in comment areas. See ref:`below <notebook_format>` for more
330 details on the notebook format.
331
338 332 .. warning::
339 333
340 334 While in simple cases you can "roundtrip" a notebook to Python, edit the
341 335 Python file, and then import it back without loss of main content, this is
342 336 in general *not guaranteed to work*. First, there is extra metadata
343 337 saved in the notebook that may not be saved to the ``.py`` format. And as
344 338 the notebook format evolves in complexity, there will be attributes of the
345 339 notebook that will not survive a roundtrip through the Python form. You
346 340 should think of the Python format as a way to output a script version of a
347 341 notebook and the import capabilities as a way to load existing code to get
348 342 a notebook started. But the Python version is *not* an alternate notebook
349 343 format.
350 344
351 345
352 346 Keyboard shortcuts
353 347 ~~~~~~~~~~~~~~~~~~
354 348 All actions in the notebook can be performed with the mouse, but keyboard
355 349 shortcuts are also available for the most common ones. The essential shortcuts
356 350 to remember are the following:
357 351
358 352 * :kbd:`Shift-Enter`: run cell
359 353 Execute the current cell, show output (if any), and jump to the next cell
360 354 below. If :kbd:`Shift-Enter` is invoked on the last input cell, a new code
361 355 cell will also be created. Note that in the notebook, typing :kbd:`Enter`
362 356 on its own *never* forces execution, but rather just inserts a new line in
363 357 the current input cell. :kbd:`Shift-Enter` is equivalent to clicking the
364 358 ``Cell | Run`` menu item.
365 359
366 360 * :kbd:`Ctrl-Enter`: run cell in-place
367 361 Execute the current cell as if it were in "terminal mode", where any
368 362 output is shown, but the cursor *remains* in the current cell. The cell's
369 363 entire contents are selected after execution, so you can just start typing
370 364 and only the new input will be in the cell. This is convenient for doing
371 365 quick experiments in place, or for querying things like filesystem
372 366 content, without needing to create additional cells that you may not want
373 367 to be saved in the notebook.
374 368
375 369 * :kbd:`Alt-Enter`: run cell, insert below
376 370 Executes the current cell, shows the output, and inserts a *new* input
377 371 cell between the current cell and the cell below (if one exists). This
378 372 is thus a shortcut for the sequence :kbd:`Shift-Enter`, :kbd:`Ctrl-m a`.
379 373 (:kbd:`Ctrl-m a` adds a new cell above the current one.)
380 374
381 375 * :kbd:`Ctrl-m`:
382 376 This is the prefix for *all* other shortcuts, which consist of :kbd:`Ctrl-m`
383 377 followed by a single letter or character. For example, if you type
384 378 :kbd:`Ctrl-m h` (that is, the sole letter :kbd:`h` after :kbd:`Ctrl-m`),
385 379 IPython will show you all the available keyboard shortcuts.
386 380
387 381
388 382 ..
389 383 TODO: these live in IPython/html/static/notebook/js/quickhelp.js
390 384 They were last updated for IPython 1.0 release, so update them again for
391 385 future releases.
392 386
393 387 Here is the complete set of keyboard shortcuts available:
394 388
395 389 ============ ==========================
396 390 **Shortcut** **Action**
397 391 ------------ --------------------------
398 392 Shift-Enter run cell
399 393 Ctrl-Enter run cell in-place
400 394 Alt-Enter run cell, insert below
401 395 Ctrl-m x cut cell
402 396 Ctrl-m c copy cell
403 397 Ctrl-m v paste cell
404 398 Ctrl-m d delete cell
405 399 Ctrl-m z undo last cell deletion
406 400 Ctrl-m - split cell
407 401 Ctrl-m a insert cell above
408 402 Ctrl-m b insert cell below
409 403 Ctrl-m o toggle output
410 404 Ctrl-m O toggle output scroll
411 405 Ctrl-m l toggle line numbers
412 406 Ctrl-m s save notebook
413 407 Ctrl-m j move cell down
414 408 Ctrl-m k move cell up
415 409 Ctrl-m y code cell
416 410 Ctrl-m m markdown cell
417 411 Ctrl-m t raw cell
418 412 Ctrl-m 1-6 heading 1-6 cell
419 413 Ctrl-m p select previous
420 414 Ctrl-m n select next
421 415 Ctrl-m i interrupt kernel
422 416 Ctrl-m . restart kernel
423 417 Ctrl-m h show keyboard shortcuts
424 418 ============ ==========================
425 419
426 420
427 421
428 422 Magic commands
429 423 --------------
430 424 Magic commands, or *magics*, are commands for controlling IPython itself.
431 425 They all begin with ``%`` and are entered into code input cells; the code
432 426 cells are executed as usual with :kbd:`Shift-Enter`.
433 427
434 428 The magic commands call special functions defined by IPython which manipulate
435 429 the computational state in certain ways.
436 430
437 431 There are two types of magics:
438 432
439 433 - **line magics**:
440 434
441 435 These begin with a single ``%`` and take as arguments the rest of the
442 436 *same line* of the code cell. Any other lines of the code cell are
443 437 treated as if they were part of a standard code cell.
444 438
445 439 - **cell magics**:
446 440
447 441 These begin with ``%%`` and operate on the *entire* remaining contents
448 442 of the code cell.
449 443
450 444 Line magics
451 445 ~~~~~~~~~~~
452 446 Some of the available line magics are the following:
453 447
454 448 * ``%load filename``:
455 449
456 450 Loads the contents of the file ``filename`` into a new code cell. This
457 451 can be a URL for a remote file.
458 452
459 453 * ``%timeit code``:
460 454
461 455 An easy way to time how long the single line of code ``code`` takes to
462 456 run
463 457
464 458 * ``%config``:
465 459
466 460 Configuration of the IPython Notebook
467 461
468 462 * ``%lsmagic``:
469 463
470 464 Provides a list of all available magic commands
471 465
472 466 Cell magics
473 467 ~~~~~~~~~~~
474 468
475 469 * ``%%latex``:
476 470
477 471 Renders the entire contents of the cell in LaTeX, without needing to use
478 472 explicit LaTeX delimiters.
479 473
480 474 * ``%%bash``:
481 475
482 476 The code cell is executed by sending it to be executed by ``bash``. The
483 477 output of the ``bash`` commands is captured and displayed in the
484 478 notebook.
485 479
486 480 * ``%%file filename``:
487 481
488 482 Writes the contents of the cell to the file ``filename``.
489 483 **Caution**: The file is over-written without warning!
490 484
491 485 * ``%%R``:
492 486
493 487 Execute the contents of the cell using the R language.
494 488
495 489 * ``%%timeit``:
496 490
497 491 Version of ``%timeit`` which times the entire block of code in the
498 492 current code cell.
499 493
500 494
501 495
502 496 Several of the cell magics provide functionality to manipulate the filesystem
503 497 of a remote server to which you otherwise do not have access.
504 498
505 499
506 500 Plotting
507 501 --------
508 502 One major feature of the Notebook is the ability to interact with
509 503 plots that are the output of running code cells. IPython is designed to work
510 504 seamlessly with the ``matplotlib`` plotting library to provide this
511 505 functionality.
512 506
513 507 To set this up, before any plotting is performed you must execute the
514 508 ``%matplotlib`` magic command. This performs the necessary behind-the-scenes
515 509 setup for IPython to work correctly hand in hand with ``matplotlib``; it does
516 510 *not*, however, actually execute any Python ``import`` commands, that is, no
517 511 names are added to the namespace.
518 512
519 513 If the ``%matplotlib`` magic is called without an argument, the
520 514 output of a plotting command is displayed using the default ``matplotlib``
521 515 backend in a separate window. Alternatively, the backend can be explicitly
522 516 requested using, for example::
523 517
524 518 %matplotlib gtk
525 519
526 520 A particularly interesting backend is the ``inline`` backend.
527 521 This is applicable only for the IPython Notebook and the IPython QtConsole.
528 522 It can be invoked as follows::
529 523
530 524 %matplotlib inline
531 525
532 526 With this backend, output of plotting commands is displayed *inline* within
533 527 the notebook format, directly below the input cell that produced it. The
534 528 resulting plots will then also be stored in the notebook document. This
535 529 provides a key part of the functionality for reproducibility_ that the IPython
536 530 Notebook provides.
537 531
538 532 .. _reproducibility: https://en.wikipedia.org/wiki/Reproducibility
539 533
540 534
541 535
542 536 Configuring the IPython Notebook
543 537 --------------------------------
544 538 The IPython Notebook can be run with a variety of command line arguments.
545 539 To see a list of available options enter::
546 540
547 541 $ ipython notebook --help
548 542
549 543 Defaults for these options can also be set by creating a file named
550 544 ``ipython_notebook_config.py`` in your IPython *profile folder*. The profile
551 545 folder is a subfolder of your IPython directory; to find out where it is
552 546 located, run::
553 547
554 548 $ ipython locate
555 549
556 550 To create a new set of default configuration files, with lots of information
557 551 on available options, use::
558 552
559 553 $ ipython profile create
560 554
561 .. seealso:
555 .. seealso::
562 556
563 557 :ref:`config_overview`, in particular :ref:`Profiles`.
564 558
565 559
566 Importing `.py` files
567 ----------------------
568
560 Importing ``.py`` files
561 -----------------------
569 562
570 563 ``.py`` files will be imported into the IPython Notebook as a notebook with
571 564 the same basename, but an ``.ipynb`` extension, located in the notebook
572 565 directory. The notebook created will have just one cell, which will contain
573 566 all the code in the ``.py`` file. You can later manually partition this into
574 567 individual cells using the ``Edit | Split Cell`` menu option, or the
575 568 :kbd:`Ctrl-m -` keyboard shortcut.
576 569
577 .. Alternatively, prior to importing the ``.py``, you can manually add ``# <
578 nbformat>2</nbformat>`` at the start of the file, and then add separators for
579 text and code cells, to get a cleaner import with the file already broken into
580 individual cells.
570 Note that ``.py`` scripts obtained from a notebook document using nbconvert_
571 maintain the structure of the notebook in comments. Reimporting such a
572 script back into the Notebook will preserve this structxure.
581 573
574
575 .. warning::
576
577 You can "roundtrip" a notebook to Python, by exporting the
578 notebook to a ``.py`` script, editing the script, and then importing it back
579 into the Notebook without loss of main content. However,
580 in general this is *not guaranteed* to work. First, there is extra metadata
581 saved in the notebook that may not be saved to the ``.py`` format. Second,
582 as the notebook format evolves in complexity, there will be attributes of
583 the notebook that will not survive a roundtrip through the Python form. You
584 should think of the Python format as a way to output a script version of a
585 notebook and the import capabilities as a way to load existing code to get
586 a notebook started. But the Python version is *not* an alternate notebook
587 format.
588
589 .. seealso::
590 :ref:`notebook_format`
@@ -1,182 +1,187 b''
1 1 .. _working_remotely.txt
2 2
3 3 Working remotely
4 4 ================
5 5
6 6
7 7 The IPython Notebook web app is based on a server-client structure.
8 This server uses a two-process kernel architecture based on ZeroMQ, as well as
9 Tornado for serving HTTP requests. Other clients may connect to the same
8 This server uses a two-process kernel architecture based on ZeroMQ_, as well
9 as Tornado_ for serving HTTP requests. Other clients may connect to the same
10 10 underlying IPython kernel; see below.
11 11
12 .. _ZeroMQ: http://zeromq.org
13
14 .. _Tornado: http://www.tornadoweb.org
15
16
12 17 .. _notebook_security:
13 18
14 19 Security
15 20 --------
16 21
17 22 You can protect your Notebook server with a simple single password by
18 23 setting the :attr:`NotebookApp.password` configurable. You can prepare a
19 24 hashed password using the function :func:`IPython.lib.security.passwd`:
20 25
21 26 .. sourcecode:: ipython
22 27
23 28 In [1]: from IPython.lib import passwd
24 29 In [2]: passwd()
25 30 Enter password:
26 31 Verify password:
27 32 Out[2]: 'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
28 33
29 34 .. note::
30 35
31 36 :func:`~IPython.lib.security.passwd` can also take the password as a string
32 37 argument. **Do not** pass it as an argument inside an IPython session, as it
33 38 will be saved in your input history.
34 39
35 40 You can then add this to your :file:`ipython_notebook_config.py`, e.g.::
36 41
37 42 # Password to use for web authentication
38 43 c = get_config()
39 44 c.NotebookApp.password =
40 45 u'sha1:67c9e60bb8b6:9ffede0825894254b2e042ea597d771089e11aed'
41 46
42 47 When using a password, it is a good idea to also use SSL, so that your
43 48 password is not sent unencrypted by your browser. You can start the notebook
44 49 to communicate via a secure protocol mode using a self-signed certificate with
45 50 the command::
46 51
47 52 $ ipython notebook --certfile=mycert.pem
48 53
49 54 .. note::
50 55
51 56 A self-signed certificate can be generated with ``openssl``. For example,
52 57 the following command will create a certificate valid for 365 days with
53 58 both the key and certificate data written to the same file::
54 59
55 60 $ openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.
56 61 pem -out mycert.pem
57 62
58 63 Your browser will warn you of a dangerous certificate because it is
59 64 self-signed. If you want to have a fully compliant certificate that will not
60 65 raise warnings, it is possible (but rather involved) to obtain one,
61 `as explained in detailed in this tutorial`__.
66 as explained in detail in `this tutorial`__.
62 67
63 68 .. __: http://arstechnica.com/security/news/2009/12/how-to-get-set-with-a-
64 69 secure-sertificate-for-free.ars
65 70
66 71 Keep in mind that when you enable SSL support, you will need to access the
67 72 notebook server over ``https://``, not over plain ``http://``. The startup
68 73 message from the server prints this, but it is easy to overlook and think the
69 74 server is for some reason non-responsive.
70 75
71 76
72 77 Connecting to an existing kernel
73 78 ---------------------------------
74 79
75 80 The notebook server always prints to the terminal the full details of
76 81 how to connect to each kernel, with messages such as the following::
77 82
78 83 [IPKernelApp] To connect another client to this kernel, use:
79 84 [IPKernelApp] --existing kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
80 85
81 86 This long string is the name of a JSON file that contains all the port and
82 87 validation information necessary to connect to the kernel. You can then, for
83 88 example, manually start a Qt console connected to the *same* kernel with::
84 89
85 90 $ ipython qtconsole --existing
86 91 kernel-3bb93edd-6b5a-455c-99c8-3b658f45dde5.json
87 92
88 93 If you have only a single kernel running, simply typing::
89 94
90 95 $ ipython qtconsole --existing
91 96
92 97 will automatically find it. (It will always find the most recently
93 98 started kernel if there is more than one.) You can also request this
94 99 connection data by typing ``%connect_info``; this will print the same
95 100 file information as well as the content of the JSON data structure it
96 101 contains.
97 102
98 103
99 104 Running a public notebook server
100 105 --------------------------------
101 106
102 107 If you want to access your notebook server remotely via a web browser,
103 108 you can do the following.
104 109
105 110 Start by creating a certificate file and a hashed password, as explained
106 111 above. Then create a custom profile for the notebook, with the following
107 112 command line, type::
108 113
109 114 $ ipython profile create nbserver
110 115
111 116 In the profile directory just created, edit the file
112 117 ``ipython_notebook_config.py``. By default, the file has all fields
113 118 commented; the minimum set you need to uncomment and edit is the following::
114 119
115 120 c = get_config()
116 121
117 122 # Kernel config
118 123 c.IPKernelApp.pylab = 'inline' # if you want plotting support always
119 124
120 125 # Notebook config
121 126 c.NotebookApp.certfile = u'/absolute/path/to/your/certificate/mycert.pem'
122 127 c.NotebookApp.ip = '*'
123 128 c.NotebookApp.open_browser = False
124 129 c.NotebookApp.password = u'sha1:bcd259ccf...[your hashed password here]'
125 130 # It is a good idea to put it on a known, fixed port
126 131 c.NotebookApp.port = 9999
127 132
128 133 You can then start the notebook and access it later by pointing your browser
129 134 to ``https://your.host.com:9999`` with ``ipython notebook
130 135 --profile=nbserver``.
131 136
132 137 Running with a different URL prefix
133 138 -----------------------------------
134 139
135 140 The notebook dashboard (the landing page with an overview
136 141 of the notebooks in your working directory) typically lives at the URL
137 142 ``http://localhost:8888/``. If you prefer that it lives, together with the
138 143 rest of the notebook, under a sub-directory,
139 144 e.g. ``http://localhost:8888/ipython/``, you can do so with
140 145 configuration options like the following (see above for instructions about
141 146 modifying ``ipython_notebook_config.py``)::
142 147
143 148 c.NotebookApp.base_project_url = '/ipython/'
144 149 c.NotebookApp.base_kernel_url = '/ipython/'
145 150 c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'}
146 151
147 152 Using a different notebook store
148 153 --------------------------------
149 154
150 155 By default, the Notebook app stores the notebook documents that it saves as
151 156 files in the working directory of the Notebook app, also known as the
152 157 ``notebook_dir``. This logic is implemented in the
153 158 :class:`FileNotebookManager` class. However, the server can be configured to
154 159 use a different notebook manager class, which can
155 160 store the notebooks in a different format.
156 161
157 162 Currently, we ship a :class:`AzureNotebookManager` class that stores notebooks
158 163 in Azure blob storage. This can be used by adding the following lines to your
159 164 ``ipython_notebook_config.py`` file::
160 165
161 166 c.NotebookApp.notebook_manager_class =
162 167 'IPython.html.services.notebooks.azurenbmanager.AzureNotebookManager'
163 168 c.AzureNotebookManager.account_name = u'paste_your_account_name_here'
164 169 c.AzureNotebookManager.account_key = u'paste_your_account_key_here'
165 170 c.AzureNotebookManager.container = u'notebooks'
166 171
167 172 In addition to providing your Azure Blob Storage account name and key, you
168 173 will have to provide a container name; you can use multiple containers to
169 174 organize your notebooks.
170 175
171 176
172 177 Known issues
173 178 ------------
174 179
175 180 When behind a proxy, especially if your system or browser is set to autodetect
176 181 the proxy, the Notebook app might fail to connect to the server's websockets,
177 182 and present you with a warning at startup. In this case, you need to configure
178 183 your system not to use the proxy for the server's address.
179 184
180 185 For example, in Firefox, go to the Preferences panel, Advanced section,
181 186 Network tab, click 'Settings...', and add the address of the notebook server
182 187 to the 'No proxy for' field.
General Comments 0
You need to be logged in to leave comments. Login now