nbconvertapp.py
180 lines
| 6.6 KiB
| text/x-python
|
PythonLexer
Brian E. Granger
|
r11087 | #!/usr/bin/env python | ||
"""NBConvert is a utility for conversion of IPYNB files. | ||||
Commandline interface for the NBConvert conversion utility. Read the | ||||
readme.rst for usage information | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
#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 | ||||
#----------------------------------------------------------------------------- | ||||
#Stdlib imports | ||||
from __future__ import print_function | ||||
import sys | ||||
import os | ||||
Jonathan Frederic
|
r11367 | import glob | ||
Brian E. Granger
|
r11087 | |||
#From IPython | ||||
Jonathan Frederic
|
r11367 | from IPython.core.application import BaseIPythonApplication | ||
from IPython.config.application import catch_config_error | ||||
from IPython.utils.traitlets import Unicode, List, Instance, DottedObjectName, Type | ||||
from IPython.utils.importstring import import_item | ||||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11372 | from .exporters.export import export_by_name, get_export_names, ExporterNameError | ||
Brian E. Granger
|
r11090 | from .exporters.exporter import Exporter | ||
Jonathan Frederic
|
r11367 | from .writers.base import WriterBase | ||
Brian E. Granger
|
r11090 | from .utils.config import GlobalConfigurable | ||
Brian E. Granger
|
r11087 | |||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r11367 | #Classes and functions | ||
Brian E. Granger
|
r11087 | #----------------------------------------------------------------------------- | ||
Jonathan Frederic
|
r11367 | class NbConvertApp(BaseIPythonApplication): | ||
"""Application used to convert to and from notebook file type (*.ipynb)""" | ||||
Brian E. Granger
|
r11087 | |||
Paul Ivanov
|
r11248 | |||
Jonathan Frederic
|
r11367 | description = Unicode( | ||
u"""This application is used to convert notebook files (*.ipynb). | ||||
An ipython config file can be used to batch convert notebooks in the | ||||
current directory.""") | ||||
Paul Ivanov
|
r11251 | |||
Jonathan Frederic
|
r11367 | examples = Unicode(u""" | ||
Running `ipython nbconvert` will read the directory config file and then | ||||
apply it to one or more notebooks. | ||||
Brian E. Granger
|
r11087 | |||
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 | ||||
> ipython nbconvert # this will use the config file to fill in the notebooks | ||||
""") | ||||
Paul Ivanov
|
r11251 | |||
Jonathan Frederic
|
r11367 | config_file_name = Unicode(u'ipython_nbconvert_config.py') | ||
Paul Ivanov
|
r11251 | |||
Jonathan Frederic
|
r11367 | #Writer specific variables | ||
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
|
r11367 | #Other configurable variables | ||
export_format = Unicode( | ||||
"", config=True, | ||||
help="""If specified, nbconvert will convert the document(s) specified | ||||
using this format.""") | ||||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11367 | notebooks = List([], config=True, help="""List of notebooks to convert. | ||
Search patterns are supported.""") | ||||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11367 | aliases = {'format':'NbConvertApp.export_format', | ||
'notebooks':'NbConvertApp.notebooks', | ||||
'writer':'NbConvertApp.writer_class'} | ||||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11367 | @catch_config_error | ||
def initialize(self, argv=None): | ||||
super(NbConvertApp, self).initialize(argv) | ||||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11367 | #Register class here to have help with help all | ||
self.classes.insert(0, Exporter) | ||||
self.classes.insert(0, WriterBase) | ||||
self.classes.insert(0, GlobalConfigurable) | ||||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11367 | #Init | ||
self.init_config(self.extra_args) | ||||
self.init_writer() | ||||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11367 | def init_config(self, extra_args): | ||
""" | ||||
Add notebooks to the config if needed. Glob each notebook to replace | ||||
notebook patterns with filenames. | ||||
""" | ||||
Brian E. Granger
|
r11087 | |||
Jonathan Frederic
|
r11367 | #Get any additional notebook patterns from the commandline | ||
if len(extra_args) > 0: | ||||
for pattern in extra_args: | ||||
self.notebooks.append(pattern) | ||||
#Use glob to replace all the notebook patterns with filenames. | ||||
filenames = [] | ||||
for pattern in self.notebooks: | ||||
for filename in glob.glob(pattern): | ||||
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 | |||
def start(self, argv=None): | ||||
""" | ||||
Jonathan Frederic
|
r11367 | Entrypoint of NbConvert application. | ||
""" | ||||
Brian E. Granger
|
r11087 | |||
#Call base | ||||
super(NbConvertApp, self).start() | ||||
Jonathan Frederic
|
r11367 | #Export each notebook | ||
Jonathan Frederic
|
r11379 | #TODO: Empty check | ||
Jonathan Frederic
|
r11367 | for notebook_filename in self.notebooks: | ||
#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('.')] | ||||
resources = {} | ||||
resources['unique_key'] = notebook_name | ||||
Jonathan Frederic
|
r11368 | #Try to export | ||
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
|
r11379 | except Exception as e: | ||
print("Error: could no export '%s'" % notebook_filename, file=sys.stderr) | ||||
print(e, file=sys.stderr) | ||||
Jonathan Frederic
|
r11368 | |||
#Write | ||||
Jonathan Frederic
|
r11367 | self.writer.write(output, resources, notebook_name=notebook_name) | ||
Brian E. Granger
|
r11087 | |||
#----------------------------------------------------------------------------- | ||||
Brian E. Granger
|
r11092 | # Main entry point | ||
Brian E. Granger
|
r11087 | #----------------------------------------------------------------------------- | ||
MinRK
|
r11176 | launch_new_instance = NbConvertApp.launch_instance | ||