nbconvert.py
117 lines
| 4.0 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 | |||
|
r8979 | # All the stuff needed for the configurable things | ||
from IPython.config.application import Application, catch_config_error | ||||
|
r8980 | from IPython.config.configurable import Configurable, SingletonConfigurable | ||
|
r8979 | from IPython.config.loader import Config, ConfigFileNotFound | ||
from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum | ||||
|
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 | ||
|
r8980 | from converters.base import Converter | ||
|
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, | ||||
} | ||||
|
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 | |||
|
r8979 | class NbconvertApp(Application): | ||
|
r6220 | |||
|
r8979 | |||
fmt = CaselessStrEnum(converters.keys(), | ||||
|
r8980 | default_value='rst', | ||
|
r8979 | config=True, | ||
help="Supported conversion format") | ||||
exclude = List( [], | ||||
config=True, | ||||
help = 'list of cells to exclude while converting') | ||||
aliases = { | ||||
'format':'NbconvertApp.fmt', | ||||
|
r8985 | 'exclude':'NbconvertApp.exclude', | ||
|
r8980 | 'highlight':'Converter.highlight_source', | ||
'preamble':'Converter.preamble', | ||||
|
r8979 | } | ||
def __init__(self, **kwargs): | ||||
super(NbconvertApp, self).__init__(**kwargs) | ||||
|
r8980 | # ensure those are registerd | ||
self.classes.insert(0,Converter) | ||||
self.classes.insert(0,ConverterRST) | ||||
self.classes.insert(0,ConverterMarkdown) | ||||
self.classes.insert(0,ConverterBloggerHTML) | ||||
self.classes.insert(0,ConverterLaTeX) | ||||
self.classes.insert(0,ConverterPy) | ||||
|
r8979 | |||
def initialize(self, argv=None): | ||||
self.parse_command_line(argv) | ||||
cl_config = self.config | ||||
self.update_config(cl_config) | ||||
def run(self): | ||||
"""Convert a notebook to html in one step""" | ||||
ConverterClass = converters[self.fmt] | ||||
|
r8983 | infile = (self.extra_args or [None])[0] | ||
converter = ConverterClass(infile=infile, config=self.config) | ||||
|
r8979 | converter.render() | ||
def main(): | ||||
"""Convert a notebook to html in one step""" | ||||
app = NbconvertApp.instance() | ||||
|
r8980 | app.description = __doc__ | ||
|
r8979 | app.initialize() | ||
app.start() | ||||
app.run() | ||||
|
r6677 | #----------------------------------------------------------------------------- | ||
# Script main | ||||
#----------------------------------------------------------------------------- | ||||
|
r6261 | |||
|
r6280 | if __name__ == '__main__': | ||
|
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 | |||
|
r8979 | #parser.add_argument('-e', '--exclude', default='', | ||
# help='Comma-separated list of cells to exclude') | ||||
#exclude_cells = [s.strip() for s in args.exclude.split(',')] | ||||
main() | ||||