nbconvertapp.py
226 lines
| 8.1 KiB
| text/x-python
|
PythonLexer
Brian E. Granger
|
r11087 | #!/usr/bin/env python | ||
MinRK
|
r11448 | """NBConvert is a utility for conversion of .ipynb files. | ||
Brian E. Granger
|
r11087 | |||
MinRK
|
r11448 | Command-line interface for the NbConvert conversion utility. | ||
Brian E. Granger
|
r11087 | """ | ||
#----------------------------------------------------------------------------- | ||||
#Copyright (c) 2013, the IPython Development Team. | ||||
# | ||||
#Distributed under the terms of the Modified BSD License. | ||||
# | ||||
#The full license is in the file COPYING.txt, distributed with this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
#Imports | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r11632 | # Stdlib imports | ||
Brian E. Granger
|
r11087 | from __future__ import print_function | ||
import sys | ||||
import os | ||||
Jonathan Frederic
|
r11367 | import glob | ||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11632 | # From IPython | ||
MinRK
|
r11448 | from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags | ||
MinRK
|
r11453 | from IPython.config import catch_config_error, Configurable | ||
MinRK
|
r11454 | from IPython.utils.traitlets import ( | ||
Unicode, List, Instance, DottedObjectName, Type, CaselessStrEnum, | ||||
) | ||||
Jonathan Frederic
|
r11367 | from IPython.utils.importstring import import_item | ||
Brian E. Granger
|
r11087 | |||
MinRK
|
r11453 | from .exporters.export import export_by_name, get_export_names, ExporterNameError | ||
from IPython.nbconvert import exporters, transformers, writers | ||||
Jonathan Frederic
|
r11420 | from .utils.base import NbConvertBase | ||
Brian E. Granger
|
r11087 | |||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r11367 | #Classes and functions | ||
Brian E. Granger
|
r11087 | #----------------------------------------------------------------------------- | ||
MinRK
|
r11448 | nbconvert_aliases = {} | ||
nbconvert_aliases.update(base_aliases) | ||||
nbconvert_aliases.update({ | ||||
'format' : 'NbConvertApp.export_format', | ||||
'notebooks' : 'NbConvertApp.notebooks', | ||||
'writer' : 'NbConvertApp.writer_class', | ||||
}) | ||||
nbconvert_flags = {} | ||||
nbconvert_flags.update(base_flags) | ||||
nbconvert_flags.update({ | ||||
'stdout' : ( | ||||
{'NbConvertApp' : {'writer_class' : "StdoutWriter"}}, | ||||
"Write notebook output to stdout instead of files." | ||||
) | ||||
}) | ||||
Jonathan Frederic
|
r11367 | class NbConvertApp(BaseIPythonApplication): | ||
"""Application used to convert to and from notebook file type (*.ipynb)""" | ||||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11406 | name = 'ipython-nbconvert' | ||
MinRK
|
r11448 | aliases = nbconvert_aliases | ||
flags = nbconvert_flags | ||||
def _classes_default(self): | ||||
MinRK
|
r11453 | classes = [NbConvertBase] | ||
for pkg in (exporters, transformers, writers): | ||||
for name in dir(pkg): | ||||
cls = getattr(pkg, name) | ||||
if isinstance(cls, type) and issubclass(cls, Configurable): | ||||
classes.append(cls) | ||||
return classes | ||||
Paul Ivanov
|
r11248 | |||
Jonathan Frederic
|
r11367 | description = Unicode( | ||
MinRK
|
r11454 | u"""This application is used to convert notebook files (*.ipynb) | ||
to various other formats.""") | ||||
Paul Ivanov
|
r11251 | |||
Jonathan Frederic
|
r11367 | examples = Unicode(u""" | ||
MinRK
|
r11454 | The simplest way to use nbconvert is | ||
> ipython nbconvert mynotebook.ipynb | ||||
which will convert mynotebook.ipynb to the default format (probably HTML). | ||||
You can specify the export format with `--format`. | ||||
Options include {0} | ||||
> ipython nbconvert --format latex mynotebook.ipnynb | ||||
You can also pipe the output to stdout, rather than a file | ||||
> ipython nbconvert mynotebook.ipynb --stdout | ||||
Jonathan Frederic
|
r11367 | Multiple notebooks can be given at the command line in a couple of | ||
different ways: | ||||
> ipython nbconvert notebook*.ipynb | ||||
> ipython nbconvert notebook1.ipynb notebook2.ipynb | ||||
MinRK
|
r11469 | |||
or you can specify the notebooks list in a config file, containing:: | ||||
c.NbConvertApp.notebooks = ["my_notebook.ipynb"] | ||||
> ipython nbconvert --config mycfg.py | ||||
MinRK
|
r11454 | """.format(get_export_names())) | ||
Jonathan Frederic
|
r11632 | # Writer specific variables | ||
Jonathan Frederic
|
r11367 | writer = Instance('IPython.nbconvert.writers.base.WriterBase', | ||
help="""Instance of the writer class used to write the | ||||
results of the conversion.""") | ||||
writer_class = DottedObjectName('FilesWriter', config=True, | ||||
help="""Writer class used to write the | ||||
results of the conversion""") | ||||
writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter', | ||||
'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter', | ||||
'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'} | ||||
writer_factory = Type() | ||||
Paul Ivanov
|
r11251 | |||
Jonathan Frederic
|
r11367 | def _writer_class_changed(self, name, old, new): | ||
if new in self.writer_aliases: | ||||
new = self.writer_aliases[new] | ||||
self.writer_factory = import_item(new) | ||||
Paul Ivanov
|
r11248 | |||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11632 | # Other configurable variables | ||
MinRK
|
r11454 | export_format = CaselessStrEnum(get_export_names(), | ||
default_value="full_html", | ||||
config=True, | ||||
help="""The export format to be used.""" | ||||
) | ||||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11367 | notebooks = List([], config=True, help="""List of notebooks to convert. | ||
MinRK
|
r11454 | Wildcards are supported. | ||
Filenames passed positionally will be added to the list. | ||||
""") | ||||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11367 | @catch_config_error | ||
def initialize(self, argv=None): | ||||
super(NbConvertApp, self).initialize(argv) | ||||
MinRK
|
r11448 | self.init_notebooks() | ||
Jonathan Frederic
|
r11367 | self.init_writer() | ||
Brian E. Granger
|
r11087 | |||
MinRK
|
r11448 | def init_notebooks(self): | ||
MinRK
|
r11468 | """Construct the list of notebooks. | ||
If notebooks are passed on the command-line, | ||||
they override notebooks specified in config files. | ||||
Glob each notebook to replace notebook patterns with filenames. | ||||
Jonathan Frederic
|
r11367 | """ | ||
Brian E. Granger
|
r11087 | |||
MinRK
|
r11468 | # Specifying notebooks on the command-line overrides (rather than adds) | ||
# the notebook list | ||||
if self.extra_args: | ||||
patterns = self.extra_args | ||||
else: | ||||
patterns = self.notebooks | ||||
Jonathan Frederic
|
r11367 | |||
Jonathan Frederic
|
r11632 | # Use glob to replace all the notebook patterns with filenames. | ||
Jonathan Frederic
|
r11367 | filenames = [] | ||
MinRK
|
r11448 | for pattern in patterns: | ||
Jonathan Frederic
|
r11660 | |||
# Use glob to find matching filenames. Allow the user to convert | ||||
# notebooks without having to type the extension. | ||||
globbed_files = glob.glob(pattern) | ||||
globbed_files.extend(glob.glob(pattern + '.ipynb')) | ||||
for filename in globbed_files: | ||||
Jonathan Frederic
|
r11367 | if not filename in filenames: | ||
filenames.append(filename) | ||||
self.notebooks = filenames | ||||
def init_writer(self): | ||||
""" | ||||
Initialize the writer (which is stateless) | ||||
""" | ||||
self._writer_class_changed(None, self.writer_class, self.writer_class) | ||||
self.writer = self.writer_factory(parent=self) | ||||
Brian E. Granger
|
r11087 | |||
MinRK
|
r11448 | def start(self): | ||
Brian E. Granger
|
r11087 | """ | ||
MinRK
|
r11448 | Ran after initialization completed | ||
Jonathan Frederic
|
r11367 | """ | ||
Brian E. Granger
|
r11087 | super(NbConvertApp, self).start() | ||
Jonathan Frederic
|
r11400 | self.convert_notebooks() | ||
def convert_notebooks(self): | ||||
""" | ||||
Convert the notebooks in the self.notebook traitlet | ||||
""" | ||||
Jonathan Frederic
|
r11632 | # Export each notebook | ||
Jonathan Frederic
|
r11400 | conversion_success = 0 | ||
Jonathan Frederic
|
r11367 | for notebook_filename in self.notebooks: | ||
Jonathan Frederic
|
r11632 | # Get a unique key for the notebook and set it in the resources object. | ||
Jonathan Frederic
|
r11367 | basename = os.path.basename(notebook_filename) | ||
notebook_name = basename[:basename.rfind('.')] | ||||
resources = {} | ||||
resources['unique_key'] = notebook_name | ||||
Jonathan Frederic
|
r11631 | resources['output_files_dir'] = '%s_files' % notebook_name | ||
Jonathan Frederic
|
r11367 | |||
Jonathan Frederic
|
r11632 | # Try to export | ||
Jonathan Frederic
|
r11368 | try: | ||
Jonathan Frederic
|
r11379 | output, resources = export_by_name(self.export_format, | ||
Jonathan Frederic
|
r11368 | notebook_filename, | ||
resources=resources, | ||||
config=self.config) | ||||
Jonathan Frederic
|
r11372 | except ExporterNameError as e: | ||
Jonathan Frederic
|
r11368 | print("Error: '%s' exporter not found." % self.export_format, | ||
file=sys.stderr) | ||||
print("Known exporters are:", | ||||
"\n\t" + "\n\t".join(get_export_names()), | ||||
file=sys.stderr) | ||||
sys.exit(-1) | ||||
Jonathan Frederic
|
r11400 | else: | ||
self.writer.write(output, resources, notebook_name=notebook_name) | ||||
conversion_success += 1 | ||||
Jonathan Frederic
|
r11632 | # If nothing was converted successfully, help the user. | ||
Jonathan Frederic
|
r11400 | if conversion_success == 0: | ||
Jonathan Frederic
|
r11666 | self.print_help() | ||
Jonathan Frederic
|
r11667 | sys.exit(-1) | ||
Jonathan Frederic
|
r11367 | |||
Brian E. Granger
|
r11087 | |||
#----------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r11092 | # Main entry point | ||
Brian E. Granger
|
r11087 | #----------------------------------------------------------------------------- | ||
MinRK
|
r11176 | launch_new_instance = NbConvertApp.launch_instance | ||