##// END OF EJS Templates
Added warnings
Jonathan Frederic -
Show More
@@ -1,205 +1,211 b''
1 """
1 """
2 Module containing single call export functions.
2 Module containing single call export functions.
3 """
3 """
4 #-----------------------------------------------------------------------------
4 #-----------------------------------------------------------------------------
5 # Copyright (c) 2013, the IPython Development Team.
5 # Copyright (c) 2013, the IPython Development Team.
6 #
6 #
7 # Distributed under the terms of the Modified BSD License.
7 # Distributed under the terms of the Modified BSD License.
8 #
8 #
9 # The full license is in the file COPYING.txt, distributed with this software.
9 # The full license is in the file COPYING.txt, distributed with this software.
10 #-----------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11
11
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 from functools import wraps
16 from functools import wraps
17
17
18 from IPython.nbformat.v3.nbbase import NotebookNode
18 from IPython.nbformat.v3.nbbase import NotebookNode
19 from IPython.config import Config
19 from IPython.config import Config
20
20
21 from .exporter import Exporter
21 from .exporter import Exporter
22 from .html import HTMLExporter
22 from .html import HTMLExporter
23 from .slides import SlidesExporter
23 from .slides import SlidesExporter
24 from .latex import LatexExporter
24 from .latex import LatexExporter
25 from .markdown import MarkdownExporter
25 from .markdown import MarkdownExporter
26 from .python import PythonExporter
26 from .python import PythonExporter
27 from .rst import RSTExporter
27 from .rst import RSTExporter
28
28
29 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
30 # Classes
30 # Classes
31 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
32
32
33 def DocDecorator(f):
33 def DocDecorator(f):
34
34
35 #Set docstring of function
35 #Set docstring of function
36 f.__doc__ = f.__doc__ + """
36 f.__doc__ = f.__doc__ + """
37 nb : Notebook node
37 nb : Notebook node
38 config : config (optional, keyword arg)
38 config : config (optional, keyword arg)
39 User configuration instance.
39 User configuration instance.
40 resources : dict (optional, keyword arg)
40 resources : dict (optional, keyword arg)
41 Resources used in the conversion process.
41 Resources used in the conversion process.
42
42
43 Returns
43 Returns
44 ----------
44 ----------
45 tuple- output, resources, exporter_instance
45 tuple- output, resources, exporter_instance
46 output : str
46 output : str
47 Jinja 2 output. This is the resulting converted notebook.
47 Jinja 2 output. This is the resulting converted notebook.
48 resources : dictionary
48 resources : dictionary
49 Dictionary of resources used prior to and during the conversion
49 Dictionary of resources used prior to and during the conversion
50 process.
50 process.
51 exporter_instance : Exporter
51 exporter_instance : Exporter
52 Instance of the Exporter class used to export the document. Useful
52 Instance of the Exporter class used to export the document. Useful
53 to caller because it provides a 'file_extension' property which
53 to caller because it provides a 'file_extension' property which
54 specifies what extension the output should be saved as."""
54 specifies what extension the output should be saved as.
55
56 WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT
57 """
55
58
56 @wraps(f)
59 @wraps(f)
57 def decorator(*args, **kwargs):
60 def decorator(*args, **kwargs):
58 return f(*args, **kwargs)
61 return f(*args, **kwargs)
59
62
60 return decorator
63 return decorator
61
64
62
65
63 #-----------------------------------------------------------------------------
66 #-----------------------------------------------------------------------------
64 # Functions
67 # Functions
65 #-----------------------------------------------------------------------------
68 #-----------------------------------------------------------------------------
66
69
67 __all__ = [
70 __all__ = [
68 'export',
71 'export',
69 'export_html',
72 'export_html',
70 'export_custom',
73 'export_custom',
71 'export_slides',
74 'export_slides',
72 'export_latex',
75 'export_latex',
73 'export_markdown',
76 'export_markdown',
74 'export_python',
77 'export_python',
75 'export_rst',
78 'export_rst',
76 'export_by_name',
79 'export_by_name',
77 'get_export_names',
80 'get_export_names',
78 'ExporterNameError'
81 'ExporterNameError'
79 ]
82 ]
80
83
81
84
82 class ExporterNameError(NameError):
85 class ExporterNameError(NameError):
83 pass
86 pass
84
87
85
88
86 @DocDecorator
89 @DocDecorator
87 def export(exporter, nb, **kw):
90 def export(exporter, nb, **kw):
88 """
91 """
89 Export a notebook object using specific exporter class.
92 Export a notebook object using specific exporter class.
90
93
91 exporter : Exporter class type or instance
94 exporter : Exporter class type or instance
92 Class type or instance of the exporter that should be used. If the
95 Class type or instance of the exporter that should be used. If the
93 method initializes it's own instance of the class, it is ASSUMED that
96 method initializes it's own instance of the class, it is ASSUMED that
94 the class type provided exposes a constructor (__init__) with the same
97 the class type provided exposes a constructor (__init__) with the same
95 signature as the base Exporter class.
98 signature as the base Exporter class.
96 """
99 """
97
100
98 #Check arguments
101 #Check arguments
99 if exporter is None:
102 if exporter is None:
100 raise TypeError("Exporter is None")
103 raise TypeError("Exporter is None")
101 elif not isinstance(exporter, Exporter) and not issubclass(exporter, Exporter):
104 elif not isinstance(exporter, Exporter) and not issubclass(exporter, Exporter):
102 raise TypeError("exporter does not inherit from Exporter (base)")
105 raise TypeError("exporter does not inherit from Exporter (base)")
103 if nb is None:
106 if nb is None:
104 raise TypeError("nb is None")
107 raise TypeError("nb is None")
105
108
106 #Create the exporter
109 #Create the exporter
107 resources = kw.pop('resources', None)
110 resources = kw.pop('resources', None)
108 if isinstance(exporter, Exporter):
111 if isinstance(exporter, Exporter):
109 exporter_instance = exporter
112 exporter_instance = exporter
110 else:
113 else:
111 exporter_instance = exporter(**kw)
114 exporter_instance = exporter(**kw)
112
115
113 #Try to convert the notebook using the appropriate conversion function.
116 #Try to convert the notebook using the appropriate conversion function.
114 if isinstance(nb, NotebookNode):
117 if isinstance(nb, NotebookNode):
115 output, resources = exporter_instance.from_notebook_node(nb, resources)
118 output, resources = exporter_instance.from_notebook_node(nb, resources)
116 elif isinstance(nb, basestring):
119 elif isinstance(nb, basestring):
117 output, resources = exporter_instance.from_filename(nb, resources)
120 output, resources = exporter_instance.from_filename(nb, resources)
118 else:
121 else:
119 output, resources = exporter_instance.from_file(nb, resources)
122 output, resources = exporter_instance.from_file(nb, resources)
120 return output, resources
123 return output, resources
121
124
122
125
123 @DocDecorator
126 @DocDecorator
124 def export_custom(nb, **kw):
127 def export_custom(nb, **kw):
125 """
128 """
126 Export a notebook object to a custom format
129 Export a notebook object to a custom format
127 """
130 """
128 return export(Exporter, nb, **kw)
131 return export(Exporter, nb, **kw)
129
132
130
133
131 @DocDecorator
134 @DocDecorator
132 def export_html(nb, **kw):
135 def export_html(nb, **kw):
133 """
136 """
134 Export a notebook object to HTML
137 Export a notebook object to HTML
135 """
138 """
136 return export(HTMLExporter, nb, **kw)
139 return export(HTMLExporter, nb, **kw)
137
140
138
141
139 @DocDecorator
142 @DocDecorator
140 def export_slides(nb, **kw):
143 def export_slides(nb, **kw):
141 """
144 """
142 Export a notebook object to Slides
145 Export a notebook object to Slides
143 """
146 """
144 return export(SlidesExporter, nb, **kw)
147 return export(SlidesExporter, nb, **kw)
145
148
146
149
147 @DocDecorator
150 @DocDecorator
148 def export_latex(nb, **kw):
151 def export_latex(nb, **kw):
149 """
152 """
150 Export a notebook object to LaTeX
153 Export a notebook object to LaTeX
151 """
154 """
152 return export(LatexExporter, nb, **kw)
155 return export(LatexExporter, nb, **kw)
153
156
154
157
155 @DocDecorator
158 @DocDecorator
156 def export_markdown(nb, **kw):
159 def export_markdown(nb, **kw):
157 """
160 """
158 Export a notebook object to Markdown
161 Export a notebook object to Markdown
159 """
162 """
160 return export(MarkdownExporter, nb, **kw)
163 return export(MarkdownExporter, nb, **kw)
161
164
162
165
163 @DocDecorator
166 @DocDecorator
164 def export_python(nb, **kw):
167 def export_python(nb, **kw):
165 """
168 """
166 Export a notebook object to Python
169 Export a notebook object to Python
167 """
170 """
168 return export(PythonExporter, nb, **kw)
171 return export(PythonExporter, nb, **kw)
169
172
170
173
171 @DocDecorator
174 @DocDecorator
172 def export_rst(nb, **kw):
175 def export_rst(nb, **kw):
173 """
176 """
174 Export a notebook object to reStructuredText
177 Export a notebook object to reStructuredText
175 """
178 """
176 return export(RSTExporter, nb, **kw)
179 return export(RSTExporter, nb, **kw)
177
180
178
181
179 @DocDecorator
182 @DocDecorator
180 def export_by_name(format_name, nb, **kw):
183 def export_by_name(format_name, nb, **kw):
181 """
184 """
182 Export a notebook object to a template type by its name. Reflection
185 Export a notebook object to a template type by its name. Reflection
183 (Inspect) is used to find the template's corresponding explicit export
186 (Inspect) is used to find the template's corresponding explicit export
184 method defined in this module. That method is then called directly.
187 method defined in this module. That method is then called directly.
185
188
186 format_name : str
189 format_name : str
187 Name of the template style to export to.
190 Name of the template style to export to.
188 """
191 """
189
192
190 function_name = "export_" + format_name.lower()
193 function_name = "export_" + format_name.lower()
191
194
192 if function_name in globals():
195 if function_name in globals():
193 return globals()[function_name](nb, **kw)
196 return globals()[function_name](nb, **kw)
194 else:
197 else:
195 raise ExporterNameError("template for `%s` not found" % function_name)
198 raise ExporterNameError("template for `%s` not found" % function_name)
196
199
197
200
198 def get_export_names():
201 def get_export_names():
199 "Return a list of the currently supported export targets"
202 """Return a list of the currently supported export targets
203
204 WARNING: API WILL CHANGE IN FUTURE RELEASES OF NBCONVERT"""
205
200 # grab everything after 'export_'
206 # grab everything after 'export_'
201 l = [x[len('export_'):] for x in __all__ if x.startswith('export_')]
207 l = [x[len('export_'):] for x in __all__ if x.startswith('export_')]
202
208
203 # filter out the one method that is not a template
209 # filter out the one method that is not a template
204 l = [x for x in l if 'by_name' not in x]
210 l = [x for x in l if 'by_name' not in x]
205 return sorted(l)
211 return sorted(l)
@@ -1,300 +1,302 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 """NBConvert is a utility for conversion of .ipynb files.
2 """NBConvert is a utility for conversion of .ipynb files.
3
3
4 Command-line interface for the NbConvert conversion utility.
4 Command-line interface for the NbConvert conversion utility.
5 """
5 """
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 #Copyright (c) 2013, the IPython Development Team.
7 #Copyright (c) 2013, the IPython Development Team.
8 #
8 #
9 #Distributed under the terms of the Modified BSD License.
9 #Distributed under the terms of the Modified BSD License.
10 #
10 #
11 #The full license is in the file COPYING.txt, distributed with this software.
11 #The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 #Imports
15 #Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 # Stdlib imports
18 # Stdlib imports
19 from __future__ import print_function
19 from __future__ import print_function
20 import sys
20 import sys
21 import os
21 import os
22 import glob
22 import glob
23
23
24 # From IPython
24 # From IPython
25 from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags
25 from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags
26 from IPython.config import catch_config_error, Configurable
26 from IPython.config import catch_config_error, Configurable
27 from IPython.utils.traitlets import (
27 from IPython.utils.traitlets import (
28 Unicode, List, Instance, DottedObjectName, Type, CaselessStrEnum,
28 Unicode, List, Instance, DottedObjectName, Type, CaselessStrEnum,
29 )
29 )
30 from IPython.utils.importstring import import_item
30 from IPython.utils.importstring import import_item
31
31
32 from .exporters.export import export_by_name, get_export_names, ExporterNameError
32 from .exporters.export import export_by_name, get_export_names, ExporterNameError
33 from IPython.nbconvert import exporters, transformers, writers, post_processors
33 from IPython.nbconvert import exporters, transformers, writers, post_processors
34 from .utils.base import NbConvertBase
34 from .utils.base import NbConvertBase
35 from .utils.exceptions import ConversionException
35 from .utils.exceptions import ConversionException
36
36
37 #-----------------------------------------------------------------------------
37 #-----------------------------------------------------------------------------
38 #Classes and functions
38 #Classes and functions
39 #-----------------------------------------------------------------------------
39 #-----------------------------------------------------------------------------
40
40
41 class DottedOrNone(DottedObjectName):
41 class DottedOrNone(DottedObjectName):
42 """
42 """
43 A string holding a valid dotted object name in Python, such as A.b3._c
43 A string holding a valid dotted object name in Python, such as A.b3._c
44 Also allows for None type."""
44 Also allows for None type."""
45
45
46 default_value = u''
46 default_value = u''
47
47
48 def validate(self, obj, value):
48 def validate(self, obj, value):
49 if value is not None and len(value) > 0:
49 if value is not None and len(value) > 0:
50 return super(DottedOrNone, self).validate(obj, value)
50 return super(DottedOrNone, self).validate(obj, value)
51 else:
51 else:
52 return value
52 return value
53
53
54 nbconvert_aliases = {}
54 nbconvert_aliases = {}
55 nbconvert_aliases.update(base_aliases)
55 nbconvert_aliases.update(base_aliases)
56 nbconvert_aliases.update({
56 nbconvert_aliases.update({
57 'to' : 'NbConvertApp.export_format',
57 'to' : 'NbConvertApp.export_format',
58 'template' : 'Exporter.template_file',
58 'template' : 'Exporter.template_file',
59 'notebooks' : 'NbConvertApp.notebooks',
59 'notebooks' : 'NbConvertApp.notebooks',
60 'writer' : 'NbConvertApp.writer_class',
60 'writer' : 'NbConvertApp.writer_class',
61 'post': 'NbConvertApp.post_processor_class'
61 'post': 'NbConvertApp.post_processor_class'
62 })
62 })
63
63
64 nbconvert_flags = {}
64 nbconvert_flags = {}
65 nbconvert_flags.update(base_flags)
65 nbconvert_flags.update(base_flags)
66 nbconvert_flags.update({
66 nbconvert_flags.update({
67 'stdout' : (
67 'stdout' : (
68 {'NbConvertApp' : {'writer_class' : "StdoutWriter"}},
68 {'NbConvertApp' : {'writer_class' : "StdoutWriter"}},
69 "Write notebook output to stdout instead of files."
69 "Write notebook output to stdout instead of files."
70 )
70 )
71 })
71 })
72
72
73
73
74 class NbConvertApp(BaseIPythonApplication):
74 class NbConvertApp(BaseIPythonApplication):
75 """Application used to convert to and from notebook file type (*.ipynb)"""
75 """Application used to convert to and from notebook file type (*.ipynb)"""
76
76
77 name = 'ipython-nbconvert'
77 name = 'ipython-nbconvert'
78 aliases = nbconvert_aliases
78 aliases = nbconvert_aliases
79 flags = nbconvert_flags
79 flags = nbconvert_flags
80
80
81 def _classes_default(self):
81 def _classes_default(self):
82 classes = [NbConvertBase]
82 classes = [NbConvertBase]
83 for pkg in (exporters, transformers, writers):
83 for pkg in (exporters, transformers, writers):
84 for name in dir(pkg):
84 for name in dir(pkg):
85 cls = getattr(pkg, name)
85 cls = getattr(pkg, name)
86 if isinstance(cls, type) and issubclass(cls, Configurable):
86 if isinstance(cls, type) and issubclass(cls, Configurable):
87 classes.append(cls)
87 classes.append(cls)
88 return classes
88 return classes
89
89
90 description = Unicode(
90 description = Unicode(
91 u"""This application is used to convert notebook files (*.ipynb)
91 u"""This application is used to convert notebook files (*.ipynb)
92 to various other formats.""")
92 to various other formats.
93
94 WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.""")
93
95
94 examples = Unicode(u"""
96 examples = Unicode(u"""
95 The simplest way to use nbconvert is
97 The simplest way to use nbconvert is
96
98
97 > ipython nbconvert mynotebook.ipynb
99 > ipython nbconvert mynotebook.ipynb
98
100
99 which will convert mynotebook.ipynb to the default format (probably HTML).
101 which will convert mynotebook.ipynb to the default format (probably HTML).
100
102
101 You can specify the export format with `--to`.
103 You can specify the export format with `--to`.
102 Options include {0}
104 Options include {0}
103
105
104 > ipython nbconvert --to latex mynotebook.ipnynb
106 > ipython nbconvert --to latex mynotebook.ipnynb
105
107
106 Both HTML and LaTeX support multiple output templates. LaTeX includes
108 Both HTML and LaTeX support multiple output templates. LaTeX includes
107 'basic', 'book', and 'article'. HTML includes 'basic' and 'full'. You
109 'basic', 'book', and 'article'. HTML includes 'basic' and 'full'. You
108 can specify the flavor of the format used.
110 can specify the flavor of the format used.
109
111
110 > ipython nbconvert --to html --template reveal mynotebook.ipnynb
112 > ipython nbconvert --to html --template reveal mynotebook.ipnynb
111
113
112 You can also pipe the output to stdout, rather than a file
114 You can also pipe the output to stdout, rather than a file
113
115
114 > ipython nbconvert mynotebook.ipynb --stdout
116 > ipython nbconvert mynotebook.ipynb --stdout
115
117
116 A post-processor can be used to compile a PDF
118 A post-processor can be used to compile a PDF
117
119
118 > ipython nbconvert mynotebook.ipynb --to latex --post PDF
120 > ipython nbconvert mynotebook.ipynb --to latex --post PDF
119
121
120 Multiple notebooks can be given at the command line in a couple of
122 Multiple notebooks can be given at the command line in a couple of
121 different ways:
123 different ways:
122
124
123 > ipython nbconvert notebook*.ipynb
125 > ipython nbconvert notebook*.ipynb
124 > ipython nbconvert notebook1.ipynb notebook2.ipynb
126 > ipython nbconvert notebook1.ipynb notebook2.ipynb
125
127
126 or you can specify the notebooks list in a config file, containing::
128 or you can specify the notebooks list in a config file, containing::
127
129
128 c.NbConvertApp.notebooks = ["my_notebook.ipynb"]
130 c.NbConvertApp.notebooks = ["my_notebook.ipynb"]
129
131
130 > ipython nbconvert --config mycfg.py
132 > ipython nbconvert --config mycfg.py
131 """.format(get_export_names()))
133 """.format(get_export_names()))
132
134
133 # Writer specific variables
135 # Writer specific variables
134 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
136 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
135 help="""Instance of the writer class used to write the
137 help="""Instance of the writer class used to write the
136 results of the conversion.""")
138 results of the conversion.""")
137 writer_class = DottedObjectName('FilesWriter', config=True,
139 writer_class = DottedObjectName('FilesWriter', config=True,
138 help="""Writer class used to write the
140 help="""Writer class used to write the
139 results of the conversion""")
141 results of the conversion""")
140 writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter',
142 writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter',
141 'PDFWriter': 'IPython.nbconvert.writers.pdf.PDFWriter',
143 'PDFWriter': 'IPython.nbconvert.writers.pdf.PDFWriter',
142 'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter',
144 'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter',
143 'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'}
145 'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'}
144 writer_factory = Type()
146 writer_factory = Type()
145
147
146 def _writer_class_changed(self, name, old, new):
148 def _writer_class_changed(self, name, old, new):
147 if new in self.writer_aliases:
149 if new in self.writer_aliases:
148 new = self.writer_aliases[new]
150 new = self.writer_aliases[new]
149 self.writer_factory = import_item(new)
151 self.writer_factory = import_item(new)
150
152
151 # Post-processor specific variables
153 # Post-processor specific variables
152 post_processor = Instance('IPython.nbconvert.post_processors.base.PostProcessorBase',
154 post_processor = Instance('IPython.nbconvert.post_processors.base.PostProcessorBase',
153 help="""Instance of the PostProcessor class used to write the
155 help="""Instance of the PostProcessor class used to write the
154 results of the conversion.""")
156 results of the conversion.""")
155
157
156 post_processor_class = DottedOrNone(config=True,
158 post_processor_class = DottedOrNone(config=True,
157 help="""PostProcessor class used to write the
159 help="""PostProcessor class used to write the
158 results of the conversion""")
160 results of the conversion""")
159 post_processor_aliases = {'PDF': 'IPython.nbconvert.post_processors.pdf.PDFPostProcessor'}
161 post_processor_aliases = {'PDF': 'IPython.nbconvert.post_processors.pdf.PDFPostProcessor'}
160 post_processor_factory = Type()
162 post_processor_factory = Type()
161
163
162 def _post_processor_class_changed(self, name, old, new):
164 def _post_processor_class_changed(self, name, old, new):
163 if new in self.post_processor_aliases:
165 if new in self.post_processor_aliases:
164 new = self.post_processor_aliases[new]
166 new = self.post_processor_aliases[new]
165 if new:
167 if new:
166 self.post_processor_factory = import_item(new)
168 self.post_processor_factory = import_item(new)
167
169
168
170
169 # Other configurable variables
171 # Other configurable variables
170 export_format = CaselessStrEnum(get_export_names(),
172 export_format = CaselessStrEnum(get_export_names(),
171 default_value="html",
173 default_value="html",
172 config=True,
174 config=True,
173 help="""The export format to be used."""
175 help="""The export format to be used."""
174 )
176 )
175
177
176 notebooks = List([], config=True, help="""List of notebooks to convert.
178 notebooks = List([], config=True, help="""List of notebooks to convert.
177 Wildcards are supported.
179 Wildcards are supported.
178 Filenames passed positionally will be added to the list.
180 Filenames passed positionally will be added to the list.
179 """)
181 """)
180
182
181 @catch_config_error
183 @catch_config_error
182 def initialize(self, argv=None):
184 def initialize(self, argv=None):
183 super(NbConvertApp, self).initialize(argv)
185 super(NbConvertApp, self).initialize(argv)
184 self.init_syspath()
186 self.init_syspath()
185 self.init_notebooks()
187 self.init_notebooks()
186 self.init_writer()
188 self.init_writer()
187 self.init_post_processor()
189 self.init_post_processor()
188
190
189
191
190
192
191 def init_syspath(self):
193 def init_syspath(self):
192 """
194 """
193 Add the cwd to the sys.path ($PYTHONPATH)
195 Add the cwd to the sys.path ($PYTHONPATH)
194 """
196 """
195 sys.path.insert(0, os.getcwd())
197 sys.path.insert(0, os.getcwd())
196
198
197
199
198 def init_notebooks(self):
200 def init_notebooks(self):
199 """Construct the list of notebooks.
201 """Construct the list of notebooks.
200 If notebooks are passed on the command-line,
202 If notebooks are passed on the command-line,
201 they override notebooks specified in config files.
203 they override notebooks specified in config files.
202 Glob each notebook to replace notebook patterns with filenames.
204 Glob each notebook to replace notebook patterns with filenames.
203 """
205 """
204
206
205 # Specifying notebooks on the command-line overrides (rather than adds)
207 # Specifying notebooks on the command-line overrides (rather than adds)
206 # the notebook list
208 # the notebook list
207 if self.extra_args:
209 if self.extra_args:
208 patterns = self.extra_args
210 patterns = self.extra_args
209 else:
211 else:
210 patterns = self.notebooks
212 patterns = self.notebooks
211
213
212 # Use glob to replace all the notebook patterns with filenames.
214 # Use glob to replace all the notebook patterns with filenames.
213 filenames = []
215 filenames = []
214 for pattern in patterns:
216 for pattern in patterns:
215
217
216 # Use glob to find matching filenames. Allow the user to convert
218 # Use glob to find matching filenames. Allow the user to convert
217 # notebooks without having to type the extension.
219 # notebooks without having to type the extension.
218 globbed_files = glob.glob(pattern)
220 globbed_files = glob.glob(pattern)
219 globbed_files.extend(glob.glob(pattern + '.ipynb'))
221 globbed_files.extend(glob.glob(pattern + '.ipynb'))
220
222
221 for filename in globbed_files:
223 for filename in globbed_files:
222 if not filename in filenames:
224 if not filename in filenames:
223 filenames.append(filename)
225 filenames.append(filename)
224 self.notebooks = filenames
226 self.notebooks = filenames
225
227
226 def init_writer(self):
228 def init_writer(self):
227 """
229 """
228 Initialize the writer (which is stateless)
230 Initialize the writer (which is stateless)
229 """
231 """
230 self._writer_class_changed(None, self.writer_class, self.writer_class)
232 self._writer_class_changed(None, self.writer_class, self.writer_class)
231 self.writer = self.writer_factory(parent=self)
233 self.writer = self.writer_factory(parent=self)
232
234
233 def init_post_processor(self):
235 def init_post_processor(self):
234 """
236 """
235 Initialize the post_processor (which is stateless)
237 Initialize the post_processor (which is stateless)
236 """
238 """
237 self._post_processor_class_changed(None, self.post_processor_class,
239 self._post_processor_class_changed(None, self.post_processor_class,
238 self.post_processor_class)
240 self.post_processor_class)
239 if self.post_processor_factory:
241 if self.post_processor_factory:
240 self.post_processor = self.post_processor_factory(parent=self)
242 self.post_processor = self.post_processor_factory(parent=self)
241
243
242 def start(self):
244 def start(self):
243 """
245 """
244 Ran after initialization completed
246 Ran after initialization completed
245 """
247 """
246 super(NbConvertApp, self).start()
248 super(NbConvertApp, self).start()
247 self.convert_notebooks()
249 self.convert_notebooks()
248
250
249 def convert_notebooks(self):
251 def convert_notebooks(self):
250 """
252 """
251 Convert the notebooks in the self.notebook traitlet
253 Convert the notebooks in the self.notebook traitlet
252 """
254 """
253 # Export each notebook
255 # Export each notebook
254 conversion_success = 0
256 conversion_success = 0
255 for notebook_filename in self.notebooks:
257 for notebook_filename in self.notebooks:
256
258
257 # Get a unique key for the notebook and set it in the resources object.
259 # Get a unique key for the notebook and set it in the resources object.
258 basename = os.path.basename(notebook_filename)
260 basename = os.path.basename(notebook_filename)
259 notebook_name = basename[:basename.rfind('.')]
261 notebook_name = basename[:basename.rfind('.')]
260 resources = {}
262 resources = {}
261 resources['unique_key'] = notebook_name
263 resources['unique_key'] = notebook_name
262 resources['output_files_dir'] = '%s_files' % notebook_name
264 resources['output_files_dir'] = '%s_files' % notebook_name
263
265
264 # Try to export
266 # Try to export
265 try:
267 try:
266 output, resources = export_by_name(self.export_format,
268 output, resources = export_by_name(self.export_format,
267 notebook_filename,
269 notebook_filename,
268 resources=resources,
270 resources=resources,
269 config=self.config)
271 config=self.config)
270 except ExporterNameError as e:
272 except ExporterNameError as e:
271 print("Error while converting '%s': '%s' exporter not found."
273 print("Error while converting '%s': '%s' exporter not found."
272 %(notebook_filename, self.export_format),
274 %(notebook_filename, self.export_format),
273 file=sys.stderr)
275 file=sys.stderr)
274 print("Known exporters are:",
276 print("Known exporters are:",
275 "\n\t" + "\n\t".join(get_export_names()),
277 "\n\t" + "\n\t".join(get_export_names()),
276 file=sys.stderr)
278 file=sys.stderr)
277 self.exit(1)
279 self.exit(1)
278 except ConversionException as e:
280 except ConversionException as e:
279 print("Error while converting '%s': %s" %(notebook_filename, e),
281 print("Error while converting '%s': %s" %(notebook_filename, e),
280 file=sys.stderr)
282 file=sys.stderr)
281 self.exit(1)
283 self.exit(1)
282 else:
284 else:
283 write_resultes = self.writer.write(output, resources, notebook_name=notebook_name)
285 write_resultes = self.writer.write(output, resources, notebook_name=notebook_name)
284
286
285 #Post-process if post processor has been defined.
287 #Post-process if post processor has been defined.
286 if hasattr(self, 'post_processor') and self.post_processor:
288 if hasattr(self, 'post_processor') and self.post_processor:
287 self.post_processor(write_resultes)
289 self.post_processor(write_resultes)
288 conversion_success += 1
290 conversion_success += 1
289
291
290 # If nothing was converted successfully, help the user.
292 # If nothing was converted successfully, help the user.
291 if conversion_success == 0:
293 if conversion_success == 0:
292 self.print_help()
294 self.print_help()
293 sys.exit(-1)
295 sys.exit(-1)
294
296
295
297
296 #-----------------------------------------------------------------------------
298 #-----------------------------------------------------------------------------
297 # Main entry point
299 # Main entry point
298 #-----------------------------------------------------------------------------
300 #-----------------------------------------------------------------------------
299
301
300 launch_new_instance = NbConvertApp.launch_instance
302 launch_new_instance = NbConvertApp.launch_instance
General Comments 0
You need to be logged in to leave comments. Login now