nbconvert.py
74 lines
| 2.7 KiB
| text/x-python
|
PythonLexer
Fernando Perez
|
r6220 | #!/usr/bin/env python | ||
Paul Ivanov
|
r6280 | """Convert IPython notebooks to other formats, such as ReST, and HTML. | ||
Fernando Perez
|
r6220 | |||
Paul Ivanov
|
r6280 | Example: | ||
MinRK
|
r7914 | ./nbconvert.py --format rst file.ipynb | ||
Fernando Perez
|
r6220 | |||
MinRK
|
r7914 | Produces 'file.rst', along with auto-generated figure files | ||
called nb_figure_NN.png. | ||||
Fernando Perez
|
r6220 | """ | ||
Fernando Perez
|
r6677 | #----------------------------------------------------------------------------- | ||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
Fernando Perez
|
r6671 | from __future__ import print_function | ||
Fernando Perez
|
r6220 | |||
Fernando Perez
|
r6671 | |||
Fernando Perez
|
r6677 | # From IPython | ||
Paul Ivanov
|
r6262 | from IPython.external import argparse | ||
MinRK
|
r7914 | |||
# local | ||||
Matthias BUSSONNIER
|
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 | ||||
Matthias BUSSONNIER
|
r8620 | from converters.notebook import ConverterNotebook | ||
from converters.python import ConverterPy | ||||
Jonathan Taylor
|
r7366 | |||
Fernando Perez
|
r8432 | known_formats = "rst (default), html, blogger-html, latex, markdown, py" | ||
Fernando Perez
|
r6220 | |||
Anton I. Sipos
|
r6261 | def main(infile, format='rst'): | ||
Fernando Perez
|
r6220 | """Convert a notebook to html in one step""" | ||
Paul Ivanov
|
r6280 | # XXX: this is just quick and dirty for now. When adding a new format, | ||
# make sure to add it to the `known_formats` string above, which gets | ||||
# printed in in the catch-all else, as well as in the help | ||||
Anton I. Sipos
|
r6261 | if format == 'rst': | ||
converter = ConverterRST(infile) | ||||
converter.render() | ||||
Fernando Perez
|
r7812 | elif format == 'markdown': | ||
converter = ConverterMarkdown(infile) | ||||
converter.render() | ||||
Anton I. Sipos
|
r6261 | elif format == 'html': | ||
MinRK
|
r7914 | converter = ConverterHTML(infile) | ||
Matthias BUSSONNIER
|
r8625 | converter.render() | ||
Fernando Perez
|
r8432 | elif format == 'blogger-html': | ||
converter = ConverterBloggerHTML(infile) | ||||
Matthias BUSSONNIER
|
r8625 | converter.render() | ||
Fernando Perez
|
r6671 | elif format == 'latex': | ||
converter = ConverterLaTeX(infile) | ||||
Matthias BUSSONNIER
|
r8625 | converter.render() | ||
Paul Ivanov
|
r8261 | elif format == 'py': | ||
converter = ConverterPy(infile) | ||||
converter.render() | ||||
Paul Ivanov
|
r6280 | else: | ||
raise SystemExit("Unknown format '%s', " % format + | ||||
"known formats are: " + known_formats) | ||||
Fernando Perez
|
r6220 | |||
Fernando Perez
|
r6677 | #----------------------------------------------------------------------------- | ||
# Script main | ||||
#----------------------------------------------------------------------------- | ||||
Anton I. Sipos
|
r6261 | |||
Paul Ivanov
|
r6280 | if __name__ == '__main__': | ||
parser = argparse.ArgumentParser(description=__doc__, | ||||
formatter_class=argparse.RawTextHelpFormatter) | ||||
Anton I. Sipos
|
r6261 | # TODO: consider passing file like object around, rather than filenames | ||
# would allow us to process stdin, or even http streams | ||||
#parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin) | ||||
#Require a filename as a positional argument | ||||
parser.add_argument('infile', nargs=1) | ||||
Anton I. Sipos
|
r6265 | parser.add_argument('-f', '--format', default='rst', | ||
Paul Ivanov
|
r6280 | help='Output format. Supported formats: \n' + | ||
known_formats) | ||||
Anton I. Sipos
|
r6261 | args = parser.parse_args() | ||
main(infile=args.infile[0], format=args.format) | ||||