Show More
@@ -1,13 +1,13 b'' | |||
|
1 | 1 | #!/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. | |
|
3 | 3 | |
|
4 | Usage | |
|
5 | ||
|
6 | ./nb2html.py file.ipynb | |
|
4 | Example: | |
|
5 | ./nbconvert.py --format html file.ipynb | |
|
7 | 6 | |
|
8 | 7 | Produces '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. | |
|
11 | 11 | """ |
|
12 | 12 | |
|
13 | 13 | import os |
@@ -190,6 +190,9 b' class ConverterRST(Converter):' | |||
|
190 | 190 | lines = [] |
|
191 | 191 | |
|
192 | 192 | 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 | |
|
193 | 196 | infile = 'nb_figure_%s.png' % self.figures_counter |
|
194 | 197 | fullname = os.path.join(self.dirpath, infile) |
|
195 | 198 | with open(fullname, 'w') as f: |
@@ -352,9 +355,13 b' def rst2simplehtml(infile):' | |||
|
352 | 355 | |
|
353 | 356 | return newfname |
|
354 | 357 | |
|
358 | known_formats = "rst (default), html, quick-html" | |
|
355 | 359 | |
|
356 | 360 | def main(infile, format='rst'): |
|
357 | 361 | """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 | |
|
358 | 365 | if format == 'rst': |
|
359 | 366 | converter = ConverterRST(infile) |
|
360 | 367 | converter.render() |
@@ -366,11 +373,15 b" def main(infile, format='rst'):" | |||
|
366 | 373 | elif format == 'quick-html': |
|
367 | 374 | converter = ConverterQuickHTML(infile) |
|
368 | 375 | rstfname = converter.render() |
|
376 | else: | |
|
377 | raise SystemExit("Unknown format '%s', " % format + | |
|
378 | "known formats are: " + known_formats) | |
|
369 | 379 | |
|
370 | 380 | |
|
371 | if __name__ == '__main__': | |
|
372 | parser = argparse.ArgumentParser(description='nbconvert: Convert IPython notebooks to other formats') | |
|
373 | 381 | |
|
382 | if __name__ == '__main__': | |
|
383 | parser = argparse.ArgumentParser(description=__doc__, | |
|
384 | formatter_class=argparse.RawTextHelpFormatter) | |
|
374 | 385 | # TODO: consider passing file like object around, rather than filenames |
|
375 | 386 | # would allow us to process stdin, or even http streams |
|
376 | 387 | #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin) |
@@ -378,6 +389,7 b" if __name__ == '__main__':" | |||
|
378 | 389 | #Require a filename as a positional argument |
|
379 | 390 | parser.add_argument('infile', nargs=1) |
|
380 | 391 | parser.add_argument('-f', '--format', default='rst', |
|
381 |
help='Output format. Supported formats: |
|
|
392 | help='Output format. Supported formats: \n' + | |
|
393 | known_formats) | |
|
382 | 394 | args = parser.parse_args() |
|
383 | 395 | main(infile=args.infile[0], format=args.format) |
General Comments 0
You need to be logged in to leave comments.
Login now