##// END OF EJS Templates
Merge pull request #3863 from damianavila/speaker_notes...
Matthias Bussonnier -
r12197:4565b731 merge
parent child Browse files
Show More
@@ -1,320 +1,322 b''
1 1 #!/usr/bin/env python
2 2 """NBConvert is a utility for conversion of .ipynb files.
3 3
4 4 Command-line interface for the NbConvert conversion utility.
5 5 """
6 6 #-----------------------------------------------------------------------------
7 7 #Copyright (c) 2013, the IPython Development Team.
8 8 #
9 9 #Distributed under the terms of the Modified BSD License.
10 10 #
11 11 #The full license is in the file COPYING.txt, distributed with this software.
12 12 #-----------------------------------------------------------------------------
13 13
14 14 #-----------------------------------------------------------------------------
15 15 #Imports
16 16 #-----------------------------------------------------------------------------
17 17
18 18 # Stdlib imports
19 19 from __future__ import print_function
20 20
21 21 import logging
22 22 import sys
23 23 import os
24 24 import glob
25 25
26 26 # From IPython
27 27 from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags
28 28 from IPython.config import catch_config_error, Configurable
29 29 from IPython.utils.traitlets import (
30 30 Unicode, List, Instance, DottedObjectName, Type, CaselessStrEnum,
31 31 )
32 32 from IPython.utils.importstring import import_item
33 33 from IPython.utils.text import dedent
34 34
35 35 from .exporters.export import get_export_names, exporter_map
36 36 from IPython.nbconvert import exporters, transformers, writers, post_processors
37 37 from .utils.base import NbConvertBase
38 38 from .utils.exceptions import ConversionException
39 39
40 40 #-----------------------------------------------------------------------------
41 41 #Classes and functions
42 42 #-----------------------------------------------------------------------------
43 43
44 44 class DottedOrNone(DottedObjectName):
45 45 """
46 46 A string holding a valid dotted object name in Python, such as A.b3._c
47 47 Also allows for None type."""
48 48
49 49 default_value = u''
50 50
51 51 def validate(self, obj, value):
52 52 if value is not None and len(value) > 0:
53 53 return super(DottedOrNone, self).validate(obj, value)
54 54 else:
55 55 return value
56 56
57 57 nbconvert_aliases = {}
58 58 nbconvert_aliases.update(base_aliases)
59 59 nbconvert_aliases.update({
60 60 'to' : 'NbConvertApp.export_format',
61 61 'template' : 'Exporter.template_file',
62 62 'writer' : 'NbConvertApp.writer_class',
63 63 'post': 'NbConvertApp.post_processor_class',
64 'output': 'NbConvertApp.output_base'
64 'output': 'NbConvertApp.output_base',
65 'offline-slides': 'RevealHelpTransformer.url_prefix',
66 'slide-notes': 'RevealHelpTransformer.speaker_notes'
65 67 })
66 68
67 69 nbconvert_flags = {}
68 70 nbconvert_flags.update(base_flags)
69 71 nbconvert_flags.update({
70 72 'stdout' : (
71 73 {'NbConvertApp' : {'writer_class' : "StdoutWriter"}},
72 74 "Write notebook output to stdout instead of files."
73 75 )
74 76 })
75 77
76 78
77 79 class NbConvertApp(BaseIPythonApplication):
78 80 """Application used to convert to and from notebook file type (*.ipynb)"""
79 81
80 82 name = 'ipython-nbconvert'
81 83 aliases = nbconvert_aliases
82 84 flags = nbconvert_flags
83 85
84 86 def _log_level_default(self):
85 87 return logging.INFO
86 88
87 89 def _classes_default(self):
88 90 classes = [NbConvertBase]
89 91 for pkg in (exporters, transformers, writers):
90 92 for name in dir(pkg):
91 93 cls = getattr(pkg, name)
92 94 if isinstance(cls, type) and issubclass(cls, Configurable):
93 95 classes.append(cls)
94 96 return classes
95 97
96 98 description = Unicode(
97 99 u"""This application is used to convert notebook files (*.ipynb)
98 100 to various other formats.
99 101
100 102 WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.""")
101 103
102 104 output_base = Unicode('', config=True, help='''overwrite base name use for output files.
103 105 can only be use when converting one notebook at a time.
104 106 ''')
105 107
106 108 examples = Unicode(u"""
107 109 The simplest way to use nbconvert is
108 110
109 111 > ipython nbconvert mynotebook.ipynb
110 112
111 113 which will convert mynotebook.ipynb to the default format (probably HTML).
112 114
113 115 You can specify the export format with `--to`.
114 116 Options include {0}
115 117
116 118 > ipython nbconvert --to latex mynotebook.ipnynb
117 119
118 120 Both HTML and LaTeX support multiple output templates. LaTeX includes
119 121 'basic', 'book', and 'article'. HTML includes 'basic' and 'full'. You
120 122 can specify the flavor of the format used.
121 123
122 124 > ipython nbconvert --to html --template basic mynotebook.ipynb
123 125
124 126 You can also pipe the output to stdout, rather than a file
125 127
126 128 > ipython nbconvert mynotebook.ipynb --stdout
127 129
128 130 A post-processor can be used to compile a PDF
129 131
130 132 > ipython nbconvert mynotebook.ipynb --to latex --post PDF
131 133
132 134 You can get (and serve) a Reveal.js-powered slideshow
133 135
134 136 > ipython nbconvert myslides.ipynb --to slides --post serve
135 137
136 138 Multiple notebooks can be given at the command line in a couple of
137 139 different ways:
138 140
139 141 > ipython nbconvert notebook*.ipynb
140 142 > ipython nbconvert notebook1.ipynb notebook2.ipynb
141 143
142 144 or you can specify the notebooks list in a config file, containing::
143 145
144 146 c.NbConvertApp.notebooks = ["my_notebook.ipynb"]
145 147
146 148 > ipython nbconvert --config mycfg.py
147 149 """.format(get_export_names()))
148 150
149 151 # Writer specific variables
150 152 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
151 153 help="""Instance of the writer class used to write the
152 154 results of the conversion.""")
153 155 writer_class = DottedObjectName('FilesWriter', config=True,
154 156 help="""Writer class used to write the
155 157 results of the conversion""")
156 158 writer_aliases = {'fileswriter': 'IPython.nbconvert.writers.files.FilesWriter',
157 159 'debugwriter': 'IPython.nbconvert.writers.debug.DebugWriter',
158 160 'stdoutwriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'}
159 161 writer_factory = Type()
160 162
161 163 def _writer_class_changed(self, name, old, new):
162 164 if new.lower() in self.writer_aliases:
163 165 new = self.writer_aliases[new.lower()]
164 166 self.writer_factory = import_item(new)
165 167
166 168 # Post-processor specific variables
167 169 post_processor = Instance('IPython.nbconvert.post_processors.base.PostProcessorBase',
168 170 help="""Instance of the PostProcessor class used to write the
169 171 results of the conversion.""")
170 172
171 173 post_processor_class = DottedOrNone(config=True,
172 174 help="""PostProcessor class used to write the
173 175 results of the conversion""")
174 176 post_processor_aliases = {'pdf': 'IPython.nbconvert.post_processors.pdf.PDFPostProcessor',
175 177 'serve': 'IPython.nbconvert.post_processors.serve.ServePostProcessor'}
176 178 post_processor_factory = Type()
177 179
178 180 def _post_processor_class_changed(self, name, old, new):
179 181 if new.lower() in self.post_processor_aliases:
180 182 new = self.post_processor_aliases[new.lower()]
181 183 if new:
182 184 self.post_processor_factory = import_item(new)
183 185
184 186
185 187 # Other configurable variables
186 188 export_format = CaselessStrEnum(get_export_names(),
187 189 default_value="html",
188 190 config=True,
189 191 help="""The export format to be used."""
190 192 )
191 193
192 194 notebooks = List([], config=True, help="""List of notebooks to convert.
193 195 Wildcards are supported.
194 196 Filenames passed positionally will be added to the list.
195 197 """)
196 198
197 199 @catch_config_error
198 200 def initialize(self, argv=None):
199 201 super(NbConvertApp, self).initialize(argv)
200 202 self.init_syspath()
201 203 self.init_notebooks()
202 204 self.init_writer()
203 205 self.init_post_processor()
204 206
205 207
206 208
207 209 def init_syspath(self):
208 210 """
209 211 Add the cwd to the sys.path ($PYTHONPATH)
210 212 """
211 213 sys.path.insert(0, os.getcwd())
212 214
213 215
214 216 def init_notebooks(self):
215 217 """Construct the list of notebooks.
216 218 If notebooks are passed on the command-line,
217 219 they override notebooks specified in config files.
218 220 Glob each notebook to replace notebook patterns with filenames.
219 221 """
220 222
221 223 # Specifying notebooks on the command-line overrides (rather than adds)
222 224 # the notebook list
223 225 if self.extra_args:
224 226 patterns = self.extra_args
225 227 else:
226 228 patterns = self.notebooks
227 229
228 230 # Use glob to replace all the notebook patterns with filenames.
229 231 filenames = []
230 232 for pattern in patterns:
231 233
232 234 # Use glob to find matching filenames. Allow the user to convert
233 235 # notebooks without having to type the extension.
234 236 globbed_files = glob.glob(pattern)
235 237 globbed_files.extend(glob.glob(pattern + '.ipynb'))
236 238 if not globbed_files:
237 239 self.log.warn("pattern %r matched no files", pattern)
238 240
239 241 for filename in globbed_files:
240 242 if not filename in filenames:
241 243 filenames.append(filename)
242 244 self.notebooks = filenames
243 245
244 246 def init_writer(self):
245 247 """
246 248 Initialize the writer (which is stateless)
247 249 """
248 250 self._writer_class_changed(None, self.writer_class, self.writer_class)
249 251 self.writer = self.writer_factory(parent=self)
250 252
251 253 def init_post_processor(self):
252 254 """
253 255 Initialize the post_processor (which is stateless)
254 256 """
255 257 self._post_processor_class_changed(None, self.post_processor_class,
256 258 self.post_processor_class)
257 259 if self.post_processor_factory:
258 260 self.post_processor = self.post_processor_factory(parent=self)
259 261
260 262 def start(self):
261 263 """
262 264 Ran after initialization completed
263 265 """
264 266 super(NbConvertApp, self).start()
265 267 self.convert_notebooks()
266 268
267 269 def convert_notebooks(self):
268 270 """
269 271 Convert the notebooks in the self.notebook traitlet
270 272 """
271 273 # Export each notebook
272 274 conversion_success = 0
273 275
274 276 if self.output_base != '' and len(self.notebooks) > 1:
275 277 self.log.error(
276 278 """UsageError: --output flag or `NbConvertApp.output_base` config option
277 279 cannot be used when converting multiple notebooks.
278 280 """)
279 281 self.exit(1)
280 282
281 283 exporter = exporter_map[self.export_format](config=self.config)
282 284
283 285 for notebook_filename in self.notebooks:
284 286 self.log.info("Converting notebook %s to %s", notebook_filename, self.export_format)
285 287
286 288 # Get a unique key for the notebook and set it in the resources object.
287 289 basename = os.path.basename(notebook_filename)
288 290 notebook_name = basename[:basename.rfind('.')]
289 291 if self.output_base:
290 292 notebook_name = self.output_base
291 293 resources = {}
292 294 resources['unique_key'] = notebook_name
293 295 resources['output_files_dir'] = '%s_files' % notebook_name
294 296 self.log.info("Support files will be in %s", os.path.join(resources['output_files_dir'], ''))
295 297
296 298 # Try to export
297 299 try:
298 300 output, resources = exporter.from_filename(notebook_filename, resources=resources)
299 301 except ConversionException as e:
300 302 self.log.error("Error while converting '%s'", notebook_filename,
301 303 exc_info=True)
302 304 self.exit(1)
303 305 else:
304 306 write_resultes = self.writer.write(output, resources, notebook_name=notebook_name)
305 307
306 308 #Post-process if post processor has been defined.
307 309 if hasattr(self, 'post_processor') and self.post_processor:
308 310 self.post_processor(write_resultes)
309 311 conversion_success += 1
310 312
311 313 # If nothing was converted successfully, help the user.
312 314 if conversion_success == 0:
313 315 self.print_help()
314 316 sys.exit(-1)
315 317
316 318 #-----------------------------------------------------------------------------
317 319 # Main entry point
318 320 #-----------------------------------------------------------------------------
319 321
320 322 launch_new_instance = NbConvertApp.launch_instance
@@ -1,185 +1,185 b''
1 1 {%- extends 'reveal_internals/slides.tpl' -%}
2 2
3 3
4 4 {% block header %}
5 5 <!DOCTYPE html>
6 6 <html>
7 7 <head>
8 8
9 9 <meta charset="utf-8" />
10 10 <meta http-equiv="X-UA-Compatible" content="chrome=1">
11 11
12 12 <meta name="apple-mobile-web-app-capable" content="yes" />
13 13 <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
14 14
15 15 <!-- General and theme style sheets -->
16 16 <link rel="stylesheet" href="{{resources.reveal.url_prefix}}/css/reveal.css">
17 17 <link rel="stylesheet" href="{{resources.reveal.url_prefix}}/css/theme/simple.css" id="theme">
18 18
19 19 <!-- For syntax highlighting -->
20 20 <link rel="stylesheet" href="{{resources.reveal.url_prefix}}/lib/css/zenburn.css">
21 21
22 22 <!-- If the query includes 'print-pdf', use the PDF print sheet -->
23 23 <script>
24 24 document.write( '<link rel="stylesheet" href="{{resources.reveal.url_prefix}}/css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' );
25 25 </script>
26 26
27 27 <!--[if lt IE 9]>
28 28 <script src="{{resources.reveal.url_prefix}}/lib/js/html5shiv.js"></script>
29 29 <![endif]-->
30 30
31 31 {% for css in resources.inlining.css -%}
32 32 <style type="text/css">
33 33 {{ css }}
34 34 </style>
35 35 {% endfor %}
36 36
37 37 <style type="text/css">
38 38 /* Overrides of notebook CSS for static HTML export */
39 39 .reveal {
40 40 font-size: 20px;
41 41 overflow-y: auto;
42 42 overflow-x: hidden;
43 43 }
44 44 .reveal pre {
45 45 width: 95%;
46 46 padding: 0.4em;
47 47 margin: 0px;
48 48 font-family: monospace, sans-serif;
49 49 font-size: 80%;
50 50 box-shadow: 0px 0px 0px rgba(0, 0, 0, 0);
51 51 }
52 52 .reveal section img {
53 53 border: 0px solid black;
54 54 box-shadow: 0 0 10px rgba(0, 0, 0, 0);
55 55 }
56 56 .reveal .slides {
57 57 text-align: left;
58 58 }
59 59 .reveal.fade {
60 60 opacity: 1;
61 61 }
62 62 div.input_area {
63 63 padding: 0.06em;
64 64 }
65 65 div.code_cell {
66 66 background-color: transparent;
67 67 }
68 68 div.prompt {
69 69 width: 11ex;
70 70 padding: 0.4em;
71 71 margin: 0px;
72 72 font-family: monospace, sans-serif;
73 73 font-size: 80%;
74 74 text-align: right;
75 75 }
76 76 div.output_area pre {
77 77 font-family: monospace, sans-serif;
78 78 font-size: 80%;
79 79 }
80 80 div.output_prompt {
81 81 /* 5px right shift to account for margin in parent container */
82 82 margin: 5px 5px 0 0;
83 83 }
84 84 .rendered_html p {
85 85 text-align: inherit;
86 86 }
87 87 </style>
88 88
89 89 <!-- Custom stylesheet, it must be in the same directory as the html file -->
90 90 <link rel="stylesheet" href="custom.css">
91 91
92 92 </head>
93 93 {% endblock header%}
94 94
95 95
96 96 {% block body %}
97 97 <body>
98 98 <div class="reveal">
99 99 <div class="slides">
100 100 {{ super() }}
101 101 </div>
102 102 </div>
103 103
104 104 <!--
105 105 Uncomment the following block and the addthis_widget.js (see below inside dependencies)
106 106 to get enable social buttons.
107 107 -->
108 108
109 109 <!--
110 110 <div class="addthis_toolbox addthis_floating_style addthis_32x32_style" style="left:20px;top:20px;">
111 111 <a class="addthis_button_twitter"></a>
112 112 <a class="addthis_button_google_plusone_share"></a>
113 113 <a class="addthis_button_linkedin"></a>
114 114 <a class="addthis_button_facebook"></a>
115 115 <a class="addthis_button_more"></a>
116 116 </div>
117 117 -->
118 118
119 119 <script src="{{resources.reveal.url_prefix}}/lib/js/head.min.js"></script>
120 120
121 121 <script src="{{resources.reveal.url_prefix}}/js/reveal.js"></script>
122 122
123 123 <script>
124 124
125 125 // Full list of configuration options available here: https://github.com/hakimel/reveal.js#configuration
126 126 Reveal.initialize({
127 127 controls: true,
128 128 progress: true,
129 129 history: true,
130 130
131 131 theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
132 132 transition: Reveal.getQueryHash().transition || 'linear', // default/cube/page/concave/zoom/linear/none
133 133
134 134 // Optional libraries used to extend on reveal.js
135 135 dependencies: [
136 136 { src: "{{resources.reveal.url_prefix}}/lib/js/classList.js", condition: function() { return !document.body.classList; } },
137 137 { src: "{{resources.reveal.url_prefix}}/plugin/highlight/highlight.js", async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
138 { src: "{{resources.reveal.url_prefix}}/plugin/notes/notes.js", async: true, condition: function() { return !!document.body.classList; } }
138 { src: "{{resources.reveal.notes_prefix}}/plugin/notes/notes.js", async: true, condition: function() { return !!document.body.classList; } }
139 139 // { src: 'http://s7.addthis.com/js/300/addthis_widget.js', async: true},
140 140 ]
141 141 });
142 142 </script>
143 143
144 144 <!-- MathJax configuration -->
145 145 <script type="text/x-mathjax-config">
146 146 MathJax.Hub.Config({
147 147 tex2jax: {
148 148 inlineMath: [ ['$','$'], ["\\(","\\)"] ],
149 149 displayMath: [ ['$$','$$'], ["\\[","\\]"] ]
150 150 },
151 151 displayAlign: 'left', // Change this to 'center' to center equations.
152 152 "HTML-CSS": {
153 153 styles: {'.MathJax_Display': {"margin": 0}}
154 154 }
155 155 });
156 156 </script>
157 157 <!-- End of mathjax configuration -->
158 158
159 159 <script>
160 160 // We wait for the onload function to load MathJax after the page is completely loaded.
161 161 // MathJax is loaded 1 unit of time after the page is ready.
162 162 // This hack prevent problems when you load multiple js files (i.e. social button from addthis).
163 163 //
164 164 window.onload = function () {
165 165 setTimeout(function () {
166 166 var script = document.createElement("script");
167 167 script.type = "text/javascript";
168 168 script.src = "https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS_HTML";
169 169 document.getElementsByTagName("head")[0].appendChild(script);
170 170 },1)
171 171 }
172 172 </script>
173 173
174 174 <script>
175 175 Reveal.addEventListener( 'slidechanged', function( event ) {
176 176 MathJax.Hub.Rerender(event.currentSlide);
177 177 });
178 178 </script>
179 179
180 180 </body>
181 181 {% endblock body %}
182 182
183 183 {% block footer %}
184 184 </html>
185 185 {% endblock footer %} No newline at end of file
@@ -1,61 +1,94 b''
1 1 """Module that pre-processes the notebook for export via Reveal.
2 2 """
3 3 #-----------------------------------------------------------------------------
4 4 # Copyright (c) 2013, the IPython Development Team.
5 5 #
6 6 # Distributed under the terms of the Modified BSD License.
7 7 #
8 8 # The full license is in the file COPYING.txt, distributed with this software.
9 9 #-----------------------------------------------------------------------------
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Imports
13 13 #-----------------------------------------------------------------------------
14 14
15 import os
16 import urllib2
17
15 18 from .base import Transformer
16 from IPython.utils.traitlets import Unicode
19 from IPython.utils.traitlets import Unicode, Bool
17 20
18 21 #-----------------------------------------------------------------------------
19 22 # Classes and functions
20 23 #-----------------------------------------------------------------------------
21 24
22 25 class RevealHelpTransformer(Transformer):
23 26
24 27 url_prefix = Unicode('//cdn.jsdelivr.net/reveal.js/2.4.0',
25 28 config=True,
26 29 help="""If you want to use a local reveal.js library,
27 30 use 'url_prefix':'reveal.js' in your config object.""")
28 31
32 speaker_notes = Bool(False,
33 config=True,
34 help="""If you want to use the speaker notes
35 set this to True.""")
36
29 37 def call(self, nb, resources):
30 38 """
31 39 Called once to 'transform' contents of the notebook.
32 40
33 41 Parameters
34 42 ----------
35 43 nb : NotebookNode
36 44 Notebook being converted
37 45 resources : dictionary
38 46 Additional resources used in the conversion process. Allows
39 47 transformers to pass variables into the Jinja engine.
40 48 """
41 49
42 50 for worksheet in nb.worksheets :
43 51 for index, cell in enumerate(worksheet.cells):
44 52
45 53 #Make sure the cell has slideshow metadata.
46 54 cell.metadata.align_type = cell.get('metadata', {}).get('slideshow', {}).get('align_type', 'Left')
47 55 cell.metadata.slide_type = cell.get('metadata', {}).get('slideshow', {}).get('slide_type', '-')
48 56
49 57 #Get the slide type. If type is start of subslide or slide,
50 58 #end the last subslide/slide.
51 59 if cell.metadata.slide_type in ['slide']:
52 60 worksheet.cells[index - 1].metadata.slide_helper = 'slide_end'
53 61 if cell.metadata.slide_type in ['subslide']:
54 62 worksheet.cells[index - 1].metadata.slide_helper = 'subslide_end'
55 63
56 64
57 65 if not isinstance(resources['reveal'], dict):
58 66 resources['reveal'] = {}
59 67 resources['reveal']['url_prefix'] = self.url_prefix
68 resources['reveal']['notes_prefix'] = self.url_prefix
69
70 cdn = 'http://cdn.jsdelivr.net/reveal.js/2.4.0'
71 local = 'local'
72 html_path = 'plugin/notes/notes.html'
73 js_path = 'plugin/notes/notes.js'
74
75 html_infile = os.path.join(cdn, html_path)
76 js_infile = os.path.join(cdn, js_path)
77 html_outfile = os.path.join(local, html_path)
78 js_outfile = os.path.join(local, js_path)
79
80 if self.speaker_notes:
81 if 'outputs' not in resources:
82 resources['outputs'] = {}
83 resources['outputs'][html_outfile] = self.notes_helper(html_infile)
84 resources['outputs'][js_outfile] = self.notes_helper(js_infile)
85 resources['reveal']['notes_prefix'] = local
60 86
61 87 return nb, resources
88
89 def notes_helper(self, infile):
90 """Helper function to get the content from an url."""
91
92 content = urllib2.urlopen(infile).read()
93
94 return content
@@ -1,212 +1,217 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 If you want to use the speaker notes plugin, just add
67 ``--slide-notes=True`` on the command-line.
68 For low connectivity environments, you can use a local copy of the reveal.js library,
69 just add ``--offline-slides=reveal.js`` on the command-line, and do not forget to move
70 your downloaded ``reveal.js`` library to the same folder where your slides are located.
66 71
67 72 * ``--to markdown``
68 73
69 74 Simple markdown output. Markdown cells are unaffected,
70 75 and code cells are placed in triple-backtick (```````) blocks.
71 76
72 77 * ``--to rst``
73 78
74 79 Basic reStructuredText output. Useful as a starting point for embedding notebooks
75 80 in Sphinx docs.
76 81
77 82 * ``--to python``
78 83
79 84 Convert a notebook to an executable Python script.
80 85 This is the simplest way to get a Python script out of a notebook.
81 86 If there were any magics in the notebook, this may only be executable from
82 87 an IPython session.
83 88
84 89 .. note::
85 90
86 91 nbconvert uses pandoc_ to convert between various markup languages,
87 92 so pandoc is a dependency of most nbconvert transforms,
88 93 excluding Markdown and Python.
89 94
90 95 .. _pandoc: http://johnmacfarlane.net/pandoc/
91 96
92 97 The output file created by ``nbconvert`` will have the same base name as
93 98 the notebook and will be placed in the current working directory. Any
94 99 supporting files (graphics, etc) will be placed in a new directory with the
95 100 same base name as the notebook, suffixed with ``_files``::
96 101
97 102 $ ipython nbconvert notebook.ipynb
98 103 $ ls
99 104 notebook.ipynb notebook.html notebook_files/
100 105
101 106 For simple single-file output, such as html, markdown, etc.,
102 107 the output may be sent to standard output with::
103 108
104 109 $ ipython nbconvert --to markdown notebook.ipynb --stdout
105 110
106 111 Multiple notebooks can be specified from the command line::
107 112
108 113 $ ipython nbconvert notebook*.ipynb
109 114 $ ipython nbconvert notebook1.ipynb notebook2.ipynb
110 115
111 116 or via a list in a configuration file, say ``mycfg.py``, containing the text::
112 117
113 118 c = get_config()
114 119 c.NbConvertApp.notebooks = ["notebook1.ipynb", "notebook2.ipynb"]
115 120
116 121 and using the command::
117 122
118 123 $ ipython nbconvert --config mycfg.py
119 124
120 125
121 126 .. _notebook_format:
122 127
123 128 Notebook JSON file format
124 129 -------------------------
125 130
126 131 Notebook documents are JSON files with an ``.ipynb`` extension, formatted
127 132 as legibly as possible with minimal extra indentation and cell content broken
128 133 across lines to make them reasonably friendly to use in version-control
129 134 workflows. You should be very careful if you ever manually edit this JSON
130 135 data, as it is extremely easy to corrupt its internal structure and make the
131 136 file impossible to load. In general, you should consider the notebook as a
132 137 file meant only to be edited by the IPython Notebook app itself, not for
133 138 hand-editing.
134 139
135 140 .. note::
136 141
137 142 Binary data such as figures are also saved directly in the JSON file.
138 143 This provides convenient single-file portability, but means that the
139 144 files can be large; a ``diff`` of binary data is also not very
140 145 meaningful. Since the binary blobs are encoded in a single line, they
141 146 affect only one line of the ``diff`` output, but they are typically very
142 147 long lines. You can use the ``Cell | All Output | Clear`` menu option to
143 148 remove all output from a notebook prior to committing it to version
144 149 control, if this is a concern.
145 150
146 151 The notebook server can also generate a pure Python version of your notebook,
147 152 using the ``File | Download as`` menu option. The resulting ``.py`` file will
148 153 contain all the code cells from your notebook verbatim, and all Markdown cells
149 154 prepended with a comment marker. The separation between code and Markdown
150 155 cells is indicated with special comments and there is a header indicating the
151 156 format version. All output is removed when exporting to Python.
152 157
153 158 As an example, consider a simple notebook called ``simple.ipynb`` which
154 159 contains one Markdown cell, with the content ``The simplest notebook.``, one
155 160 code input cell with the content ``print "Hello, IPython!"``, and the
156 161 corresponding output.
157 162
158 163 The contents of the notebook document ``simple.ipynb`` is the following JSON
159 164 container::
160 165
161 166 {
162 167 "metadata": {
163 168 "name": "simple"
164 169 },
165 170 "nbformat": 3,
166 171 "nbformat_minor": 0,
167 172 "worksheets": [
168 173 {
169 174 "cells": [
170 175 {
171 176 "cell_type": "markdown",
172 177 "metadata": {},
173 178 "source": "The simplest notebook."
174 179 },
175 180 {
176 181 "cell_type": "code",
177 182 "collapsed": false,
178 183 "input": "print \"Hello, IPython\"",
179 184 "language": "python",
180 185 "metadata": {},
181 186 "outputs": [
182 187 {
183 188 "output_type": "stream",
184 189 "stream": "stdout",
185 190 "text": "Hello, IPython\n"
186 191 }
187 192 ],
188 193 "prompt_number": 1
189 194 }
190 195 ],
191 196 "metadata": {}
192 197 }
193 198 ]
194 199 }
195 200
196 201
197 202 The corresponding Python script is::
198 203
199 204 # -*- coding: utf-8 -*-
200 205 # <nbformat>3.0</nbformat>
201 206
202 207 # <markdowncell>
203 208
204 209 # The simplest notebook.
205 210
206 211 # <codecell>
207 212
208 213 print "Hello, IPython"
209 214
210 215 Note that indeed the output of the code cell, which is present in the JSON
211 216 container, has been removed in the ``.py`` script.
212 217
General Comments 0
You need to be logged in to leave comments. Login now