##// END OF EJS Templates
clarify `reveal-prefix` alias name
MinRK -
Show More
@@ -1,324 +1,323 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.core.profiledir import ProfileDir
29 29 from IPython.config import catch_config_error, Configurable
30 30 from IPython.utils.traitlets import (
31 31 Unicode, List, Instance, DottedObjectName, Type, CaselessStrEnum,
32 32 )
33 33 from IPython.utils.importstring import import_item
34 34 from IPython.utils.text import dedent
35 35
36 36 from .exporters.export import get_export_names, exporter_map
37 37 from IPython.nbconvert import exporters, preprocessors, writers, postprocessors
38 38 from .utils.base import NbConvertBase
39 39 from .utils.exceptions import ConversionException
40 40
41 41 #-----------------------------------------------------------------------------
42 42 #Classes and functions
43 43 #-----------------------------------------------------------------------------
44 44
45 45 class DottedOrNone(DottedObjectName):
46 46 """
47 47 A string holding a valid dotted object name in Python, such as A.b3._c
48 48 Also allows for None type."""
49 49
50 50 default_value = u''
51 51
52 52 def validate(self, obj, value):
53 53 if value is not None and len(value) > 0:
54 54 return super(DottedOrNone, self).validate(obj, value)
55 55 else:
56 56 return value
57 57
58 58 nbconvert_aliases = {}
59 59 nbconvert_aliases.update(base_aliases)
60 60 nbconvert_aliases.update({
61 61 'to' : 'NbConvertApp.export_format',
62 62 'template' : 'Exporter.template_file',
63 63 'writer' : 'NbConvertApp.writer_class',
64 64 'post': 'NbConvertApp.postprocessor_class',
65 65 'output': 'NbConvertApp.output_base',
66 'offline-slides': 'RevealHelpPreprocessor.url_prefix',
67 'slide-notes': 'RevealHelpPreprocessor.speaker_notes'
66 'reveal-prefix': 'RevealHelpPreprocessor.url_prefix',
68 67 })
69 68
70 69 nbconvert_flags = {}
71 70 nbconvert_flags.update(base_flags)
72 71 nbconvert_flags.update({
73 72 'stdout' : (
74 73 {'NbConvertApp' : {'writer_class' : "StdoutWriter"}},
75 74 "Write notebook output to stdout instead of files."
76 75 )
77 76 })
78 77
79 78
80 79 class NbConvertApp(BaseIPythonApplication):
81 80 """Application used to convert to and from notebook file type (*.ipynb)"""
82 81
83 82 name = 'ipython-nbconvert'
84 83 aliases = nbconvert_aliases
85 84 flags = nbconvert_flags
86 85
87 86 def _log_level_default(self):
88 87 return logging.INFO
89 88
90 89 def _classes_default(self):
91 90 classes = [NbConvertBase, ProfileDir]
92 91 for pkg in (exporters, preprocessors, writers):
93 92 for name in dir(pkg):
94 93 cls = getattr(pkg, name)
95 94 if isinstance(cls, type) and issubclass(cls, Configurable):
96 95 classes.append(cls)
97 96
98 97 return classes
99 98
100 99 description = Unicode(
101 100 u"""This application is used to convert notebook files (*.ipynb)
102 101 to various other formats.
103 102
104 103 WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.""")
105 104
106 105 output_base = Unicode('', config=True, help='''overwrite base name use for output files.
107 106 can only be use when converting one notebook at a time.
108 107 ''')
109 108
110 109 examples = Unicode(u"""
111 110 The simplest way to use nbconvert is
112 111
113 112 > ipython nbconvert mynotebook.ipynb
114 113
115 114 which will convert mynotebook.ipynb to the default format (probably HTML).
116 115
117 116 You can specify the export format with `--to`.
118 117 Options include {0}
119 118
120 119 > ipython nbconvert --to latex mynotebook.ipnynb
121 120
122 121 Both HTML and LaTeX support multiple output templates. LaTeX includes
123 122 'basic', 'book', and 'article'. HTML includes 'basic' and 'full'. You
124 123 can specify the flavor of the format used.
125 124
126 125 > ipython nbconvert --to html --template basic mynotebook.ipynb
127 126
128 127 You can also pipe the output to stdout, rather than a file
129 128
130 129 > ipython nbconvert mynotebook.ipynb --stdout
131 130
132 131 A post-processor can be used to compile a PDF
133 132
134 133 > ipython nbconvert mynotebook.ipynb --to latex --post PDF
135 134
136 135 You can get (and serve) a Reveal.js-powered slideshow
137 136
138 137 > ipython nbconvert myslides.ipynb --to slides --post serve
139 138
140 139 Multiple notebooks can be given at the command line in a couple of
141 140 different ways:
142 141
143 142 > ipython nbconvert notebook*.ipynb
144 143 > ipython nbconvert notebook1.ipynb notebook2.ipynb
145 144
146 145 or you can specify the notebooks list in a config file, containing::
147 146
148 147 c.NbConvertApp.notebooks = ["my_notebook.ipynb"]
149 148
150 149 > ipython nbconvert --config mycfg.py
151 150 """.format(get_export_names()))
152 151
153 152 # Writer specific variables
154 153 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
155 154 help="""Instance of the writer class used to write the
156 155 results of the conversion.""")
157 156 writer_class = DottedObjectName('FilesWriter', config=True,
158 157 help="""Writer class used to write the
159 158 results of the conversion""")
160 159 writer_aliases = {'fileswriter': 'IPython.nbconvert.writers.files.FilesWriter',
161 160 'debugwriter': 'IPython.nbconvert.writers.debug.DebugWriter',
162 161 'stdoutwriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'}
163 162 writer_factory = Type()
164 163
165 164 def _writer_class_changed(self, name, old, new):
166 165 if new.lower() in self.writer_aliases:
167 166 new = self.writer_aliases[new.lower()]
168 167 self.writer_factory = import_item(new)
169 168
170 169 # Post-processor specific variables
171 170 postprocessor = Instance('IPython.nbconvert.postprocessors.base.PostProcessorBase',
172 171 help="""Instance of the PostProcessor class used to write the
173 172 results of the conversion.""")
174 173
175 174 postprocessor_class = DottedOrNone(config=True,
176 175 help="""PostProcessor class used to write the
177 176 results of the conversion""")
178 177 postprocessor_aliases = {'pdf': 'IPython.nbconvert.postprocessors.pdf.PDFPostProcessor',
179 178 'serve': 'IPython.nbconvert.postprocessors.serve.ServePostProcessor'}
180 179 postprocessor_factory = Type()
181 180
182 181 def _postprocessor_class_changed(self, name, old, new):
183 182 if new.lower() in self.postprocessor_aliases:
184 183 new = self.postprocessor_aliases[new.lower()]
185 184 if new:
186 185 self.postprocessor_factory = import_item(new)
187 186
188 187
189 188 # Other configurable variables
190 189 export_format = CaselessStrEnum(get_export_names(),
191 190 default_value="html",
192 191 config=True,
193 192 help="""The export format to be used."""
194 193 )
195 194
196 195 notebooks = List([], config=True, help="""List of notebooks to convert.
197 196 Wildcards are supported.
198 197 Filenames passed positionally will be added to the list.
199 198 """)
200 199
201 200 @catch_config_error
202 201 def initialize(self, argv=None):
203 202 super(NbConvertApp, self).initialize(argv)
204 203 self.init_syspath()
205 204 self.init_notebooks()
206 205 self.init_writer()
207 206 self.init_postprocessor()
208 207
209 208
210 209
211 210 def init_syspath(self):
212 211 """
213 212 Add the cwd to the sys.path ($PYTHONPATH)
214 213 """
215 214 sys.path.insert(0, os.getcwd())
216 215
217 216
218 217 def init_notebooks(self):
219 218 """Construct the list of notebooks.
220 219 If notebooks are passed on the command-line,
221 220 they override notebooks specified in config files.
222 221 Glob each notebook to replace notebook patterns with filenames.
223 222 """
224 223
225 224 # Specifying notebooks on the command-line overrides (rather than adds)
226 225 # the notebook list
227 226 if self.extra_args:
228 227 patterns = self.extra_args
229 228 else:
230 229 patterns = self.notebooks
231 230
232 231 # Use glob to replace all the notebook patterns with filenames.
233 232 filenames = []
234 233 for pattern in patterns:
235 234
236 235 # Use glob to find matching filenames. Allow the user to convert
237 236 # notebooks without having to type the extension.
238 237 globbed_files = glob.glob(pattern)
239 238 globbed_files.extend(glob.glob(pattern + '.ipynb'))
240 239 if not globbed_files:
241 240 self.log.warn("pattern %r matched no files", pattern)
242 241
243 242 for filename in globbed_files:
244 243 if not filename in filenames:
245 244 filenames.append(filename)
246 245 self.notebooks = filenames
247 246
248 247 def init_writer(self):
249 248 """
250 249 Initialize the writer (which is stateless)
251 250 """
252 251 self._writer_class_changed(None, self.writer_class, self.writer_class)
253 252 self.writer = self.writer_factory(parent=self)
254 253
255 254 def init_postprocessor(self):
256 255 """
257 256 Initialize the postprocessor (which is stateless)
258 257 """
259 258 self._postprocessor_class_changed(None, self.postprocessor_class,
260 259 self.postprocessor_class)
261 260 if self.postprocessor_factory:
262 261 self.postprocessor = self.postprocessor_factory(parent=self)
263 262
264 263 def start(self):
265 264 """
266 265 Ran after initialization completed
267 266 """
268 267 super(NbConvertApp, self).start()
269 268 self.convert_notebooks()
270 269
271 270 def convert_notebooks(self):
272 271 """
273 272 Convert the notebooks in the self.notebook traitlet
274 273 """
275 274 # Export each notebook
276 275 conversion_success = 0
277 276
278 277 if self.output_base != '' and len(self.notebooks) > 1:
279 278 self.log.error(
280 279 """UsageError: --output flag or `NbConvertApp.output_base` config option
281 280 cannot be used when converting multiple notebooks.
282 281 """)
283 282 self.exit(1)
284 283
285 284 exporter = exporter_map[self.export_format](config=self.config)
286 285
287 286 for notebook_filename in self.notebooks:
288 287 self.log.info("Converting notebook %s to %s", notebook_filename, self.export_format)
289 288
290 289 # Get a unique key for the notebook and set it in the resources object.
291 290 basename = os.path.basename(notebook_filename)
292 291 notebook_name = basename[:basename.rfind('.')]
293 292 if self.output_base:
294 293 notebook_name = self.output_base
295 294 resources = {}
296 295 resources['unique_key'] = notebook_name
297 296 resources['output_files_dir'] = '%s_files' % notebook_name
298 297 self.log.info("Support files will be in %s", os.path.join(resources['output_files_dir'], ''))
299 298
300 299 # Try to export
301 300 try:
302 301 output, resources = exporter.from_filename(notebook_filename, resources=resources)
303 302 except ConversionException as e:
304 303 self.log.error("Error while converting '%s'", notebook_filename,
305 304 exc_info=True)
306 305 self.exit(1)
307 306 else:
308 307 write_resultes = self.writer.write(output, resources, notebook_name=notebook_name)
309 308
310 309 #Post-process if post processor has been defined.
311 310 if hasattr(self, 'postprocessor') and self.postprocessor:
312 311 self.postprocessor(write_resultes)
313 312 conversion_success += 1
314 313
315 314 # If nothing was converted successfully, help the user.
316 315 if conversion_success == 0:
317 316 self.print_help()
318 317 sys.exit(-1)
319 318
320 319 #-----------------------------------------------------------------------------
321 320 # Main entry point
322 321 #-----------------------------------------------------------------------------
323 322
324 323 launch_new_instance = NbConvertApp.launch_instance
General Comments 0
You need to be logged in to leave comments. Login now