Show More
@@ -0,0 +1,212 b'' | |||||
|
1 | .. _nbconvert: | |||
|
2 | ||||
|
3 | Converting notebooks to other formats | |||
|
4 | ===================================== | |||
|
5 | ||||
|
6 | Newly added in the 1.0 release of IPython is the ``nbconvert`` tool, which | |||
|
7 | allows you to convert an ``.ipynb`` notebook document file into various static | |||
|
8 | formats. | |||
|
9 | ||||
|
10 | Currently, ``nbconvert`` is provided as a command line tool, run as a script | |||
|
11 | using IPython. A direct export capability from within the | |||
|
12 | IPython Notebook web app is planned. | |||
|
13 | ||||
|
14 | The command-line syntax to run the ``nbconvert`` script is:: | |||
|
15 | ||||
|
16 | $ ipython nbconvert --to FORMAT notebook.ipynb | |||
|
17 | ||||
|
18 | This will convert the IPython document file ``notebook.ipynb`` into the output | |||
|
19 | format given by the ``FORMAT`` string. | |||
|
20 | ||||
|
21 | The default output format is html, for which the ``--to`` argument may be | |||
|
22 | omitted:: | |||
|
23 | ||||
|
24 | $ ipython nbconvert notebook.ipynb | |||
|
25 | ||||
|
26 | IPython provides a few templates for some output formats, and these can be | |||
|
27 | specified via an additional ``--template`` argument. | |||
|
28 | ||||
|
29 | The currently supported export formats are: | |||
|
30 | ||||
|
31 | * ``--to html`` | |||
|
32 | ||||
|
33 | - ``--template full`` (default) | |||
|
34 | ||||
|
35 | A full static HTML render of the notebook. | |||
|
36 | This looks very similar to the interactive view. | |||
|
37 | ||||
|
38 | - ``--template basic`` | |||
|
39 | ||||
|
40 | Simplified HTML, useful for embedding in webpages, blogs, etc. | |||
|
41 | This excludes HTML headers. | |||
|
42 | ||||
|
43 | * ``--to latex`` | |||
|
44 | ||||
|
45 | Latex export. This generates ``NOTEBOOK_NAME.tex`` file, | |||
|
46 | ready for export. You can automatically run latex on it to generate a PDF | |||
|
47 | by adding ``--post PDF``. | |||
|
48 | ||||
|
49 | - ``--template article`` (default) | |||
|
50 | ||||
|
51 | Latex article, derived from Sphinx's howto template. | |||
|
52 | ||||
|
53 | - ``--template book`` | |||
|
54 | ||||
|
55 | Latex book, derived from Sphinx's manual template. | |||
|
56 | ||||
|
57 | - ``--template basic`` | |||
|
58 | ||||
|
59 | Very basic latex output - mainly meant as a starting point for custom templates. | |||
|
60 | ||||
|
61 | * ``--to slides`` | |||
|
62 | ||||
|
63 | This generates a Reveal.js HTML slideshow. | |||
|
64 | It must be served by an HTTP server. The easiest way to get this is to add | |||
|
65 | ``--post serve`` on the command-line. | |||
|
66 | ||||
|
67 | * ``--to markdown`` | |||
|
68 | ||||
|
69 | Simple markdown output. Markdown cells are unaffected, | |||
|
70 | and code cells are placed in triple-backtick (``\`\`\```) blocks. | |||
|
71 | ||||
|
72 | * ``--to rst`` | |||
|
73 | ||||
|
74 | Basic reStructuredText output. Useful as a starting point for embedding notebooks | |||
|
75 | in Sphinx docs. | |||
|
76 | ||||
|
77 | * ``--to python`` | |||
|
78 | ||||
|
79 | Convert a notebook to an executable Python script. | |||
|
80 | This is the simplest way to get a Python script out of a notebook. | |||
|
81 | If there were any magics in the notebook, this may only be executable from | |||
|
82 | an IPython session. | |||
|
83 | ||||
|
84 | .. note:: | |||
|
85 | ||||
|
86 | nbconvert uses pandoc_ to convert between various markup languages, | |||
|
87 | so pandoc is a dependency of most nbconvert transforms, | |||
|
88 | excluding Markdown and Python. | |||
|
89 | ||||
|
90 | .. _pandoc: http://johnmacfarlane.net/pandoc/ | |||
|
91 | ||||
|
92 | The output file created by ``nbconvert`` will have the same base name as | |||
|
93 | the notebook and will be placed in the current working directory. Any | |||
|
94 | supporting files (graphics, etc) will be placed in a new directory with the | |||
|
95 | same base name as the notebook, suffixed with ``_files``:: | |||
|
96 | ||||
|
97 | $ ipython nbconvert notebook.ipynb | |||
|
98 | $ ls | |||
|
99 | notebook.ipynb notebook.html notebook_files/ | |||
|
100 | ||||
|
101 | For simple single-file output, such as html, markdown, etc., | |||
|
102 | the output may be sent to standard output with:: | |||
|
103 | ||||
|
104 | $ ipython nbconvert --to markdown notebook.ipynb --stdout | |||
|
105 | ||||
|
106 | Multiple notebooks can be specified from the command line:: | |||
|
107 | ||||
|
108 | $ ipython nbconvert notebook*.ipynb | |||
|
109 | $ ipython nbconvert notebook1.ipynb notebook2.ipynb | |||
|
110 | ||||
|
111 | or via a list in a configuration file, say ``mycfg.py``, containing the text:: | |||
|
112 | ||||
|
113 | c = get_config() | |||
|
114 | c.NbConvertApp.notebooks = ["notebook1.ipynb", "notebook2.ipynb"] | |||
|
115 | ||||
|
116 | and using the command:: | |||
|
117 | ||||
|
118 | $ ipython nbconvert --config mycfg.py | |||
|
119 | ||||
|
120 | ||||
|
121 | .. _notebook_format: | |||
|
122 | ||||
|
123 | Notebook JSON file format | |||
|
124 | ------------------------- | |||
|
125 | ||||
|
126 | Notebook documents are JSON files with an ``.ipynb`` extension, formatted | |||
|
127 | as legibly as possible with minimal extra indentation and cell content broken | |||
|
128 | across lines to make them reasonably friendly to use in version-control | |||
|
129 | workflows. You should be very careful if you ever manually edit this JSON | |||
|
130 | data, as it is extremely easy to corrupt its internal structure and make the | |||
|
131 | file impossible to load. In general, you should consider the notebook as a | |||
|
132 | file meant only to be edited by the IPython Notebook app itself, not for | |||
|
133 | hand-editing. | |||
|
134 | ||||
|
135 | .. note:: | |||
|
136 | ||||
|
137 | Binary data such as figures are also saved directly in the JSON file. | |||
|
138 | This provides convenient single-file portability, but means that the | |||
|
139 | files can be large; a ``diff`` of binary data is also not very | |||
|
140 | meaningful. Since the binary blobs are encoded in a single line, they | |||
|
141 | affect only one line of the ``diff`` output, but they are typically very | |||
|
142 | long lines. You can use the ``Cell | All Output | Clear`` menu option to | |||
|
143 | remove all output from a notebook prior to committing it to version | |||
|
144 | control, if this is a concern. | |||
|
145 | ||||
|
146 | The notebook server can also generate a pure Python version of your notebook, | |||
|
147 | using the ``File | Download as`` menu option. The resulting ``.py`` file will | |||
|
148 | contain all the code cells from your notebook verbatim, and all Markdown cells | |||
|
149 | prepended with a comment marker. The separation between code and Markdown | |||
|
150 | cells is indicated with special comments and there is a header indicating the | |||
|
151 | format version. All output is removed when exporting to Python. | |||
|
152 | ||||
|
153 | As an example, consider a simple notebook called ``simple.ipynb`` which | |||
|
154 | contains one Markdown cell, with the content ``The simplest notebook.``, one | |||
|
155 | code input cell with the content ``print "Hello, IPython!"``, and the | |||
|
156 | corresponding output. | |||
|
157 | ||||
|
158 | The contents of the notebook document ``simple.ipynb`` is the following JSON | |||
|
159 | container:: | |||
|
160 | ||||
|
161 | { | |||
|
162 | "metadata": { | |||
|
163 | "name": "simple" | |||
|
164 | }, | |||
|
165 | "nbformat": 3, | |||
|
166 | "nbformat_minor": 0, | |||
|
167 | "worksheets": [ | |||
|
168 | { | |||
|
169 | "cells": [ | |||
|
170 | { | |||
|
171 | "cell_type": "markdown", | |||
|
172 | "metadata": {}, | |||
|
173 | "source": "The simplest notebook." | |||
|
174 | }, | |||
|
175 | { | |||
|
176 | "cell_type": "code", | |||
|
177 | "collapsed": false, | |||
|
178 | "input": "print \"Hello, IPython\"", | |||
|
179 | "language": "python", | |||
|
180 | "metadata": {}, | |||
|
181 | "outputs": [ | |||
|
182 | { | |||
|
183 | "output_type": "stream", | |||
|
184 | "stream": "stdout", | |||
|
185 | "text": "Hello, IPython\n" | |||
|
186 | } | |||
|
187 | ], | |||
|
188 | "prompt_number": 1 | |||
|
189 | } | |||
|
190 | ], | |||
|
191 | "metadata": {} | |||
|
192 | } | |||
|
193 | ] | |||
|
194 | } | |||
|
195 | ||||
|
196 | ||||
|
197 | The corresponding Python script is:: | |||
|
198 | ||||
|
199 | # -*- coding: utf-8 -*- | |||
|
200 | # <nbformat>3.0</nbformat> | |||
|
201 | ||||
|
202 | # <markdowncell> | |||
|
203 | ||||
|
204 | # The simplest notebook. | |||
|
205 | ||||
|
206 | # <codecell> | |||
|
207 | ||||
|
208 | print "Hello, IPython" | |||
|
209 | ||||
|
210 | Note that indeed the output of the code cell, which is present in the JSON | |||
|
211 | container, has been removed in the ``.py`` script. | |||
|
212 |
@@ -11,7 +11,7 b' Using IPython for interactive work' | |||||
11 | shell |
|
11 | shell | |
12 | qtconsole |
|
12 | qtconsole | |
13 | notebook |
|
13 | notebook | |
14 | converting_notebooks |
|
14 | nbconvert | |
15 | working_remotely |
|
15 | working_remotely | |
16 |
|
16 | |||
17 |
|
17 |
@@ -477,26 +477,7 b' setup for IPython to work correctly hand in hand with ``matplotlib``; it does' | |||||
477 | *not*, however, actually execute any Python ``import`` commands, that is, no |
|
477 | *not*, however, actually execute any Python ``import`` commands, that is, no | |
478 | names are added to the namespace. |
|
478 | names are added to the namespace. | |
479 |
|
479 | |||
480 | For more agile *interactive* use of the notebook space, an alternative magic, |
|
480 | If the ``%matplotlib`` magic is called without an argument, the | |
481 | ``%pylab``, is provided. This does the same work as the ``%matplotlib`` magic, |
|
|||
482 | but *in addition* it automatically executes a standard sequence of ``import`` |
|
|||
483 | statements required to work with the ``%matplotlib`` library, importing the |
|
|||
484 | following names into the namespace: |
|
|||
485 |
|
||||
486 | ``numpy`` as ``np``; ``matplotlib.pyplot`` as ``plt``; |
|
|||
487 | ``matplotlib``, ``pylab`` and ``mlab`` from ``matplotlib``; and *all names* |
|
|||
488 | from within ``numpy`` and ``pylab``. |
|
|||
489 |
|
||||
490 | However, the use of ``%pylab`` is discouraged, since names coming from |
|
|||
491 | different packages may collide. In general, the use of ``from package import |
|
|||
492 | *`` is discouraged. A better option is then:: |
|
|||
493 |
|
||||
494 | %pylab --no-import-all |
|
|||
495 |
|
||||
496 | which imports the names listed above, but does *not* perform this |
|
|||
497 | ``import *`` imports. |
|
|||
498 |
|
||||
499 | If the ``%matplotlib`` or ``%pylab` magics are called without an argument, the |
|
|||
500 | output of a plotting command is displayed using the default ``matplotlib`` |
|
481 | output of a plotting command is displayed using the default ``matplotlib`` | |
501 | backend in a separate window. Alternatively, the backend can be explicitly |
|
482 | backend in a separate window. Alternatively, the backend can be explicitly | |
502 | requested using, for example:: |
|
483 | requested using, for example:: | |
@@ -504,7 +485,7 b' requested using, for example::' | |||||
504 | %matplotlib gtk |
|
485 | %matplotlib gtk | |
505 |
|
486 | |||
506 | A particularly interesting backend is the ``inline`` backend. |
|
487 | A particularly interesting backend is the ``inline`` backend. | |
507 |
This is applicable only for the IPython Notebook and the IPython Qt |
|
488 | This is applicable only for the IPython Notebook and the IPython QtConsole. | |
508 | It can be invoked as follows:: |
|
489 | It can be invoked as follows:: | |
509 |
|
490 | |||
510 | %matplotlib inline |
|
491 | %matplotlib inline |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now