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