Show More
@@ -92,12 +92,15 class Exporter(Configurable): | |||
|
92 | 92 | __doc__ = __doc__.format(filters = '- '+'\n - '.join(default_filters.keys())) |
|
93 | 93 | |
|
94 | 94 | |
|
95 | flavor = Unicode(config=True, help="""Flavor of the data format to use. | |
|
96 | I.E. 'full' or 'basic'""") | |
|
97 | ||
|
98 | template_file = Unicode( | |
|
95 | template_file = Unicode(u'default', | |
|
99 | 96 | config=True, |
|
100 | 97 | help="Name of the template file to use") |
|
98 | def _template_file_changed(self, name, old, new): | |
|
99 | if new=='default': | |
|
100 | self.template_file = self.default_template | |
|
101 | else: | |
|
102 | self.template_file = new | |
|
103 | default_template = Unicode(u'') | |
|
101 | 104 | |
|
102 | 105 | file_extension = Unicode( |
|
103 | 106 | 'txt', config=True, |
@@ -156,9 +159,8 class Exporter(Configurable): | |||
|
156 | 159 | extra_loaders : list[of Jinja Loaders] |
|
157 | 160 | ordered list of Jinja loder to find templates. Will be tried in order |
|
158 | 161 | before the default FileSysteme ones. |
|
159 | flavor : str | |
|
160 |
|
|
|
161 | if one hasn't been specifically provided. | |
|
162 | template : str (optional, kw arg) | |
|
163 | Template to use when exporting. | |
|
162 | 164 | """ |
|
163 | 165 | |
|
164 | 166 | #Call the base class constructor |
@@ -194,12 +196,29 class Exporter(Configurable): | |||
|
194 | 196 | nb_copy = copy.deepcopy(nb) |
|
195 | 197 | resources = self._init_resources(resources) |
|
196 | 198 | |
|
197 | #Preprocess | |
|
199 | # Preprocess | |
|
198 | 200 | nb_copy, resources = self._transform(nb_copy, resources) |
|
199 | 201 | |
|
200 | #Convert | |
|
201 | self.template = self.environment.get_template(self.template_file + self.template_extension) | |
|
202 | output = self.template.render(nb=nb_copy, resources=resources) | |
|
202 | # Try different template names during conversion. First try to load the | |
|
203 | # template by name with extension added, then try loading the template | |
|
204 | # as if the name is explicitly specified, then try the name as a | |
|
205 | # 'flavor', and lastly just try to load the template by module name. | |
|
206 | module_name = self.__module__.split('.')[-1] | |
|
207 | try_names = [self.template_file + self.template_extension, | |
|
208 | self.template_file, | |
|
209 | module_name + '_' + self.template_file + self.template_extension, | |
|
210 | module_name + self.template_extension] | |
|
211 | for try_name in try_names: | |
|
212 | try: | |
|
213 | self.template = self.environment.get_template(try_name) | |
|
214 | break | |
|
215 | except: | |
|
216 | pass | |
|
217 | ||
|
218 | if hasattr(self, 'template'): | |
|
219 | output = self.template.render(nb=nb_copy, resources=resources) | |
|
220 | else: | |
|
221 | raise IOError('template file "%s" could not be found' % self.template_file) | |
|
203 | 222 | return output, resources |
|
204 | 223 | |
|
205 | 224 | |
@@ -339,19 +358,9 class Exporter(Configurable): | |||
|
339 | 358 | Make sure a template name is specified. If one isn't specified, try to |
|
340 | 359 | build one from the information we know. |
|
341 | 360 | """ |
|
342 | ||
|
343 | # Set the template_file if it has not been set explicitly. | |
|
344 |
|
|
|
345 | ||
|
346 | # Build the template file name from the name of the exporter and the | |
|
347 | # flavor (if available). The flavor can be set on the traitlet | |
|
348 | # or passed in as a kw arg. The flavor specified in kw overrides | |
|
349 | # what is set in the flavor traitlet. | |
|
350 | module_name = self.__module__.split('.')[-1] | |
|
351 | if self.flavor or 'flavor' in kw: | |
|
352 | self.template_file = module_name + '_' + kw.get('flavor', self.flavor) | |
|
353 | else: | |
|
354 | self.template_file = module_name | |
|
361 | self._template_file_changed('template_file', self.template_file, self.template_file) | |
|
362 | if 'template' in kw: | |
|
363 | self.template_file = kw['template'] | |
|
355 | 364 | |
|
356 | 365 | |
|
357 | 366 | def _init_environment(self, extra_loaders=None): |
@@ -38,18 +38,15 class HTMLExporter(Exporter): | |||
|
38 | 38 | help="Extension of the file that should be written to disk" |
|
39 | 39 | ) |
|
40 | 40 | |
|
41 |
|
|
|
42 | use. I.E. 'full' or 'basic'""") | |
|
41 | default_template = Unicode('full', config=True, help="""Flavor of the data | |
|
42 | format to use. I.E. 'full' or 'basic'""") | |
|
43 | 43 | |
|
44 | 44 | @property |
|
45 | 45 | def default_config(self): |
|
46 | 46 | c = Config({ |
|
47 | 47 | 'CSSHTMLHeaderTransformer':{ |
|
48 | 48 | 'enabled':True |
|
49 |
} |
|
|
50 | 'RevealHelpTransformer':{ | |
|
51 | 'enabled':True, | |
|
52 | }, | |
|
49 | } | |
|
53 | 50 | }) |
|
54 | 51 | c.merge(super(HTMLExporter,self).default_config) |
|
55 | 52 | return c |
@@ -44,8 +44,8 class LatexExporter(Exporter): | |||
|
44 | 44 | 'tex', config=True, |
|
45 | 45 | help="Extension of the file that should be written to disk") |
|
46 | 46 | |
|
47 |
|
|
|
48 | use. I.E. 'full' or 'basic'""") | |
|
47 | default_template = Unicode('article', config=True, help="""Template of the | |
|
48 | data format to use. I.E. 'full' or 'basic'""") | |
|
49 | 49 | |
|
50 | 50 | #Latex constants |
|
51 | 51 | default_template_path = Unicode( |
@@ -35,8 +35,8 class SlidesExporter(Exporter): | |||
|
35 | 35 | help="Extension of the file that should be written to disk" |
|
36 | 36 | ) |
|
37 | 37 | |
|
38 |
|
|
|
39 | use. I.E. 'reveal'""") | |
|
38 | default_template = Unicode('reveal', config=True, help="""Template of the | |
|
39 | data format to use. I.E. 'reveal'""") | |
|
40 | 40 | |
|
41 | 41 | @property |
|
42 | 42 | def default_config(self): |
@@ -42,15 +42,15 class TestHTMLExporter(ExportersTestsBase): | |||
|
42 | 42 | |
|
43 | 43 | def test_export_basic(self): |
|
44 | 44 | """ |
|
45 |
Can a HTMLExporter export using the 'basic' |
|
|
45 | Can a HTMLExporter export using the 'basic' template? | |
|
46 | 46 | """ |
|
47 |
(output, resources) = HTMLExporter( |
|
|
47 | (output, resources) = HTMLExporter(template='basic').from_filename(self._get_notebook()) | |
|
48 | 48 | assert len(output) > 0 |
|
49 | 49 | |
|
50 | 50 | |
|
51 | 51 | def test_export_full(self): |
|
52 | 52 | """ |
|
53 |
Can a HTMLExporter export using the 'full' |
|
|
53 | Can a HTMLExporter export using the 'full' template? | |
|
54 | 54 | """ |
|
55 |
(output, resources) = HTMLExporter( |
|
|
55 | (output, resources) = HTMLExporter(template='full').from_filename(self._get_notebook()) | |
|
56 | 56 | assert len(output) > 0 |
@@ -43,23 +43,23 class TestLatexExporter(ExportersTestsBase): | |||
|
43 | 43 | |
|
44 | 44 | def test_export_book(self): |
|
45 | 45 | """ |
|
46 |
Can a LatexExporter export using 'book' |
|
|
46 | Can a LatexExporter export using 'book' template? | |
|
47 | 47 | """ |
|
48 |
(output, resources) = LatexExporter( |
|
|
48 | (output, resources) = LatexExporter(template='book').from_filename(self._get_notebook()) | |
|
49 | 49 | assert len(output) > 0 |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | def test_export_basic(self): |
|
53 | 53 | """ |
|
54 |
Can a LatexExporter export using 'basic' |
|
|
54 | Can a LatexExporter export using 'basic' template? | |
|
55 | 55 | """ |
|
56 |
(output, resources) = LatexExporter( |
|
|
56 | (output, resources) = LatexExporter(template='basic').from_filename(self._get_notebook()) | |
|
57 | 57 | assert len(output) > 0 |
|
58 | 58 | |
|
59 | 59 | |
|
60 | 60 | def test_export_article(self): |
|
61 | 61 | """ |
|
62 |
Can a LatexExporter export using 'article' |
|
|
62 | Can a LatexExporter export using 'article' template? | |
|
63 | 63 | """ |
|
64 |
(output, resources) = LatexExporter( |
|
|
64 | (output, resources) = LatexExporter(template='article').from_filename(self._get_notebook()) | |
|
65 | 65 | assert len(output) > 0 No newline at end of file |
@@ -41,7 +41,7 class TestSlidesExporter(ExportersTestsBase): | |||
|
41 | 41 | |
|
42 | 42 | def test_export_reveal(self): |
|
43 | 43 | """ |
|
44 |
Can a SlidesExporter export using the 'reveal' |
|
|
44 | Can a SlidesExporter export using the 'reveal' template? | |
|
45 | 45 | """ |
|
46 |
(output, resources) = SlidesExporter( |
|
|
46 | (output, resources) = SlidesExporter(template='reveal').from_filename(self._get_notebook()) | |
|
47 | 47 | assert len(output) > 0 |
@@ -42,7 +42,6 nbconvert_aliases = {} | |||
|
42 | 42 | nbconvert_aliases.update(base_aliases) |
|
43 | 43 | nbconvert_aliases.update({ |
|
44 | 44 | 'to' : 'NbConvertApp.export_format', |
|
45 | 'flavor' : 'Exporter.flavor', | |
|
46 | 45 | 'template' : 'Exporter.template_file', |
|
47 | 46 | 'notebooks' : 'NbConvertApp.notebooks', |
|
48 | 47 | 'writer' : 'NbConvertApp.writer_class', |
@@ -58,7 +57,7 nbconvert_flags.update({ | |||
|
58 | 57 | |
|
59 | 58 | 'pdf' : ( |
|
60 | 59 | {'NbConvertApp' : {'writer_class' : "PDFWriter"}}, |
|
61 | "Compile notebook output to a PDF." | |
|
60 | "Compile notebook output to a PDF (requires `--to latex`)." | |
|
62 | 61 | ) |
|
63 | 62 | }) |
|
64 | 63 | |
@@ -95,11 +94,11 class NbConvertApp(BaseIPythonApplication): | |||
|
95 | 94 | |
|
96 | 95 | > ipython nbconvert --to latex mynotebook.ipnynb |
|
97 | 96 | |
|
98 |
Both HTML and LaTeX support multiple |
|
|
97 | Both HTML and LaTeX support multiple output templates. LaTeX includes | |
|
99 | 98 | 'basic', 'book', and 'article'. HTML includes 'basic' and 'full'. You |
|
100 | 99 | can specify the flavor of the format used. |
|
101 | 100 | |
|
102 |
> ipython nbconvert --to html -- |
|
|
101 | > ipython nbconvert --to html --template reveal mynotebook.ipnynb | |
|
103 | 102 | |
|
104 | 103 | You can also pipe the output to stdout, rather than a file |
|
105 | 104 | |
@@ -107,7 +106,7 class NbConvertApp(BaseIPythonApplication): | |||
|
107 | 106 | |
|
108 | 107 | or to a PDF |
|
109 | 108 | |
|
110 | > ipython nbconvert mynotebook.ipynb --pdf | |
|
109 | > ipython nbconvert mynotebook.ipynb --to latex --pdf | |
|
111 | 110 | |
|
112 | 111 | Multiple notebooks can be given at the command line in a couple of |
|
113 | 112 | different ways: |
@@ -80,13 +80,13 class TestNbConvertApp(TestsBase): | |||
|
80 | 80 | assert os.path.isfile('notebook2.py') |
|
81 | 81 | |
|
82 | 82 | |
|
83 |
def test_ |
|
|
83 | def test_template(self): | |
|
84 | 84 | """ |
|
85 |
Do export |
|
|
85 | Do export templates work? | |
|
86 | 86 | """ |
|
87 | 87 | with self.create_temp_cwd(['notebook*.ipynb']): |
|
88 | 88 | assert not 'error' in self.call([IPYTHON, 'nbconvert', '--to="slides"', |
|
89 |
'--notebooks=["notebook2.ipynb"]', '-- |
|
|
89 | '--notebooks=["notebook2.ipynb"]', '--template="reveal"']).lower() | |
|
90 | 90 | assert os.path.isfile('notebook2.html') |
|
91 | 91 | with open('notebook2.html') as f: |
|
92 | 92 | assert '/reveal.css' in f.read() |
@@ -17,7 +17,7 Contains writer for writing nbconvert output to PDF. | |||
|
17 | 17 | import subprocess |
|
18 | 18 | import os |
|
19 | 19 | |
|
20 | from IPython.utils.traitlets import Integer | |
|
20 | from IPython.utils.traitlets import Integer, Unicode | |
|
21 | 21 | |
|
22 | 22 | from .files import FilesWriter |
|
23 | 23 | |
@@ -31,12 +31,17 class PDFWriter(FilesWriter): | |||
|
31 | 31 | How many times pdflatex will be called. |
|
32 | 32 | """) |
|
33 | 33 | |
|
34 | compiler = Unicode(u'pdflatex {0}', config=True, help=""" | |
|
35 | Shell command used to compile PDF.""") | |
|
36 | ||
|
34 | 37 | def write(self, output, resources, notebook_name=None, **kw): |
|
35 | 38 | """ |
|
36 | 39 | Consume and write Jinja output a PDF. |
|
37 | 40 | See files.py for more... |
|
38 | 41 | """ |
|
39 |
dest = super(PDFWriter, self).write(output, resources, |
|
|
40 | command = 'pdflatex ' + dest | |
|
42 | dest = super(PDFWriter, self).write(output, resources, | |
|
43 | notebook_name=notebook_name, **kw) | |
|
44 | command = self.compiler.format(dest) | |
|
45 | ||
|
41 | 46 | for index in range(self.iteration_count): |
|
42 | 47 | subprocess.Popen(command, shell=True, stdout=open(os.devnull, 'wb')) |
General Comments 0
You need to be logged in to leave comments.
Login now