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