##// END OF EJS Templates
Moved PDF logic into Post-Processor class
Jonathan Frederic -
Show More
@@ -0,0 +1,2 b''
1 from .base import PostProcessorBase
2 from .pdf import PDFPostProcessor
@@ -0,0 +1,36 b''
1 #!/usr/bin/env python
2 """
3 Basic post processor
4 """
5 #-----------------------------------------------------------------------------
6 #Copyright (c) 2013, the IPython Development Team.
7 #
8 #Distributed under the terms of the Modified BSD License.
9 #
10 #The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 from ..utils.base import NbConvertBase
18
19
20 #-----------------------------------------------------------------------------
21 # Classes
22 #-----------------------------------------------------------------------------
23 class PostProcessorBase(NbConvertBase):
24
25 def __call__(self, input):
26 """
27 See def call() ...
28 """
29 self.call(input)
30
31
32 def call(self, input):
33 """
34 Post-process output from a writer.
35 """
36 raise NotImplementedError('call')
@@ -1,6 +1,7 b''
1 """Utilities for converting notebooks to and from different formats."""
1 """Utilities for converting notebooks to and from different formats."""
2
2
3 from .exporters import *
3 from .exporters import *
4 import filters
4 import filters
5 import transformers
5 import transformers
6 import post_processors
6 import writers
7 import writers
@@ -1,258 +1,300 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
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):
42 """
43 A string holding a valid dotted object name in Python, such as A.b3._c
44 Also allows for None type."""
45
46 default_value = u''
47
48 def validate(self, obj, value):
49 if value is not None and len(value) > 0:
50 return super(DottedOrNone, self).validate(obj, value)
51 else:
52 return value
53
41 nbconvert_aliases = {}
54 nbconvert_aliases = {}
42 nbconvert_aliases.update(base_aliases)
55 nbconvert_aliases.update(base_aliases)
43 nbconvert_aliases.update({
56 nbconvert_aliases.update({
44 'to' : 'NbConvertApp.export_format',
57 'to' : 'NbConvertApp.export_format',
45 'template' : 'Exporter.template_file',
58 'template' : 'Exporter.template_file',
46 'notebooks' : 'NbConvertApp.notebooks',
59 'notebooks' : 'NbConvertApp.notebooks',
47 'writer' : 'NbConvertApp.writer_class',
60 'writer' : 'NbConvertApp.writer_class',
61 'post': 'NbConvertApp.post_processor_class'
48 })
62 })
49
63
50 nbconvert_flags = {}
64 nbconvert_flags = {}
51 nbconvert_flags.update(base_flags)
65 nbconvert_flags.update(base_flags)
52 nbconvert_flags.update({
66 nbconvert_flags.update({
53 'stdout' : (
67 'stdout' : (
54 {'NbConvertApp' : {'writer_class' : "StdoutWriter"}},
68 {'NbConvertApp' : {'writer_class' : "StdoutWriter"}},
55 "Write notebook output to stdout instead of files."
69 "Write notebook output to stdout instead of files."
56 ),
57
58 'pdf' : (
59 {'NbConvertApp' : {'writer_class' : "PDFWriter"}},
60 "Compile notebook output to a PDF (requires `--to latex`)."
61 )
70 )
62 })
71 })
63
72
64
73
65 class NbConvertApp(BaseIPythonApplication):
74 class NbConvertApp(BaseIPythonApplication):
66 """Application used to convert to and from notebook file type (*.ipynb)"""
75 """Application used to convert to and from notebook file type (*.ipynb)"""
67
76
68 name = 'ipython-nbconvert'
77 name = 'ipython-nbconvert'
69 aliases = nbconvert_aliases
78 aliases = nbconvert_aliases
70 flags = nbconvert_flags
79 flags = nbconvert_flags
71
80
72 def _classes_default(self):
81 def _classes_default(self):
73 classes = [NbConvertBase]
82 classes = [NbConvertBase]
74 for pkg in (exporters, transformers, writers):
83 for pkg in (exporters, transformers, writers):
75 for name in dir(pkg):
84 for name in dir(pkg):
76 cls = getattr(pkg, name)
85 cls = getattr(pkg, name)
77 if isinstance(cls, type) and issubclass(cls, Configurable):
86 if isinstance(cls, type) and issubclass(cls, Configurable):
78 classes.append(cls)
87 classes.append(cls)
79 return classes
88 return classes
80
89
81 description = Unicode(
90 description = Unicode(
82 u"""This application is used to convert notebook files (*.ipynb)
91 u"""This application is used to convert notebook files (*.ipynb)
83 to various other formats.""")
92 to various other formats.""")
84
93
85 examples = Unicode(u"""
94 examples = Unicode(u"""
86 The simplest way to use nbconvert is
95 The simplest way to use nbconvert is
87
96
88 > ipython nbconvert mynotebook.ipynb
97 > ipython nbconvert mynotebook.ipynb
89
98
90 which will convert mynotebook.ipynb to the default format (probably HTML).
99 which will convert mynotebook.ipynb to the default format (probably HTML).
91
100
92 You can specify the export format with `--to`.
101 You can specify the export format with `--to`.
93 Options include {0}
102 Options include {0}
94
103
95 > ipython nbconvert --to latex mynotebook.ipnynb
104 > ipython nbconvert --to latex mynotebook.ipnynb
96
105
97 Both HTML and LaTeX support multiple output templates. LaTeX includes
106 Both HTML and LaTeX support multiple output templates. LaTeX includes
98 'basic', 'book', and 'article'. HTML includes 'basic' and 'full'. You
107 'basic', 'book', and 'article'. HTML includes 'basic' and 'full'. You
99 can specify the flavor of the format used.
108 can specify the flavor of the format used.
100
109
101 > ipython nbconvert --to html --template reveal mynotebook.ipnynb
110 > ipython nbconvert --to html --template reveal mynotebook.ipnynb
102
111
103 You can also pipe the output to stdout, rather than a file
112 You can also pipe the output to stdout, rather than a file
104
113
105 > ipython nbconvert mynotebook.ipynb --stdout
114 > ipython nbconvert mynotebook.ipynb --stdout
106
115
107 or to a PDF
116 or to a PDF
108
117
109 > ipython nbconvert mynotebook.ipynb --to latex --pdf
118 > ipython nbconvert mynotebook.ipynb --to latex --pdf
110
119
111 Multiple notebooks can be given at the command line in a couple of
120 Multiple notebooks can be given at the command line in a couple of
112 different ways:
121 different ways:
113
122
114 > ipython nbconvert notebook*.ipynb
123 > ipython nbconvert notebook*.ipynb
115 > ipython nbconvert notebook1.ipynb notebook2.ipynb
124 > ipython nbconvert notebook1.ipynb notebook2.ipynb
116
125
117 or you can specify the notebooks list in a config file, containing::
126 or you can specify the notebooks list in a config file, containing::
118
127
119 c.NbConvertApp.notebooks = ["my_notebook.ipynb"]
128 c.NbConvertApp.notebooks = ["my_notebook.ipynb"]
120
129
121 > ipython nbconvert --config mycfg.py
130 > ipython nbconvert --config mycfg.py
122 """.format(get_export_names()))
131 """.format(get_export_names()))
132
123 # Writer specific variables
133 # Writer specific variables
124 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
134 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
125 help="""Instance of the writer class used to write the
135 help="""Instance of the writer class used to write the
126 results of the conversion.""")
136 results of the conversion.""")
127 writer_class = DottedObjectName('FilesWriter', config=True,
137 writer_class = DottedObjectName('FilesWriter', config=True,
128 help="""Writer class used to write the
138 help="""Writer class used to write the
129 results of the conversion""")
139 results of the conversion""")
130 writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter',
140 writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter',
131 'PDFWriter': 'IPython.nbconvert.writers.pdf.PDFWriter',
141 'PDFWriter': 'IPython.nbconvert.writers.pdf.PDFWriter',
132 'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter',
142 'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter',
133 'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'}
143 'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'}
134 writer_factory = Type()
144 writer_factory = Type()
135
145
136 def _writer_class_changed(self, name, old, new):
146 def _writer_class_changed(self, name, old, new):
137 if new in self.writer_aliases:
147 if new in self.writer_aliases:
138 new = self.writer_aliases[new]
148 new = self.writer_aliases[new]
139 self.writer_factory = import_item(new)
149 self.writer_factory = import_item(new)
140
150
151 # Post-processor specific variables
152 post_processor = Instance('IPython.nbconvert.post_processors.base.PostProcessorBase',
153 help="""Instance of the PostProcessor class used to write the
154 results of the conversion.""")
155
156 post_processor_class = DottedOrNone(config=True,
157 help="""PostProcessor class used to write the
158 results of the conversion""")
159 post_processor_aliases = {'PDF': 'IPython.nbconvert.post_processors.pdf.PDFPostProcessor'}
160 post_processor_factory = Type()
161
162 def _post_processor_class_changed(self, name, old, new):
163 if new in self.post_processor_aliases:
164 new = self.post_processor_aliases[new]
165 if new:
166 self.post_processor_factory = import_item(new)
167
141
168
142 # Other configurable variables
169 # Other configurable variables
143 export_format = CaselessStrEnum(get_export_names(),
170 export_format = CaselessStrEnum(get_export_names(),
144 default_value="html",
171 default_value="html",
145 config=True,
172 config=True,
146 help="""The export format to be used."""
173 help="""The export format to be used."""
147 )
174 )
148
175
149 notebooks = List([], config=True, help="""List of notebooks to convert.
176 notebooks = List([], config=True, help="""List of notebooks to convert.
150 Wildcards are supported.
177 Wildcards are supported.
151 Filenames passed positionally will be added to the list.
178 Filenames passed positionally will be added to the list.
152 """)
179 """)
153
180
154 @catch_config_error
181 @catch_config_error
155 def initialize(self, argv=None):
182 def initialize(self, argv=None):
156 super(NbConvertApp, self).initialize(argv)
183 super(NbConvertApp, self).initialize(argv)
157 self.init_syspath()
184 self.init_syspath()
158 self.init_notebooks()
185 self.init_notebooks()
159 self.init_writer()
186 self.init_writer()
187 self.init_post_processor()
188
160
189
161
190
162 def init_syspath(self):
191 def init_syspath(self):
163 """
192 """
164 Add the cwd to the sys.path ($PYTHONPATH)
193 Add the cwd to the sys.path ($PYTHONPATH)
165 """
194 """
166 sys.path.insert(0, os.getcwd())
195 sys.path.insert(0, os.getcwd())
167
196
168
197
169 def init_notebooks(self):
198 def init_notebooks(self):
170 """Construct the list of notebooks.
199 """Construct the list of notebooks.
171 If notebooks are passed on the command-line,
200 If notebooks are passed on the command-line,
172 they override notebooks specified in config files.
201 they override notebooks specified in config files.
173 Glob each notebook to replace notebook patterns with filenames.
202 Glob each notebook to replace notebook patterns with filenames.
174 """
203 """
175
204
176 # Specifying notebooks on the command-line overrides (rather than adds)
205 # Specifying notebooks on the command-line overrides (rather than adds)
177 # the notebook list
206 # the notebook list
178 if self.extra_args:
207 if self.extra_args:
179 patterns = self.extra_args
208 patterns = self.extra_args
180 else:
209 else:
181 patterns = self.notebooks
210 patterns = self.notebooks
182
211
183 # Use glob to replace all the notebook patterns with filenames.
212 # Use glob to replace all the notebook patterns with filenames.
184 filenames = []
213 filenames = []
185 for pattern in patterns:
214 for pattern in patterns:
186
215
187 # Use glob to find matching filenames. Allow the user to convert
216 # Use glob to find matching filenames. Allow the user to convert
188 # notebooks without having to type the extension.
217 # notebooks without having to type the extension.
189 globbed_files = glob.glob(pattern)
218 globbed_files = glob.glob(pattern)
190 globbed_files.extend(glob.glob(pattern + '.ipynb'))
219 globbed_files.extend(glob.glob(pattern + '.ipynb'))
191
220
192 for filename in globbed_files:
221 for filename in globbed_files:
193 if not filename in filenames:
222 if not filename in filenames:
194 filenames.append(filename)
223 filenames.append(filename)
195 self.notebooks = filenames
224 self.notebooks = filenames
196
225
197 def init_writer(self):
226 def init_writer(self):
198 """
227 """
199 Initialize the writer (which is stateless)
228 Initialize the writer (which is stateless)
200 """
229 """
201 self._writer_class_changed(None, self.writer_class, self.writer_class)
230 self._writer_class_changed(None, self.writer_class, self.writer_class)
202 self.writer = self.writer_factory(parent=self)
231 self.writer = self.writer_factory(parent=self)
203
232
233 def init_post_processor(self):
234 """
235 Initialize the post_processor (which is stateless)
236 """
237 self._post_processor_class_changed(None, self.post_processor_class,
238 self.post_processor_class)
239 if self.post_processor_factory:
240 self.post_processor = self.post_processor_factory(parent=self)
241
204 def start(self):
242 def start(self):
205 """
243 """
206 Ran after initialization completed
244 Ran after initialization completed
207 """
245 """
208 super(NbConvertApp, self).start()
246 super(NbConvertApp, self).start()
209 self.convert_notebooks()
247 self.convert_notebooks()
210
248
211 def convert_notebooks(self):
249 def convert_notebooks(self):
212 """
250 """
213 Convert the notebooks in the self.notebook traitlet
251 Convert the notebooks in the self.notebook traitlet
214 """
252 """
215 # Export each notebook
253 # Export each notebook
216 conversion_success = 0
254 conversion_success = 0
217 for notebook_filename in self.notebooks:
255 for notebook_filename in self.notebooks:
218
256
219 # Get a unique key for the notebook and set it in the resources object.
257 # Get a unique key for the notebook and set it in the resources object.
220 basename = os.path.basename(notebook_filename)
258 basename = os.path.basename(notebook_filename)
221 notebook_name = basename[:basename.rfind('.')]
259 notebook_name = basename[:basename.rfind('.')]
222 resources = {}
260 resources = {}
223 resources['unique_key'] = notebook_name
261 resources['unique_key'] = notebook_name
224 resources['output_files_dir'] = '%s_files' % notebook_name
262 resources['output_files_dir'] = '%s_files' % notebook_name
225
263
226 # Try to export
264 # Try to export
227 try:
265 try:
228 output, resources = export_by_name(self.export_format,
266 output, resources = export_by_name(self.export_format,
229 notebook_filename,
267 notebook_filename,
230 resources=resources,
268 resources=resources,
231 config=self.config)
269 config=self.config)
232 except ExporterNameError as e:
270 except ExporterNameError as e:
233 print("Error while converting '%s': '%s' exporter not found."
271 print("Error while converting '%s': '%s' exporter not found."
234 %(notebook_filename, self.export_format),
272 %(notebook_filename, self.export_format),
235 file=sys.stderr)
273 file=sys.stderr)
236 print("Known exporters are:",
274 print("Known exporters are:",
237 "\n\t" + "\n\t".join(get_export_names()),
275 "\n\t" + "\n\t".join(get_export_names()),
238 file=sys.stderr)
276 file=sys.stderr)
239 self.exit(1)
277 self.exit(1)
240 except ConversionException as e:
278 except ConversionException as e:
241 print("Error while converting '%s': %s" %(notebook_filename, e),
279 print("Error while converting '%s': %s" %(notebook_filename, e),
242 file=sys.stderr)
280 file=sys.stderr)
243 self.exit(1)
281 self.exit(1)
244 else:
282 else:
245 self.writer.write(output, resources, notebook_name=notebook_name)
283 write_resultes = self.writer.write(output, resources, notebook_name=notebook_name)
284
285 #Post-process if post processor has been defined.
286 if hasattr(self, 'post_processor') and self.post_processor:
287 self.post_processor(write_resultes)
246 conversion_success += 1
288 conversion_success += 1
247
289
248 # If nothing was converted successfully, help the user.
290 # If nothing was converted successfully, help the user.
249 if conversion_success == 0:
291 if conversion_success == 0:
250 self.print_help()
292 self.print_help()
251 sys.exit(-1)
293 sys.exit(-1)
252
294
253
295
254 #-----------------------------------------------------------------------------
296 #-----------------------------------------------------------------------------
255 # Main entry point
297 # Main entry point
256 #-----------------------------------------------------------------------------
298 #-----------------------------------------------------------------------------
257
299
258 launch_new_instance = NbConvertApp.launch_instance
300 launch_new_instance = NbConvertApp.launch_instance
@@ -1,47 +1,52 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 """
2 """
3 Contains writer for writing nbconvert output to PDF.
3 Contains writer for writing nbconvert output to PDF.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 #Copyright (c) 2013, the IPython Development Team.
6 #Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 #Distributed under the terms of the Modified BSD License.
8 #Distributed under the terms of the Modified BSD License.
9 #
9 #
10 #The full license is in the file COPYING.txt, distributed with this software.
10 #The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16
16
17 import subprocess
17 import subprocess
18 import os
18 import os
19
19
20 from IPython.utils.traitlets import Integer, Unicode
20 from IPython.utils.traitlets import Integer, Unicode, Bool
21
21
22 from .files import FilesWriter
22 from .base import PostProcessorBase
23
23
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25 # Classes
25 # Classes
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 class PDFWriter(FilesWriter):
27 class PDFPostProcessor(PostProcessorBase):
28 """Writer designed to write to PDF files"""
28 """Writer designed to write to PDF files"""
29
29
30 iteration_count = Integer(3, config=True, help="""
30 iteration_count = Integer(3, config=True, help="""
31 How many times pdflatex will be called.
31 How many times pdflatex will be called.
32 """)
32 """)
33
33
34 compiler = Unicode(u'pdflatex {0}', config=True, help="""
34 compiler = Unicode(u'pdflatex {0}', config=True, help="""
35 Shell command used to compile PDF.""")
35 Shell command used to compile PDF.""")
36
36
37 def write(self, output, resources, notebook_name=None, **kw):
37 verbose = Bool(False, config=True, help="""
38 Whether or not to display the output of the compile call.
39 """)
40
41 def call(self, input):
38 """
42 """
39 Consume and write Jinja output a PDF.
43 Consume and write Jinja output a PDF.
40 See files.py for more...
44 See files.py for more...
41 """
45 """
42 dest = super(PDFWriter, self).write(output, resources,
46 command = self.compiler.format(input)
43 notebook_name=notebook_name, **kw)
44 command = self.compiler.format(dest)
45
46 for index in range(self.iteration_count):
47 for index in range(self.iteration_count):
47 subprocess.Popen(command, shell=True, stdout=open(os.devnull, 'wb'))
48 if self.verbose:
49 subprocess.Popen(command, shell=True)
50 else:
51 with open(os.devnull, 'wb') as null:
52 subprocess.Popen(command, shell=True, stdout=null)
@@ -1,135 +1,149 b''
1 """
1 """
2 Contains tests for the nbconvertapp
2 Contains tests for the nbconvertapp
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 import os
16 import os
17 from .base import TestsBase
17 from .base import TestsBase
18
18
19 from IPython.utils import py3compat
19 from IPython.utils import py3compat
20 from IPython.testing import decorators as dec
20
21
21
22
22 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
23 # Constants
24 # Constants
24 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
25
26
26 # Define ipython commandline name
27 # Define ipython commandline name
27 if py3compat.PY3:
28 if py3compat.PY3:
28 IPYTHON = 'ipython3'
29 IPYTHON = 'ipython3'
29 else:
30 else:
30 IPYTHON = 'ipython'
31 IPYTHON = 'ipython'
31
32
32
33
33 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
34 # Classes and functions
35 # Classes and functions
35 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
36
37
37 class TestNbConvertApp(TestsBase):
38 class TestNbConvertApp(TestsBase):
38 """Collection of NbConvertApp tests"""
39 """Collection of NbConvertApp tests"""
39
40
40
41
41 def test_notebook_help(self):
42 def test_notebook_help(self):
42 """
43 """
43 Will help show if no notebooks are specified?
44 Will help show if no notebooks are specified?
44 """
45 """
45 with self.create_temp_cwd():
46 with self.create_temp_cwd():
46 assert "see '--help-all'" in self.call([IPYTHON, 'nbconvert'])
47 assert "see '--help-all'" in self.call([IPYTHON, 'nbconvert'])
47
48
48
49
49 def test_glob(self):
50 def test_glob(self):
50 """
51 """
51 Do search patterns work for notebook names?
52 Do search patterns work for notebook names?
52 """
53 """
53 with self.create_temp_cwd(['notebook*.ipynb']):
54 with self.create_temp_cwd(['notebook*.ipynb']):
54 assert not 'error' in self.call([IPYTHON, 'nbconvert',
55 assert not 'error' in self.call([IPYTHON, 'nbconvert',
55 '--to="python"', '--notebooks=["*.ipynb"]']).lower()
56 '--to="python"', '--notebooks=["*.ipynb"]']).lower()
56 assert os.path.isfile('notebook1.py')
57 assert os.path.isfile('notebook1.py')
57 assert os.path.isfile('notebook2.py')
58 assert os.path.isfile('notebook2.py')
58
59
59
60
60 def test_glob_subdir(self):
61 def test_glob_subdir(self):
61 """
62 """
62 Do search patterns work for subdirectory notebook names?
63 Do search patterns work for subdirectory notebook names?
63 """
64 """
64 with self.create_temp_cwd() as cwd:
65 with self.create_temp_cwd() as cwd:
65 self.copy_files_to(['notebook*.ipynb'], 'subdir/')
66 self.copy_files_to(['notebook*.ipynb'], 'subdir/')
66 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="python"',
67 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="python"',
67 '--notebooks=["%s"]' % os.path.join('subdir', '*.ipynb')]).lower()
68 '--notebooks=["%s"]' % os.path.join('subdir', '*.ipynb')]).lower()
68 assert os.path.isfile('notebook1.py')
69 assert os.path.isfile('notebook1.py')
69 assert os.path.isfile('notebook2.py')
70 assert os.path.isfile('notebook2.py')
70
71
71
72
72 def test_explicit(self):
73 def test_explicit(self):
73 """
74 """
74 Do explicit notebook names work?
75 Do explicit notebook names work?
75 """
76 """
76 with self.create_temp_cwd(['notebook*.ipynb']):
77 with self.create_temp_cwd(['notebook*.ipynb']):
77 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="python"',
78 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="python"',
78 '--notebooks=["notebook2.ipynb"]']).lower()
79 '--notebooks=["notebook2.ipynb"]']).lower()
79 assert not os.path.isfile('notebook1.py')
80 assert not os.path.isfile('notebook1.py')
80 assert os.path.isfile('notebook2.py')
81 assert os.path.isfile('notebook2.py')
81
82
82
83
84 #@dec.skip_known_failure
85 def test_post_processor(self):
86 """
87 Do post processors work?
88 """
89 with self.create_temp_cwd(['notebook1.ipynb']):
90 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="latex"',
91 'notebook1', '--post="PDF"', 'PDFPostProcessor.verbose=True']).lower()
92 assert os.path.isfile('notebook1.tex')
93 print("\n\n\t" + "\n\t".join([f for f in os.listdir('.') if os.path.isfile(f)]) + "\n\n")
94 assert os.path.isfile('notebook1.pdf')
95
96
83 def test_template(self):
97 def test_template(self):
84 """
98 """
85 Do export templates work?
99 Do export templates work?
86 """
100 """
87 with self.create_temp_cwd(['notebook*.ipynb']):
101 with self.create_temp_cwd(['notebook*.ipynb']):
88 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="slides"',
102 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="slides"',
89 '--notebooks=["notebook2.ipynb"]', '--template="reveal"']).lower()
103 '--notebooks=["notebook2.ipynb"]', '--template="reveal"']).lower()
90 assert os.path.isfile('notebook2.html')
104 assert os.path.isfile('notebook2.html')
91 with open('notebook2.html') as f:
105 with open('notebook2.html') as f:
92 assert '/reveal.css' in f.read()
106 assert '/reveal.css' in f.read()
93
107
94
108
95 def test_glob_explicit(self):
109 def test_glob_explicit(self):
96 """
110 """
97 Can a search pattern be used along with matching explicit notebook names?
111 Can a search pattern be used along with matching explicit notebook names?
98 """
112 """
99 with self.create_temp_cwd(['notebook*.ipynb']):
113 with self.create_temp_cwd(['notebook*.ipynb']):
100 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="python"',
114 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="python"',
101 '--notebooks=["*.ipynb", "notebook1.ipynb", "notebook2.ipynb"]']).lower()
115 '--notebooks=["*.ipynb", "notebook1.ipynb", "notebook2.ipynb"]']).lower()
102 assert os.path.isfile('notebook1.py')
116 assert os.path.isfile('notebook1.py')
103 assert os.path.isfile('notebook2.py')
117 assert os.path.isfile('notebook2.py')
104
118
105
119
106 def test_explicit_glob(self):
120 def test_explicit_glob(self):
107 """
121 """
108 Can explicit notebook names be used and then a matching search pattern?
122 Can explicit notebook names be used and then a matching search pattern?
109 """
123 """
110 with self.create_temp_cwd(['notebook*.ipynb']):
124 with self.create_temp_cwd(['notebook*.ipynb']):
111 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="python"',
125 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="python"',
112 '--notebooks=["notebook1.ipynb", "notebook2.ipynb", "*.ipynb"]']).lower()
126 '--notebooks=["notebook1.ipynb", "notebook2.ipynb", "*.ipynb"]']).lower()
113 assert os.path.isfile('notebook1.py')
127 assert os.path.isfile('notebook1.py')
114 assert os.path.isfile('notebook2.py')
128 assert os.path.isfile('notebook2.py')
115
129
116
130
117 def test_default_config(self):
131 def test_default_config(self):
118 """
132 """
119 Does the default config work?
133 Does the default config work?
120 """
134 """
121 with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py']):
135 with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py']):
122 assert not 'error' in self.call([IPYTHON, 'nbconvert']).lower()
136 assert not 'error' in self.call([IPYTHON, 'nbconvert']).lower()
123 assert os.path.isfile('notebook1.py')
137 assert os.path.isfile('notebook1.py')
124 assert not os.path.isfile('notebook2.py')
138 assert not os.path.isfile('notebook2.py')
125
139
126
140
127 def test_override_config(self):
141 def test_override_config(self):
128 """
142 """
129 Can the default config be overriden?
143 Can the default config be overriden?
130 """
144 """
131 with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py',
145 with self.create_temp_cwd(['notebook*.ipynb', 'ipython_nbconvert_config.py',
132 'override.py']):
146 'override.py']):
133 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--config="override.py"']).lower()
147 assert not 'error' in self.call([IPYTHON, 'nbconvert', '--config="override.py"']).lower()
134 assert not os.path.isfile('notebook1.py')
148 assert not os.path.isfile('notebook1.py')
135 assert os.path.isfile('notebook2.py')
149 assert os.path.isfile('notebook2.py')
General Comments 0
You need to be logged in to leave comments. Login now