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