nbconvert.py
87 lines
| 3.1 KiB
| text/x-python
|
PythonLexer
|
r6220 | #!/usr/bin/env python | ||
|
r6280 | """Convert IPython notebooks to other formats, such as ReST, and HTML. | ||
|
r6220 | |||
|
r6280 | Example: | ||
|
r7914 | ./nbconvert.py --format rst file.ipynb | ||
|
r6220 | |||
|
r7914 | Produces 'file.rst', along with auto-generated figure files | ||
called nb_figure_NN.png. | ||||
|
r6220 | """ | ||
|
r6677 | #----------------------------------------------------------------------------- | ||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
|
r6671 | from __future__ import print_function | ||
|
r6677 | # From IPython | ||
|
r6262 | from IPython.external import argparse | ||
|
r7914 | |||
# local | ||||
|
r8618 | from converters.html import ConverterHTML | ||
from converters.markdown import ConverterMarkdown | ||||
from converters.bloggerhtml import ConverterBloggerHTML | ||||
from converters.rst import ConverterRST | ||||
from converters.latex import ConverterLaTeX | ||||
|
r8620 | from converters.python import ConverterPy | ||
|
r8823 | from converters.reveal import ConverterReveal | ||
|
r7366 | |||
|
r8728 | |||
# When adding a new format, make sure to add it to the `converters` | ||||
# dictionary below. This is used to create the list of known formats, | ||||
# which gets printed in case an unknown format is encounteres, as well | ||||
# as in the help | ||||
|
r8731 | converters = { | ||
'rst': ConverterRST, | ||||
'markdown': ConverterMarkdown, | ||||
'html': ConverterHTML, | ||||
'blogger-html': ConverterBloggerHTML, | ||||
'latex': ConverterLaTeX, | ||||
'py': ConverterPy, | ||||
|
r8823 | 'reveal': ConverterReveal, | ||
|
r8731 | } | ||
|
r8728 | |||
|
r8731 | default_format = 'rst' | ||
|
r8730 | |||
|
r8728 | # Extract the list of known formats and mark the first format as the default. | ||
|
r8730 | known_formats = ', '.join([key + " (default)" if key == default_format else key | ||
|
r8733 | for key in converters]) | ||
|
r8728 | |||
|
r6220 | |||
|
r8750 | def main(infile, format='rst', preamble=None, exclude=None): | ||
|
r6220 | """Convert a notebook to html in one step""" | ||
|
r8728 | try: | ||
ConverterClass = converters[format] | ||||
except KeyError: | ||||
|
r6280 | raise SystemExit("Unknown format '%s', " % format + | ||
|
r8750 | "known formats are: " + known_formats) | ||
|
r6220 | |||
|
r8728 | converter = ConverterClass(infile) | ||
converter.render() | ||||
|
r6677 | #----------------------------------------------------------------------------- | ||
# Script main | ||||
#----------------------------------------------------------------------------- | ||||
|
r6261 | |||
|
r6280 | if __name__ == '__main__': | ||
parser = argparse.ArgumentParser(description=__doc__, | ||||
formatter_class=argparse.RawTextHelpFormatter) | ||||
|
r6261 | # TODO: consider passing file like object around, rather than filenames | ||
# would allow us to process stdin, or even http streams | ||||
|
r8749 | #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), | ||
# default=sys.stdin) | ||||
|
r6261 | |||
#Require a filename as a positional argument | ||||
parser.add_argument('infile', nargs=1) | ||||
|
r6265 | parser.add_argument('-f', '--format', default='rst', | ||
|
r6280 | help='Output format. Supported formats: \n' + | ||
known_formats) | ||||
|
r8750 | parser.add_argument('-p', '--preamble', | ||
help='Path to a user-specified preamble file') | ||||
|
r8753 | parser.add_argument('-e', '--exclude', default='', | ||
help='Comma-separated list of cells to exclude') | ||||
|
r6261 | args = parser.parse_args() | ||
|
r8753 | exclude_cells = [s.strip() for s in args.exclude.split(',')] | ||
|
r8750 | main(infile=args.infile[0], format=args.format, | ||
|
r8753 | preamble=args.preamble, exclude=exclude_cells) | ||