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