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