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