Show More
@@ -1,13 +1,13 b'' | |||||
1 | #!/usr/bin/env python |
|
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 |
|
4 | Example: | |
5 |
|
5 | ./nbconvert.py --format html file.ipynb | ||
6 | ./nb2html.py file.ipynb |
|
|||
7 |
|
6 | |||
8 | Produces 'file.rst' and 'file.html', along with auto-generated figure files |
|
7 | Produces 'file.rst' and 'file.html', along with auto-generated figure files | |
9 | called nb_figure_NN.png. |
|
8 | called nb_figure_NN.png. To avoid the two-step process, ipynb -> rst -> html, | |
10 |
|
9 | use '--format quick-html' which will do ipynb -> html, but won't look as | ||
|
10 | pretty. | |||
11 | """ |
|
11 | """ | |
12 |
|
12 | |||
13 | import os |
|
13 | import os | |
@@ -190,6 +190,9 b' class ConverterRST(Converter):' | |||||
190 | lines = [] |
|
190 | lines = [] | |
191 |
|
191 | |||
192 | if 'png' in output: |
|
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 | infile = 'nb_figure_%s.png' % self.figures_counter |
|
196 | infile = 'nb_figure_%s.png' % self.figures_counter | |
194 | fullname = os.path.join(self.dirpath, infile) |
|
197 | fullname = os.path.join(self.dirpath, infile) | |
195 | with open(fullname, 'w') as f: |
|
198 | with open(fullname, 'w') as f: | |
@@ -352,9 +355,13 b' def rst2simplehtml(infile):' | |||||
352 |
|
355 | |||
353 | return newfname |
|
356 | return newfname | |
354 |
|
357 | |||
|
358 | known_formats = "rst (default), html, quick-html" | |||
355 |
|
359 | |||
356 | def main(infile, format='rst'): |
|
360 | def main(infile, format='rst'): | |
357 | """Convert a notebook to html in one step""" |
|
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 | if format == 'rst': |
|
365 | if format == 'rst': | |
359 | converter = ConverterRST(infile) |
|
366 | converter = ConverterRST(infile) | |
360 | converter.render() |
|
367 | converter.render() | |
@@ -366,11 +373,15 b" def main(infile, format='rst'):" | |||||
366 | elif format == 'quick-html': |
|
373 | elif format == 'quick-html': | |
367 | converter = ConverterQuickHTML(infile) |
|
374 | converter = ConverterQuickHTML(infile) | |
368 | rstfname = converter.render() |
|
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 | # TODO: consider passing file like object around, rather than filenames |
|
385 | # TODO: consider passing file like object around, rather than filenames | |
375 | # would allow us to process stdin, or even http streams |
|
386 | # would allow us to process stdin, or even http streams | |
376 | #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin) |
|
387 | #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin) | |
@@ -378,6 +389,7 b" if __name__ == '__main__':" | |||||
378 | #Require a filename as a positional argument |
|
389 | #Require a filename as a positional argument | |
379 | parser.add_argument('infile', nargs=1) |
|
390 | parser.add_argument('infile', nargs=1) | |
380 | parser.add_argument('-f', '--format', default='rst', |
|
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 | args = parser.parse_args() |
|
394 | args = parser.parse_args() | |
383 | main(infile=args.infile[0], format=args.format) |
|
395 | main(infile=args.infile[0], format=args.format) |
General Comments 0
You need to be logged in to leave comments.
Login now