nbconvertapp.py
407 lines
| 14.9 KiB
| text/x-python
|
PythonLexer
Brian E. Granger
|
r11087 | #!/usr/bin/env python | ||
MinRK
|
r16265 | """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 | """ | ||
MinRK
|
r16265 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
Brian E. Granger
|
r11087 | |||
from __future__ import print_function | ||||
MinRK
|
r11842 | |||
import logging | ||||
Brian E. Granger
|
r11087 | import sys | ||
import os | ||||
Jonathan Frederic
|
r11367 | import glob | ||
Brian E. Granger
|
r11087 | |||
MinRK
|
r11448 | from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags | ||
MinRK
|
r12358 | from IPython.core.profiledir import ProfileDir | ||
MinRK
|
r11453 | from IPython.config import catch_config_error, Configurable | ||
MinRK
|
r11454 | from IPython.utils.traitlets import ( | ||
Jessica B. Hamrick
|
r20583 | Unicode, List, Instance, DottedObjectName, Type, CaselessStrEnum, Bool, | ||
MinRK
|
r11454 | ) | ||
Jonathan Frederic
|
r11367 | from IPython.utils.importstring import import_item | ||
Brian E. Granger
|
r11087 | |||
MinRK
|
r11865 | from .exporters.export import get_export_names, exporter_map | ||
Jake Vanderplas
|
r12249 | from IPython.nbconvert import exporters, preprocessors, writers, postprocessors | ||
Jonathan Frederic
|
r11420 | from .utils.base import NbConvertBase | ||
David Wolever
|
r11703 | from .utils.exceptions import ConversionException | ||
Brian E. Granger
|
r11087 | |||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r11367 | #Classes and functions | ||
Brian E. Granger
|
r11087 | #----------------------------------------------------------------------------- | ||
Jonathan Frederic
|
r11747 | class DottedOrNone(DottedObjectName): | ||
""" | ||||
A string holding a valid dotted object name in Python, such as A.b3._c | ||||
Also allows for None type.""" | ||||
default_value = u'' | ||||
def validate(self, obj, value): | ||||
if value is not None and len(value) > 0: | ||||
return super(DottedOrNone, self).validate(obj, value) | ||||
else: | ||||
return value | ||||
MinRK
|
r11448 | nbconvert_aliases = {} | ||
nbconvert_aliases.update(base_aliases) | ||||
nbconvert_aliases.update({ | ||||
Jonathan Frederic
|
r11735 | 'to' : 'NbConvertApp.export_format', | ||
Matthias BUSSONNIER
|
r12500 | 'template' : 'TemplateExporter.template_file', | ||
MinRK
|
r11448 | 'writer' : 'NbConvertApp.writer_class', | ||
Jonathan Frederic
|
r12258 | 'post': 'NbConvertApp.postprocessor_class', | ||
damianavila
|
r11894 | 'output': 'NbConvertApp.output_base', | ||
MinRK
|
r12440 | 'reveal-prefix': 'RevealHelpPreprocessor.url_prefix', | ||
MinRK
|
r18247 | 'nbformat': 'NotebookExporter.nbformat_version', | ||
MinRK
|
r11448 | }) | ||
nbconvert_flags = {} | ||||
nbconvert_flags.update(base_flags) | ||||
nbconvert_flags.update({ | ||||
MinRK
|
r18459 | 'execute' : ( | ||
{'ExecutePreprocessor' : {'enabled' : True}}, | ||||
"Execute the notebook prior to export." | ||||
), | ||||
MinRK
|
r11448 | 'stdout' : ( | ||
{'NbConvertApp' : {'writer_class' : "StdoutWriter"}}, | ||||
"Write notebook output to stdout instead of files." | ||||
Jessica B. Hamrick
|
r20583 | ), | ||
Jessica B. Hamrick
|
r20584 | 'inplace' : ( | ||
{ | ||||
'NbConvertApp' : {'use_output_suffix' : False}, | ||||
'FilesWriter': {'build_directory': ''} | ||||
}, | ||||
"""Run nbconvert in place, overwriting the existing notebook (only | ||||
relevant when converting to notebook format)""" | ||||
MinRK
|
r11448 | ) | ||
}) | ||||
Jonathan Frederic
|
r11367 | class NbConvertApp(BaseIPythonApplication): | ||
Thomas Kluyver
|
r13597 | """Application used to convert from notebook file type (``*.ipynb``)""" | ||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11406 | name = 'ipython-nbconvert' | ||
MinRK
|
r11448 | aliases = nbconvert_aliases | ||
flags = nbconvert_flags | ||||
MinRK
|
r11842 | def _log_level_default(self): | ||
return logging.INFO | ||||
MinRK
|
r11448 | def _classes_default(self): | ||
MinRK
|
r12358 | classes = [NbConvertBase, ProfileDir] | ||
jakobgager
|
r12902 | for pkg in (exporters, preprocessors, writers, postprocessors): | ||
MinRK
|
r11453 | for name in dir(pkg): | ||
cls = getattr(pkg, name) | ||||
if isinstance(cls, type) and issubclass(cls, Configurable): | ||||
classes.append(cls) | ||||
MinRK
|
r12358 | |||
MinRK
|
r11453 | return classes | ||
Paul Ivanov
|
r11248 | |||
Jonathan Frederic
|
r11367 | description = Unicode( | ||
MinRK
|
r11454 | u"""This application is used to convert notebook files (*.ipynb) | ||
Jonathan Frederic
|
r11755 | to various other formats. | ||
WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.""") | ||||
Paul Ivanov
|
r11251 | |||
Matthias BUSSONNIER
|
r11801 | output_base = Unicode('', config=True, help='''overwrite base name use for output files. | ||
MinRK
|
r18247 | can only be used when converting one notebook at a time. | ||
Matthias BUSSONNIER
|
r11801 | ''') | ||
Jessica B. Hamrick
|
r20583 | use_output_suffix = Bool( | ||
True, | ||||
config=True, | ||||
help="""Whether to apply a suffix prior to the extension (only relevant | ||||
when converting to notebook format). The suffix is determined by | ||||
the exporter, and is usually '.nbconvert'.""") | ||||
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). | ||||
Jonathan Frederic
|
r11741 | You can specify the export format with `--to`. | ||
MinRK
|
r11454 | Options include {0} | ||
dexterdev
|
r16435 | > ipython nbconvert --to latex mynotebook.ipynb | ||
Jonathan Frederic
|
r11741 | |||
Jonathan Frederic
|
r11745 | Both HTML and LaTeX support multiple output templates. LaTeX includes | ||
Pierre Haessig
|
r16720 | 'base', 'article' and 'report'. HTML includes 'basic' and 'full'. You | ||
Jonathan Frederic
|
r11744 | can specify the flavor of the format used. | ||
Jonathan Frederic
|
r11741 | |||
Jonathan Frederic
|
r11761 | > ipython nbconvert --to html --template basic mynotebook.ipynb | ||
MinRK
|
r11454 | |||
You can also pipe the output to stdout, rather than a file | ||||
> ipython nbconvert mynotebook.ipynb --stdout | ||||
Jonathan Frederic
|
r11742 | |||
MinRK
|
r16265 | PDF is generated via latex | ||
Jonathan Frederic
|
r11742 | |||
MinRK
|
r16265 | > ipython nbconvert mynotebook.ipynb --to pdf | ||
MinRK
|
r11454 | |||
damianavila
|
r11776 | You can get (and serve) a Reveal.js-powered slideshow | ||
> ipython nbconvert myslides.ipynb --to slides --post serve | ||||
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
|
r11747 | |||
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""") | ||||
Jonathan Frederic
|
r12054 | writer_aliases = {'fileswriter': 'IPython.nbconvert.writers.files.FilesWriter', | ||
'debugwriter': 'IPython.nbconvert.writers.debug.DebugWriter', | ||||
'stdoutwriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'} | ||||
Jonathan Frederic
|
r11367 | writer_factory = Type() | ||
Paul Ivanov
|
r11251 | |||
Jonathan Frederic
|
r11367 | def _writer_class_changed(self, name, old, new): | ||
Jonathan Frederic
|
r12070 | if new.lower() in self.writer_aliases: | ||
new = self.writer_aliases[new.lower()] | ||||
Jonathan Frederic
|
r11367 | self.writer_factory = import_item(new) | ||
Paul Ivanov
|
r11248 | |||
Jonathan Frederic
|
r11747 | # Post-processor specific variables | ||
Jonathan Frederic
|
r12258 | postprocessor = Instance('IPython.nbconvert.postprocessors.base.PostProcessorBase', | ||
Jonathan Frederic
|
r11747 | help="""Instance of the PostProcessor class used to write the | ||
results of the conversion.""") | ||||
Jonathan Frederic
|
r12258 | postprocessor_class = DottedOrNone(config=True, | ||
Jonathan Frederic
|
r11747 | help="""PostProcessor class used to write the | ||
results of the conversion""") | ||||
MinRK
|
r16418 | postprocessor_aliases = {'serve': 'IPython.nbconvert.postprocessors.serve.ServePostProcessor'} | ||
Jonathan Frederic
|
r12258 | postprocessor_factory = Type() | ||
Jonathan Frederic
|
r11747 | |||
Jonathan Frederic
|
r12258 | def _postprocessor_class_changed(self, name, old, new): | ||
if new.lower() in self.postprocessor_aliases: | ||||
new = self.postprocessor_aliases[new.lower()] | ||||
Jonathan Frederic
|
r11747 | if new: | ||
Jonathan Frederic
|
r12258 | self.postprocessor_factory = import_item(new) | ||
Jonathan Frederic
|
r11747 | |||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11632 | # Other configurable variables | ||
MinRK
|
r11454 | export_format = CaselessStrEnum(get_export_names(), | ||
Jonathan Frederic
|
r11735 | default_value="html", | ||
MinRK
|
r11454 | 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): | ||||
Jonathan Frederic
|
r11652 | self.init_syspath() | ||
jon
|
r16356 | super(NbConvertApp, self).initialize(argv) | ||
MinRK
|
r11448 | self.init_notebooks() | ||
Jonathan Frederic
|
r11367 | self.init_writer() | ||
Jonathan Frederic
|
r12258 | self.init_postprocessor() | ||
Jonathan Frederic
|
r11747 | |||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11651 | |||
Jonathan Frederic
|
r11652 | def init_syspath(self): | ||
Jonathan Frederic
|
r11651 | """ | ||
Add the cwd to the sys.path ($PYTHONPATH) | ||||
""" | ||||
Jonathan Frederic
|
r11688 | sys.path.insert(0, os.getcwd()) | ||
Jonathan Frederic
|
r11651 | |||
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')) | ||||
MinRK
|
r11851 | if not globbed_files: | ||
self.log.warn("pattern %r matched no files", pattern) | ||||
Jonathan Frederic
|
r11660 | |||
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) | ||||
Jessica B. Hamrick
|
r20584 | if hasattr(self.writer, 'build_directory') and self.writer.build_directory != '': | ||
self.use_output_suffix = False | ||||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r12258 | def init_postprocessor(self): | ||
Jonathan Frederic
|
r11747 | """ | ||
Jonathan Frederic
|
r12258 | Initialize the postprocessor (which is stateless) | ||
Jonathan Frederic
|
r11747 | """ | ||
Jonathan Frederic
|
r12258 | self._postprocessor_class_changed(None, self.postprocessor_class, | ||
self.postprocessor_class) | ||||
if self.postprocessor_factory: | ||||
self.postprocessor = self.postprocessor_factory(parent=self) | ||||
Jonathan Frederic
|
r11747 | |||
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() | ||
Jessica B. Hamrick
|
r20746 | def init_single_notebook_resources(self, notebook_filename): | ||
"""Step 1: Initialize resources | ||||
This intializes the resources dictionary for a single notebook. This | ||||
method should return the resources dictionary, and MUST include the | ||||
following keys: | ||||
- profile_dir: the location of the profile directory | ||||
- unique_key: the notebook name | ||||
- output_files_dir: a directory where output files (not including | ||||
the notebook itself) should be saved | ||||
""" | ||||
# Get a unique key for the notebook and set it in the resources object. | ||||
basename = os.path.basename(notebook_filename) | ||||
notebook_name = basename[:basename.rfind('.')] | ||||
if self.output_base: | ||||
# strip duplicate extension from output_base, to avoid Basname.ext.ext | ||||
if getattr(self.exporter, 'file_extension', False): | ||||
base, ext = os.path.splitext(self.output_base) | ||||
if ext == self.exporter.file_extension: | ||||
self.output_base = base | ||||
notebook_name = self.output_base | ||||
self.log.debug("Notebook name is '%s'", notebook_name) | ||||
# first initialize the resources we want to use | ||||
resources = {} | ||||
resources['profile_dir'] = self.profile_dir.location | ||||
resources['unique_key'] = notebook_name | ||||
resources['output_files_dir'] = '%s_files' % notebook_name | ||||
return resources | ||||
def export_single_notebook(self, notebook_filename, resources): | ||||
"""Step 2: Export the notebook | ||||
Exports the notebook to a particular format according to the specified | ||||
exporter. This function returns the output and (possibly modified) | ||||
resources from the exporter. | ||||
""" | ||||
try: | ||||
output, resources = self.exporter.from_filename(notebook_filename, resources=resources) | ||||
except ConversionException: | ||||
self.log.error("Error while converting '%s'", notebook_filename, exc_info=True) | ||||
self.exit(1) | ||||
return output, resources | ||||
def write_single_notebook(self, output, resources): | ||||
"""Step 3: Write the notebook to file | ||||
This writes output from the exporter to file using the specified writer. | ||||
It returns the results from the writer. | ||||
""" | ||||
if 'unique_key' not in resources: | ||||
raise KeyError("unique_key MUST be specified in the resources, but it is not") | ||||
notebook_name = resources['unique_key'] | ||||
if self.use_output_suffix and not self.output_base: | ||||
notebook_name += resources.get('output_suffix', '') | ||||
write_results = self.writer.write( | ||||
output, resources, notebook_name=notebook_name) | ||||
return write_results | ||||
def postprocess_single_notebook(self, write_results): | ||||
"""Step 4: Postprocess the notebook | ||||
This postprocesses the notebook after it has been written, taking as an | ||||
argument the results of writing the notebook to file. This only actually | ||||
does anything if a postprocessor has actually been specified. | ||||
""" | ||||
# Post-process if post processor has been defined. | ||||
if hasattr(self, 'postprocessor') and self.postprocessor: | ||||
self.postprocessor(write_results) | ||||
def convert_single_notebook(self, notebook_filename): | ||||
"""Convert a single notebook. Performs the following steps: | ||||
1. Initialize notebook resources | ||||
2. Export the notebook to a particular format | ||||
3. Write the exported notebook to file | ||||
4. (Maybe) postprocess the written file | ||||
""" | ||||
self.log.info("Converting notebook %s to %s", notebook_filename, self.export_format) | ||||
resources = self.init_single_notebook_resources(notebook_filename) | ||||
output, resources = self.export_single_notebook(notebook_filename, resources) | ||||
write_results = self.write_single_notebook(output, resources) | ||||
self.postprocess_single_notebook(write_results) | ||||
Jonathan Frederic
|
r11400 | def convert_notebooks(self): | ||
""" | ||||
Convert the notebooks in the self.notebook traitlet | ||||
""" | ||||
Jessica B. Hamrick
|
r20746 | # check that the output base isn't specified if there is more than | ||
# one notebook to convert | ||||
Matthias BUSSONNIER
|
r11801 | if self.output_base != '' and len(self.notebooks) > 1: | ||
MinRK
|
r11865 | self.log.error( | ||
Jessica B. Hamrick
|
r20746 | """ | ||
UsageError: --output flag or `NbConvertApp.output_base` config option | ||||
cannot be used when converting multiple notebooks. | ||||
""" | ||||
) | ||||
Matthias BUSSONNIER
|
r11801 | self.exit(1) | ||
MinRK
|
r11865 | |||
Jessica B. Hamrick
|
r20746 | # initialize the exporter | ||
self.exporter = exporter_map[self.export_format](config=self.config) | ||||
Matthias BUSSONNIER
|
r11801 | |||
Jessica B. Hamrick
|
r20746 | # no notebooks to convert! | ||
if len(self.notebooks) == 0: | ||||
Jonathan Frederic
|
r11666 | self.print_help() | ||
Jonathan Frederic
|
r11667 | sys.exit(-1) | ||
Jessica B. Hamrick
|
r20746 | |||
# convert each notebook | ||||
for notebook_filename in self.notebooks: | ||||
self.convert_single_notebook(notebook_filename) | ||||
damianavila
|
r11772 | |||
Brian E. Granger
|
r11087 | #----------------------------------------------------------------------------- | ||
Brian E. Granger
|
r11092 | # Main entry point | ||
Brian E. Granger
|
r11087 | #----------------------------------------------------------------------------- | ||
MinRK
|
r11176 | launch_new_instance = NbConvertApp.launch_instance | ||