11#!/usr/bin/env python
2- """A really simple notebook to rst/html exporter .
2+ """Convert IPython notebooks to other formats, such as ReST, and HTML .
33
4- Usage
5-
6- ./nb2html.py file.ipynb
4+ Example:
5+ ./nbconvert.py --format html file.ipynb
76
87Produces 'file.rst' and 'file.html', along with auto-generated figure files
9- called nb_figure_NN.png.
10-
8+ called nb_figure_NN.png. To avoid the two-step process, ipynb -> rst -> html,
9+ use '--format quick-html' which will do ipynb -> html, but won't look as
10+ pretty.
1111"""
1212
1313import os
@@ -190,6 +190,9 @@ def render_display_data(self, output):
190190 lines = []
191191
192192 if 'png' in output :
193+ # XXX: make the figures notebooks specific (i.e. self.infile) so
194+ # that multiple notebook conversions don't clobber each other's
195+ # figures
193196 infile = 'nb_figure_%s.png' % self .figures_counter
194197 fullname = os .path .join (self .dirpath , infile )
195198 with open (fullname , 'w' ) as f :
@@ -352,9 +355,13 @@ def rst2simplehtml(infile):
352355
353356 return newfname
354357
358+ known_formats = "rst (default), html, quick-html"
355359
356360def main (infile , format = 'rst' ):
357361 """Convert a notebook to html in one step"""
362+ # XXX: this is just quick and dirty for now. When adding a new format,
363+ # make sure to add it to the `known_formats` string above, which gets
364+ # printed in in the catch-all else, as well as in the help
358365 if format == 'rst' :
359366 converter = ConverterRST (infile )
360367 converter .render ()
@@ -366,18 +373,23 @@ def main(infile, format='rst'):
366373 elif format == 'quick-html' :
367374 converter = ConverterQuickHTML (infile )
368375 rstfname = converter .render ()
376+ else :
377+ raise SystemExit ("Unknown format '%s', " % format +
378+ "known formats are: " + known_formats )
369379
370380
371- if __name__ == '__main__' :
372- parser = argparse .ArgumentParser (description = 'nbconvert: Convert IPython notebooks to other formats' )
373381
382+ if __name__ == '__main__' :
383+ parser = argparse .ArgumentParser (description = __doc__ ,
384+ formatter_class = argparse .RawTextHelpFormatter )
374385 # TODO: consider passing file like object around, rather than filenames
375386 # would allow us to process stdin, or even http streams
376387 #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin)
377388
378389 #Require a filename as a positional argument
379390 parser .add_argument ('infile' , nargs = 1 )
380391 parser .add_argument ('-f' , '--format' , default = 'rst' ,
381- help = 'Output format. Supported formats: rst (default), html.' )
392+ help = 'Output format. Supported formats: \n ' +
393+ known_formats )
382394 args = parser .parse_args ()
383395 main (infile = args .infile [0 ], format = args .format )
0 commit comments