nbconvert.py
125 lines
| 4.2 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
|
r6677 | # From IPython | ||
Paul Ivanov
|
r6262 | from IPython.external import argparse | ||
MinRK
|
r7914 | |||
Matthias BUSSONNIER
|
r9875 | # All the stuff needed for the configurable things | ||
from IPython.config.application import Application, catch_config_error | ||||
from IPython.config.configurable import Configurable, SingletonConfigurable | ||||
from IPython.config.loader import Config, ConfigFileNotFound | ||||
from IPython.utils.traitlets import List, Unicode, Type, Bool, Dict, CaselessStrEnum | ||||
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.python import ConverterPy | ||
damianavila
|
r8823 | from converters.reveal import ConverterReveal | ||
Matthias BUSSONNIER
|
r9875 | from converters.base import Converter | ||
Jonathan Taylor
|
r7366 | |||
Maximilian Albert
|
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 | ||||
Maximilian Albert
|
r8731 | converters = { | ||
'rst': ConverterRST, | ||||
'markdown': ConverterMarkdown, | ||||
'html': ConverterHTML, | ||||
'blogger-html': ConverterBloggerHTML, | ||||
'latex': ConverterLaTeX, | ||||
'py': ConverterPy, | ||||
damianavila
|
r8823 | 'reveal': ConverterReveal, | ||
Maximilian Albert
|
r8731 | } | ||
Maximilian Albert
|
r8728 | |||
Maximilian Albert
|
r8731 | default_format = 'rst' | ||
Maximilian Albert
|
r8730 | |||
Maximilian Albert
|
r8728 | # Extract the list of known formats and mark the first format as the default. | ||
Maximilian Albert
|
r8730 | known_formats = ', '.join([key + " (default)" if key == default_format else key | ||
Maximilian Albert
|
r8733 | for key in converters]) | ||
Maximilian Albert
|
r8728 | |||
Matthias BUSSONNIER
|
r9875 | class NbconvertApp(Application): | ||
Fernando Perez
|
r6220 | |||
Matthias BUSSONNIER
|
r9767 | |||
Matthias BUSSONNIER
|
r9875 | fmt = CaselessStrEnum(converters.keys(), | ||
default_value='rst', | ||||
config=True, | ||||
help="Supported conversion format") | ||||
Matthias BUSSONNIER
|
r9767 | |||
Matthias BUSSONNIER
|
r9875 | exclude = List( [], | ||
config=True, | ||||
help = 'list of cells to exclude while converting') | ||||
Matthias BUSSONNIER
|
r9568 | |||
aliases = { | ||||
'format':'NbconvertApp.fmt', | ||||
Matthias BUSSONNIER
|
r9574 | 'exclude':'NbconvertApp.exclude', | ||
Matthias BUSSONNIER
|
r9569 | 'highlight':'Converter.highlight_source', | ||
'preamble':'Converter.preamble', | ||||
Matthias BUSSONNIER
|
r9568 | } | ||
def __init__(self, **kwargs): | ||||
super(NbconvertApp, self).__init__(**kwargs) | ||||
Matthias BUSSONNIER
|
r9569 | # 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) | ||||
Matthias BUSSONNIER
|
r9568 | |||
def initialize(self, argv=None): | ||||
self.parse_command_line(argv) | ||||
cl_config = self.config | ||||
self.update_config(cl_config) | ||||
def run(self): | ||||
Jonathan Frederic
|
r9732 | """Convert a notebook in one step""" | ||
Matthias BUSSONNIER
|
r9568 | ConverterClass = converters[self.fmt] | ||
Matthias BUSSONNIER
|
r9875 | infile = (self.extra_args or [None])[0] | ||
converter = ConverterClass(infile=infile, config=self.config) | ||||
Matthias BUSSONNIER
|
r9568 | converter.render() | ||
def main(): | ||||
"""Convert a notebook to html in one step""" | ||||
app = NbconvertApp.instance() | ||||
Matthias BUSSONNIER
|
r9569 | app.description = __doc__ | ||
Matthias BUSSONNIER
|
r9671 | print(""" | ||
====================================================== | ||||
Warning, we are deprecating this version of nbconvert, | ||||
please consider using the new version. | ||||
====================================================== | ||||
""") | ||||
Matthias BUSSONNIER
|
r9568 | app.initialize() | ||
app.start() | ||||
app.run() | ||||
Fernando Perez
|
r6677 | #----------------------------------------------------------------------------- | ||
# Script main | ||||
#----------------------------------------------------------------------------- | ||||
Anton I. Sipos
|
r6261 | |||
Paul Ivanov
|
r6280 | if __name__ == '__main__': | ||
Anton I. Sipos
|
r6261 | # TODO: consider passing file like object around, rather than filenames | ||
# would allow us to process stdin, or even http streams | ||||
David Warde-Farley
|
r8749 | #parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), | ||
# default=sys.stdin) | ||||
Anton I. Sipos
|
r6261 | |||
Matthias BUSSONNIER
|
r9767 | #parser.add_argument('-e', '--exclude', default='', | ||
# help='Comma-separated list of cells to exclude') | ||||
#exclude_cells = [s.strip() for s in args.exclude.split(',')] | ||||
Matthias BUSSONNIER
|
r9568 | |||
main() | ||||