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