Show More
@@ -1,321 +1,323 | |||||
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 |
|
20 | |||
21 | import logging |
|
21 | import logging | |
22 | import sys |
|
22 | import sys | |
23 | import os |
|
23 | import os | |
24 | import glob |
|
24 | import glob | |
25 |
|
25 | |||
26 | # From IPython |
|
26 | # From IPython | |
27 | from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags |
|
27 | from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags | |
28 | from IPython.config import catch_config_error, Configurable |
|
28 | from IPython.config import catch_config_error, Configurable | |
29 | from IPython.utils.traitlets import ( |
|
29 | from IPython.utils.traitlets import ( | |
30 | Unicode, List, Instance, DottedObjectName, Type, CaselessStrEnum, |
|
30 | Unicode, List, Instance, DottedObjectName, Type, CaselessStrEnum, | |
31 | ) |
|
31 | ) | |
32 | from IPython.utils.importstring import import_item |
|
32 | from IPython.utils.importstring import import_item | |
33 | from IPython.utils.text import dedent |
|
33 | from IPython.utils.text import dedent | |
34 |
|
34 | |||
35 | from .exporters.export import get_export_names, exporter_map |
|
35 | from .exporters.export import get_export_names, exporter_map | |
36 | from IPython.nbconvert import exporters, transformers, writers, post_processors |
|
36 | from IPython.nbconvert import exporters, transformers, writers, post_processors | |
37 | from .utils.base import NbConvertBase |
|
37 | from .utils.base import NbConvertBase | |
38 | from .utils.exceptions import ConversionException |
|
38 | from .utils.exceptions import ConversionException | |
39 |
|
39 | |||
40 | #----------------------------------------------------------------------------- |
|
40 | #----------------------------------------------------------------------------- | |
41 | #Classes and functions |
|
41 | #Classes and functions | |
42 | #----------------------------------------------------------------------------- |
|
42 | #----------------------------------------------------------------------------- | |
43 |
|
43 | |||
44 | class DottedOrNone(DottedObjectName): |
|
44 | class DottedOrNone(DottedObjectName): | |
45 | """ |
|
45 | """ | |
46 | A string holding a valid dotted object name in Python, such as A.b3._c |
|
46 | A string holding a valid dotted object name in Python, such as A.b3._c | |
47 | Also allows for None type.""" |
|
47 | Also allows for None type.""" | |
48 |
|
48 | |||
49 | default_value = u'' |
|
49 | default_value = u'' | |
50 |
|
50 | |||
51 | def validate(self, obj, value): |
|
51 | def validate(self, obj, value): | |
52 | if value is not None and len(value) > 0: |
|
52 | if value is not None and len(value) > 0: | |
53 | return super(DottedOrNone, self).validate(obj, value) |
|
53 | return super(DottedOrNone, self).validate(obj, value) | |
54 | else: |
|
54 | else: | |
55 | return value |
|
55 | return value | |
56 |
|
56 | |||
57 | nbconvert_aliases = {} |
|
57 | nbconvert_aliases = {} | |
58 | nbconvert_aliases.update(base_aliases) |
|
58 | nbconvert_aliases.update(base_aliases) | |
59 | nbconvert_aliases.update({ |
|
59 | nbconvert_aliases.update({ | |
60 | 'to' : 'NbConvertApp.export_format', |
|
60 | 'to' : 'NbConvertApp.export_format', | |
61 | 'template' : 'Exporter.template_file', |
|
61 | 'template' : 'Exporter.template_file', | |
62 | 'notebooks' : 'NbConvertApp.notebooks', |
|
62 | 'notebooks' : 'NbConvertApp.notebooks', | |
63 | 'writer' : 'NbConvertApp.writer_class', |
|
63 | 'writer' : 'NbConvertApp.writer_class', | |
64 | 'post': 'NbConvertApp.post_processor_class', |
|
64 | 'post': 'NbConvertApp.post_processor_class', | |
65 | 'output': 'NbConvertApp.output_base' |
|
65 | 'output': 'NbConvertApp.output_base', | |
|
66 | 'local': 'RevealHelpTransformer.url_prefix', | |||
|
67 | 'notes': 'RevealHelpTransformer.speaker_notes' | |||
66 | }) |
|
68 | }) | |
67 |
|
69 | |||
68 | nbconvert_flags = {} |
|
70 | nbconvert_flags = {} | |
69 | nbconvert_flags.update(base_flags) |
|
71 | nbconvert_flags.update(base_flags) | |
70 | nbconvert_flags.update({ |
|
72 | nbconvert_flags.update({ | |
71 | 'stdout' : ( |
|
73 | 'stdout' : ( | |
72 | {'NbConvertApp' : {'writer_class' : "StdoutWriter"}}, |
|
74 | {'NbConvertApp' : {'writer_class' : "StdoutWriter"}}, | |
73 | "Write notebook output to stdout instead of files." |
|
75 | "Write notebook output to stdout instead of files." | |
74 | ) |
|
76 | ) | |
75 | }) |
|
77 | }) | |
76 |
|
78 | |||
77 |
|
79 | |||
78 | class NbConvertApp(BaseIPythonApplication): |
|
80 | class NbConvertApp(BaseIPythonApplication): | |
79 | """Application used to convert to and from notebook file type (*.ipynb)""" |
|
81 | """Application used to convert to and from notebook file type (*.ipynb)""" | |
80 |
|
82 | |||
81 | name = 'ipython-nbconvert' |
|
83 | name = 'ipython-nbconvert' | |
82 | aliases = nbconvert_aliases |
|
84 | aliases = nbconvert_aliases | |
83 | flags = nbconvert_flags |
|
85 | flags = nbconvert_flags | |
84 |
|
86 | |||
85 | def _log_level_default(self): |
|
87 | def _log_level_default(self): | |
86 | return logging.INFO |
|
88 | return logging.INFO | |
87 |
|
89 | |||
88 | def _classes_default(self): |
|
90 | def _classes_default(self): | |
89 | classes = [NbConvertBase] |
|
91 | classes = [NbConvertBase] | |
90 | for pkg in (exporters, transformers, writers): |
|
92 | for pkg in (exporters, transformers, writers): | |
91 | for name in dir(pkg): |
|
93 | for name in dir(pkg): | |
92 | cls = getattr(pkg, name) |
|
94 | cls = getattr(pkg, name) | |
93 | if isinstance(cls, type) and issubclass(cls, Configurable): |
|
95 | if isinstance(cls, type) and issubclass(cls, Configurable): | |
94 | classes.append(cls) |
|
96 | classes.append(cls) | |
95 | return classes |
|
97 | return classes | |
96 |
|
98 | |||
97 | description = Unicode( |
|
99 | description = Unicode( | |
98 | u"""This application is used to convert notebook files (*.ipynb) |
|
100 | u"""This application is used to convert notebook files (*.ipynb) | |
99 | to various other formats. |
|
101 | to various other formats. | |
100 |
|
102 | |||
101 | WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.""") |
|
103 | WARNING: THE COMMANDLINE INTERFACE MAY CHANGE IN FUTURE RELEASES.""") | |
102 |
|
104 | |||
103 | output_base = Unicode('', config=True, help='''overwrite base name use for output files. |
|
105 | output_base = Unicode('', config=True, help='''overwrite base name use for output files. | |
104 | can only be use when converting one notebook at a time. |
|
106 | can only be use when converting one notebook at a time. | |
105 | ''') |
|
107 | ''') | |
106 |
|
108 | |||
107 | examples = Unicode(u""" |
|
109 | examples = Unicode(u""" | |
108 | The simplest way to use nbconvert is |
|
110 | The simplest way to use nbconvert is | |
109 |
|
111 | |||
110 | > ipython nbconvert mynotebook.ipynb |
|
112 | > ipython nbconvert mynotebook.ipynb | |
111 |
|
113 | |||
112 | which will convert mynotebook.ipynb to the default format (probably HTML). |
|
114 | which will convert mynotebook.ipynb to the default format (probably HTML). | |
113 |
|
115 | |||
114 | You can specify the export format with `--to`. |
|
116 | You can specify the export format with `--to`. | |
115 | Options include {0} |
|
117 | Options include {0} | |
116 |
|
118 | |||
117 | > ipython nbconvert --to latex mynotebook.ipnynb |
|
119 | > ipython nbconvert --to latex mynotebook.ipnynb | |
118 |
|
120 | |||
119 | Both HTML and LaTeX support multiple output templates. LaTeX includes |
|
121 | Both HTML and LaTeX support multiple output templates. LaTeX includes | |
120 | 'basic', 'book', and 'article'. HTML includes 'basic' and 'full'. You |
|
122 | 'basic', 'book', and 'article'. HTML includes 'basic' and 'full'. You | |
121 | can specify the flavor of the format used. |
|
123 | can specify the flavor of the format used. | |
122 |
|
124 | |||
123 | > ipython nbconvert --to html --template basic mynotebook.ipynb |
|
125 | > ipython nbconvert --to html --template basic mynotebook.ipynb | |
124 |
|
126 | |||
125 | You can also pipe the output to stdout, rather than a file |
|
127 | You can also pipe the output to stdout, rather than a file | |
126 |
|
128 | |||
127 | > ipython nbconvert mynotebook.ipynb --stdout |
|
129 | > ipython nbconvert mynotebook.ipynb --stdout | |
128 |
|
130 | |||
129 | A post-processor can be used to compile a PDF |
|
131 | A post-processor can be used to compile a PDF | |
130 |
|
132 | |||
131 | > ipython nbconvert mynotebook.ipynb --to latex --post PDF |
|
133 | > ipython nbconvert mynotebook.ipynb --to latex --post PDF | |
132 |
|
134 | |||
133 | You can get (and serve) a Reveal.js-powered slideshow |
|
135 | You can get (and serve) a Reveal.js-powered slideshow | |
134 |
|
136 | |||
135 | > ipython nbconvert myslides.ipynb --to slides --post serve |
|
137 | > ipython nbconvert myslides.ipynb --to slides --post serve | |
136 |
|
138 | |||
137 | Multiple notebooks can be given at the command line in a couple of |
|
139 | Multiple notebooks can be given at the command line in a couple of | |
138 | different ways: |
|
140 | different ways: | |
139 |
|
141 | |||
140 | > ipython nbconvert notebook*.ipynb |
|
142 | > ipython nbconvert notebook*.ipynb | |
141 | > ipython nbconvert notebook1.ipynb notebook2.ipynb |
|
143 | > ipython nbconvert notebook1.ipynb notebook2.ipynb | |
142 |
|
144 | |||
143 | or you can specify the notebooks list in a config file, containing:: |
|
145 | or you can specify the notebooks list in a config file, containing:: | |
144 |
|
146 | |||
145 | c.NbConvertApp.notebooks = ["my_notebook.ipynb"] |
|
147 | c.NbConvertApp.notebooks = ["my_notebook.ipynb"] | |
146 |
|
148 | |||
147 | > ipython nbconvert --config mycfg.py |
|
149 | > ipython nbconvert --config mycfg.py | |
148 | """.format(get_export_names())) |
|
150 | """.format(get_export_names())) | |
149 |
|
151 | |||
150 | # Writer specific variables |
|
152 | # Writer specific variables | |
151 | writer = Instance('IPython.nbconvert.writers.base.WriterBase', |
|
153 | writer = Instance('IPython.nbconvert.writers.base.WriterBase', | |
152 | help="""Instance of the writer class used to write the |
|
154 | help="""Instance of the writer class used to write the | |
153 | results of the conversion.""") |
|
155 | results of the conversion.""") | |
154 | writer_class = DottedObjectName('FilesWriter', config=True, |
|
156 | writer_class = DottedObjectName('FilesWriter', config=True, | |
155 | help="""Writer class used to write the |
|
157 | help="""Writer class used to write the | |
156 | results of the conversion""") |
|
158 | results of the conversion""") | |
157 | writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter', |
|
159 | writer_aliases = {'FilesWriter': 'IPython.nbconvert.writers.files.FilesWriter', | |
158 | 'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter', |
|
160 | 'DebugWriter': 'IPython.nbconvert.writers.debug.DebugWriter', | |
159 | 'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'} |
|
161 | 'StdoutWriter': 'IPython.nbconvert.writers.stdout.StdoutWriter'} | |
160 | writer_factory = Type() |
|
162 | writer_factory = Type() | |
161 |
|
163 | |||
162 | def _writer_class_changed(self, name, old, new): |
|
164 | def _writer_class_changed(self, name, old, new): | |
163 | if new in self.writer_aliases: |
|
165 | if new in self.writer_aliases: | |
164 | new = self.writer_aliases[new] |
|
166 | new = self.writer_aliases[new] | |
165 | self.writer_factory = import_item(new) |
|
167 | self.writer_factory = import_item(new) | |
166 |
|
168 | |||
167 | # Post-processor specific variables |
|
169 | # Post-processor specific variables | |
168 | post_processor = Instance('IPython.nbconvert.post_processors.base.PostProcessorBase', |
|
170 | post_processor = Instance('IPython.nbconvert.post_processors.base.PostProcessorBase', | |
169 | help="""Instance of the PostProcessor class used to write the |
|
171 | help="""Instance of the PostProcessor class used to write the | |
170 | results of the conversion.""") |
|
172 | results of the conversion.""") | |
171 |
|
173 | |||
172 | post_processor_class = DottedOrNone(config=True, |
|
174 | post_processor_class = DottedOrNone(config=True, | |
173 | help="""PostProcessor class used to write the |
|
175 | help="""PostProcessor class used to write the | |
174 | results of the conversion""") |
|
176 | results of the conversion""") | |
175 | post_processor_aliases = {'PDF': 'IPython.nbconvert.post_processors.pdf.PDFPostProcessor', |
|
177 | post_processor_aliases = {'PDF': 'IPython.nbconvert.post_processors.pdf.PDFPostProcessor', | |
176 | 'serve': 'IPython.nbconvert.post_processors.serve.ServePostProcessor'} |
|
178 | 'serve': 'IPython.nbconvert.post_processors.serve.ServePostProcessor'} | |
177 | post_processor_factory = Type() |
|
179 | post_processor_factory = Type() | |
178 |
|
180 | |||
179 | def _post_processor_class_changed(self, name, old, new): |
|
181 | def _post_processor_class_changed(self, name, old, new): | |
180 | if new in self.post_processor_aliases: |
|
182 | if new in self.post_processor_aliases: | |
181 | new = self.post_processor_aliases[new] |
|
183 | new = self.post_processor_aliases[new] | |
182 | if new: |
|
184 | if new: | |
183 | self.post_processor_factory = import_item(new) |
|
185 | self.post_processor_factory = import_item(new) | |
184 |
|
186 | |||
185 |
|
187 | |||
186 | # Other configurable variables |
|
188 | # Other configurable variables | |
187 | export_format = CaselessStrEnum(get_export_names(), |
|
189 | export_format = CaselessStrEnum(get_export_names(), | |
188 | default_value="html", |
|
190 | default_value="html", | |
189 | config=True, |
|
191 | config=True, | |
190 | help="""The export format to be used.""" |
|
192 | help="""The export format to be used.""" | |
191 | ) |
|
193 | ) | |
192 |
|
194 | |||
193 | notebooks = List([], config=True, help="""List of notebooks to convert. |
|
195 | notebooks = List([], config=True, help="""List of notebooks to convert. | |
194 | Wildcards are supported. |
|
196 | Wildcards are supported. | |
195 | Filenames passed positionally will be added to the list. |
|
197 | Filenames passed positionally will be added to the list. | |
196 | """) |
|
198 | """) | |
197 |
|
199 | |||
198 | @catch_config_error |
|
200 | @catch_config_error | |
199 | def initialize(self, argv=None): |
|
201 | def initialize(self, argv=None): | |
200 | super(NbConvertApp, self).initialize(argv) |
|
202 | super(NbConvertApp, self).initialize(argv) | |
201 | self.init_syspath() |
|
203 | self.init_syspath() | |
202 | self.init_notebooks() |
|
204 | self.init_notebooks() | |
203 | self.init_writer() |
|
205 | self.init_writer() | |
204 | self.init_post_processor() |
|
206 | self.init_post_processor() | |
205 |
|
207 | |||
206 |
|
208 | |||
207 |
|
209 | |||
208 | def init_syspath(self): |
|
210 | def init_syspath(self): | |
209 | """ |
|
211 | """ | |
210 | Add the cwd to the sys.path ($PYTHONPATH) |
|
212 | Add the cwd to the sys.path ($PYTHONPATH) | |
211 | """ |
|
213 | """ | |
212 | sys.path.insert(0, os.getcwd()) |
|
214 | sys.path.insert(0, os.getcwd()) | |
213 |
|
215 | |||
214 |
|
216 | |||
215 | def init_notebooks(self): |
|
217 | def init_notebooks(self): | |
216 | """Construct the list of notebooks. |
|
218 | """Construct the list of notebooks. | |
217 | If notebooks are passed on the command-line, |
|
219 | If notebooks are passed on the command-line, | |
218 | they override notebooks specified in config files. |
|
220 | they override notebooks specified in config files. | |
219 | Glob each notebook to replace notebook patterns with filenames. |
|
221 | Glob each notebook to replace notebook patterns with filenames. | |
220 | """ |
|
222 | """ | |
221 |
|
223 | |||
222 | # Specifying notebooks on the command-line overrides (rather than adds) |
|
224 | # Specifying notebooks on the command-line overrides (rather than adds) | |
223 | # the notebook list |
|
225 | # the notebook list | |
224 | if self.extra_args: |
|
226 | if self.extra_args: | |
225 | patterns = self.extra_args |
|
227 | patterns = self.extra_args | |
226 | else: |
|
228 | else: | |
227 | patterns = self.notebooks |
|
229 | patterns = self.notebooks | |
228 |
|
230 | |||
229 | # Use glob to replace all the notebook patterns with filenames. |
|
231 | # Use glob to replace all the notebook patterns with filenames. | |
230 | filenames = [] |
|
232 | filenames = [] | |
231 | for pattern in patterns: |
|
233 | for pattern in patterns: | |
232 |
|
234 | |||
233 | # Use glob to find matching filenames. Allow the user to convert |
|
235 | # Use glob to find matching filenames. Allow the user to convert | |
234 | # notebooks without having to type the extension. |
|
236 | # notebooks without having to type the extension. | |
235 | globbed_files = glob.glob(pattern) |
|
237 | globbed_files = glob.glob(pattern) | |
236 | globbed_files.extend(glob.glob(pattern + '.ipynb')) |
|
238 | globbed_files.extend(glob.glob(pattern + '.ipynb')) | |
237 | if not globbed_files: |
|
239 | if not globbed_files: | |
238 | self.log.warn("pattern %r matched no files", pattern) |
|
240 | self.log.warn("pattern %r matched no files", pattern) | |
239 |
|
241 | |||
240 | for filename in globbed_files: |
|
242 | for filename in globbed_files: | |
241 | if not filename in filenames: |
|
243 | if not filename in filenames: | |
242 | filenames.append(filename) |
|
244 | filenames.append(filename) | |
243 | self.notebooks = filenames |
|
245 | self.notebooks = filenames | |
244 |
|
246 | |||
245 | def init_writer(self): |
|
247 | def init_writer(self): | |
246 | """ |
|
248 | """ | |
247 | Initialize the writer (which is stateless) |
|
249 | Initialize the writer (which is stateless) | |
248 | """ |
|
250 | """ | |
249 | self._writer_class_changed(None, self.writer_class, self.writer_class) |
|
251 | self._writer_class_changed(None, self.writer_class, self.writer_class) | |
250 | self.writer = self.writer_factory(parent=self) |
|
252 | self.writer = self.writer_factory(parent=self) | |
251 |
|
253 | |||
252 | def init_post_processor(self): |
|
254 | def init_post_processor(self): | |
253 | """ |
|
255 | """ | |
254 | Initialize the post_processor (which is stateless) |
|
256 | Initialize the post_processor (which is stateless) | |
255 | """ |
|
257 | """ | |
256 | self._post_processor_class_changed(None, self.post_processor_class, |
|
258 | self._post_processor_class_changed(None, self.post_processor_class, | |
257 | self.post_processor_class) |
|
259 | self.post_processor_class) | |
258 | if self.post_processor_factory: |
|
260 | if self.post_processor_factory: | |
259 | self.post_processor = self.post_processor_factory(parent=self) |
|
261 | self.post_processor = self.post_processor_factory(parent=self) | |
260 |
|
262 | |||
261 | def start(self): |
|
263 | def start(self): | |
262 | """ |
|
264 | """ | |
263 | Ran after initialization completed |
|
265 | Ran after initialization completed | |
264 | """ |
|
266 | """ | |
265 | super(NbConvertApp, self).start() |
|
267 | super(NbConvertApp, self).start() | |
266 | self.convert_notebooks() |
|
268 | self.convert_notebooks() | |
267 |
|
269 | |||
268 | def convert_notebooks(self): |
|
270 | def convert_notebooks(self): | |
269 | """ |
|
271 | """ | |
270 | Convert the notebooks in the self.notebook traitlet |
|
272 | Convert the notebooks in the self.notebook traitlet | |
271 | """ |
|
273 | """ | |
272 | # Export each notebook |
|
274 | # Export each notebook | |
273 | conversion_success = 0 |
|
275 | conversion_success = 0 | |
274 |
|
276 | |||
275 | if self.output_base != '' and len(self.notebooks) > 1: |
|
277 | if self.output_base != '' and len(self.notebooks) > 1: | |
276 | self.log.error( |
|
278 | self.log.error( | |
277 | """UsageError: --output flag or `NbConvertApp.output_base` config option |
|
279 | """UsageError: --output flag or `NbConvertApp.output_base` config option | |
278 | cannot be used when converting multiple notebooks. |
|
280 | cannot be used when converting multiple notebooks. | |
279 | """) |
|
281 | """) | |
280 | self.exit(1) |
|
282 | self.exit(1) | |
281 |
|
283 | |||
282 | exporter = exporter_map[self.export_format](config=self.config) |
|
284 | exporter = exporter_map[self.export_format](config=self.config) | |
283 |
|
285 | |||
284 | for notebook_filename in self.notebooks: |
|
286 | for notebook_filename in self.notebooks: | |
285 | self.log.info("Converting notebook %s to %s", notebook_filename, self.export_format) |
|
287 | self.log.info("Converting notebook %s to %s", notebook_filename, self.export_format) | |
286 |
|
288 | |||
287 | # Get a unique key for the notebook and set it in the resources object. |
|
289 | # Get a unique key for the notebook and set it in the resources object. | |
288 | basename = os.path.basename(notebook_filename) |
|
290 | basename = os.path.basename(notebook_filename) | |
289 | notebook_name = basename[:basename.rfind('.')] |
|
291 | notebook_name = basename[:basename.rfind('.')] | |
290 | if self.output_base: |
|
292 | if self.output_base: | |
291 | notebook_name = self.output_base |
|
293 | notebook_name = self.output_base | |
292 | resources = {} |
|
294 | resources = {} | |
293 | resources['unique_key'] = notebook_name |
|
295 | resources['unique_key'] = notebook_name | |
294 | resources['output_files_dir'] = '%s_files' % notebook_name |
|
296 | resources['output_files_dir'] = '%s_files' % notebook_name | |
295 | self.log.info("Support files will be in %s", os.path.join(resources['output_files_dir'], '')) |
|
297 | self.log.info("Support files will be in %s", os.path.join(resources['output_files_dir'], '')) | |
296 |
|
298 | |||
297 | # Try to export |
|
299 | # Try to export | |
298 | try: |
|
300 | try: | |
299 | output, resources = exporter.from_filename(notebook_filename, resources=resources) |
|
301 | output, resources = exporter.from_filename(notebook_filename, resources=resources) | |
300 | except ConversionException as e: |
|
302 | except ConversionException as e: | |
301 | self.log.error("Error while converting '%s'", notebook_filename, |
|
303 | self.log.error("Error while converting '%s'", notebook_filename, | |
302 | exc_info=True) |
|
304 | exc_info=True) | |
303 | self.exit(1) |
|
305 | self.exit(1) | |
304 | else: |
|
306 | else: | |
305 | write_resultes = self.writer.write(output, resources, notebook_name=notebook_name) |
|
307 | write_resultes = self.writer.write(output, resources, notebook_name=notebook_name) | |
306 |
|
308 | |||
307 | #Post-process if post processor has been defined. |
|
309 | #Post-process if post processor has been defined. | |
308 | if hasattr(self, 'post_processor') and self.post_processor: |
|
310 | if hasattr(self, 'post_processor') and self.post_processor: | |
309 | self.post_processor(write_resultes) |
|
311 | self.post_processor(write_resultes) | |
310 | conversion_success += 1 |
|
312 | conversion_success += 1 | |
311 |
|
313 | |||
312 | # If nothing was converted successfully, help the user. |
|
314 | # If nothing was converted successfully, help the user. | |
313 | if conversion_success == 0: |
|
315 | if conversion_success == 0: | |
314 | self.print_help() |
|
316 | self.print_help() | |
315 | sys.exit(-1) |
|
317 | sys.exit(-1) | |
316 |
|
318 | |||
317 | #----------------------------------------------------------------------------- |
|
319 | #----------------------------------------------------------------------------- | |
318 | # Main entry point |
|
320 | # Main entry point | |
319 | #----------------------------------------------------------------------------- |
|
321 | #----------------------------------------------------------------------------- | |
320 |
|
322 | |||
321 | launch_new_instance = NbConvertApp.launch_instance |
|
323 | launch_new_instance = NbConvertApp.launch_instance |
@@ -1,185 +1,185 | |||||
1 | {%- extends 'reveal_internals/slides.tpl' -%} |
|
1 | {%- extends 'reveal_internals/slides.tpl' -%} | |
2 |
|
2 | |||
3 |
|
3 | |||
4 | {% block header %} |
|
4 | {% block header %} | |
5 | <!DOCTYPE html> |
|
5 | <!DOCTYPE html> | |
6 | <html> |
|
6 | <html> | |
7 | <head> |
|
7 | <head> | |
8 |
|
8 | |||
9 | <meta charset="utf-8" /> |
|
9 | <meta charset="utf-8" /> | |
10 | <meta http-equiv="X-UA-Compatible" content="chrome=1"> |
|
10 | <meta http-equiv="X-UA-Compatible" content="chrome=1"> | |
11 |
|
11 | |||
12 | <meta name="apple-mobile-web-app-capable" content="yes" /> |
|
12 | <meta name="apple-mobile-web-app-capable" content="yes" /> | |
13 | <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" /> |
|
13 | <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" /> | |
14 |
|
14 | |||
15 | <!-- General and theme style sheets --> |
|
15 | <!-- General and theme style sheets --> | |
16 | <link rel="stylesheet" href="{{resources.reveal.url_prefix}}/css/reveal.css"> |
|
16 | <link rel="stylesheet" href="{{resources.reveal.url_prefix}}/css/reveal.css"> | |
17 | <link rel="stylesheet" href="{{resources.reveal.url_prefix}}/css/theme/simple.css" id="theme"> |
|
17 | <link rel="stylesheet" href="{{resources.reveal.url_prefix}}/css/theme/simple.css" id="theme"> | |
18 |
|
18 | |||
19 | <!-- For syntax highlighting --> |
|
19 | <!-- For syntax highlighting --> | |
20 | <link rel="stylesheet" href="{{resources.reveal.url_prefix}}/lib/css/zenburn.css"> |
|
20 | <link rel="stylesheet" href="{{resources.reveal.url_prefix}}/lib/css/zenburn.css"> | |
21 |
|
21 | |||
22 | <!-- If the query includes 'print-pdf', use the PDF print sheet --> |
|
22 | <!-- If the query includes 'print-pdf', use the PDF print sheet --> | |
23 | <script> |
|
23 | <script> | |
24 | document.write( '<link rel="stylesheet" href="{{resources.reveal.url_prefix}}/css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' ); |
|
24 | document.write( '<link rel="stylesheet" href="{{resources.reveal.url_prefix}}/css/print/' + ( window.location.search.match( /print-pdf/gi ) ? 'pdf' : 'paper' ) + '.css" type="text/css" media="print">' ); | |
25 | </script> |
|
25 | </script> | |
26 |
|
26 | |||
27 | <!--[if lt IE 9]> |
|
27 | <!--[if lt IE 9]> | |
28 | <script src="{{resources.reveal.url_prefix}}/lib/js/html5shiv.js"></script> |
|
28 | <script src="{{resources.reveal.url_prefix}}/lib/js/html5shiv.js"></script> | |
29 | <![endif]--> |
|
29 | <![endif]--> | |
30 |
|
30 | |||
31 | {% for css in resources.inlining.css -%} |
|
31 | {% for css in resources.inlining.css -%} | |
32 | <style type="text/css"> |
|
32 | <style type="text/css"> | |
33 | {{ css }} |
|
33 | {{ css }} | |
34 | </style> |
|
34 | </style> | |
35 | {% endfor %} |
|
35 | {% endfor %} | |
36 |
|
36 | |||
37 | <style type="text/css"> |
|
37 | <style type="text/css"> | |
38 | /* Overrides of notebook CSS for static HTML export */ |
|
38 | /* Overrides of notebook CSS for static HTML export */ | |
39 | .reveal { |
|
39 | .reveal { | |
40 | font-size: 20px; |
|
40 | font-size: 20px; | |
41 | overflow-y: auto; |
|
41 | overflow-y: auto; | |
42 | overflow-x: hidden; |
|
42 | overflow-x: hidden; | |
43 | } |
|
43 | } | |
44 | .reveal pre { |
|
44 | .reveal pre { | |
45 | width: 95%; |
|
45 | width: 95%; | |
46 | padding: 0.4em; |
|
46 | padding: 0.4em; | |
47 | margin: 0px; |
|
47 | margin: 0px; | |
48 | font-family: monospace, sans-serif; |
|
48 | font-family: monospace, sans-serif; | |
49 | font-size: 80%; |
|
49 | font-size: 80%; | |
50 | box-shadow: 0px 0px 0px rgba(0, 0, 0, 0); |
|
50 | box-shadow: 0px 0px 0px rgba(0, 0, 0, 0); | |
51 | } |
|
51 | } | |
52 | .reveal section img { |
|
52 | .reveal section img { | |
53 | border: 0px solid black; |
|
53 | border: 0px solid black; | |
54 | box-shadow: 0 0 10px rgba(0, 0, 0, 0); |
|
54 | box-shadow: 0 0 10px rgba(0, 0, 0, 0); | |
55 | } |
|
55 | } | |
56 | .reveal .slides { |
|
56 | .reveal .slides { | |
57 | text-align: left; |
|
57 | text-align: left; | |
58 | } |
|
58 | } | |
59 | .reveal.fade { |
|
59 | .reveal.fade { | |
60 | opacity: 1; |
|
60 | opacity: 1; | |
61 | } |
|
61 | } | |
62 | div.input_area { |
|
62 | div.input_area { | |
63 | padding: 0.06em; |
|
63 | padding: 0.06em; | |
64 | } |
|
64 | } | |
65 | div.code_cell { |
|
65 | div.code_cell { | |
66 | background-color: transparent; |
|
66 | background-color: transparent; | |
67 | } |
|
67 | } | |
68 | div.prompt { |
|
68 | div.prompt { | |
69 | width: 11ex; |
|
69 | width: 11ex; | |
70 | padding: 0.4em; |
|
70 | padding: 0.4em; | |
71 | margin: 0px; |
|
71 | margin: 0px; | |
72 | font-family: monospace, sans-serif; |
|
72 | font-family: monospace, sans-serif; | |
73 | font-size: 80%; |
|
73 | font-size: 80%; | |
74 | text-align: right; |
|
74 | text-align: right; | |
75 | } |
|
75 | } | |
76 | div.output_area pre { |
|
76 | div.output_area pre { | |
77 | font-family: monospace, sans-serif; |
|
77 | font-family: monospace, sans-serif; | |
78 | font-size: 80%; |
|
78 | font-size: 80%; | |
79 | } |
|
79 | } | |
80 | div.output_prompt { |
|
80 | div.output_prompt { | |
81 | /* 5px right shift to account for margin in parent container */ |
|
81 | /* 5px right shift to account for margin in parent container */ | |
82 | margin: 5px 5px 0 0; |
|
82 | margin: 5px 5px 0 0; | |
83 | } |
|
83 | } | |
84 | .rendered_html p { |
|
84 | .rendered_html p { | |
85 | text-align: inherit; |
|
85 | text-align: inherit; | |
86 | } |
|
86 | } | |
87 | </style> |
|
87 | </style> | |
88 |
|
88 | |||
89 | <!-- Custom stylesheet, it must be in the same directory as the html file --> |
|
89 | <!-- Custom stylesheet, it must be in the same directory as the html file --> | |
90 | <link rel="stylesheet" href="custom.css"> |
|
90 | <link rel="stylesheet" href="custom.css"> | |
91 |
|
91 | |||
92 | </head> |
|
92 | </head> | |
93 | {% endblock header%} |
|
93 | {% endblock header%} | |
94 |
|
94 | |||
95 |
|
95 | |||
96 | {% block body %} |
|
96 | {% block body %} | |
97 | <body> |
|
97 | <body> | |
98 | <div class="reveal"> |
|
98 | <div class="reveal"> | |
99 | <div class="slides"> |
|
99 | <div class="slides"> | |
100 | {{ super() }} |
|
100 | {{ super() }} | |
101 | </div> |
|
101 | </div> | |
102 | </div> |
|
102 | </div> | |
103 |
|
103 | |||
104 | <!-- |
|
104 | <!-- | |
105 | Uncomment the following block and the addthis_widget.js (see below inside dependencies) |
|
105 | Uncomment the following block and the addthis_widget.js (see below inside dependencies) | |
106 | to get enable social buttons. |
|
106 | to get enable social buttons. | |
107 | --> |
|
107 | --> | |
108 |
|
108 | |||
109 | <!-- |
|
109 | <!-- | |
110 | <div class="addthis_toolbox addthis_floating_style addthis_32x32_style" style="left:20px;top:20px;"> |
|
110 | <div class="addthis_toolbox addthis_floating_style addthis_32x32_style" style="left:20px;top:20px;"> | |
111 | <a class="addthis_button_twitter"></a> |
|
111 | <a class="addthis_button_twitter"></a> | |
112 | <a class="addthis_button_google_plusone_share"></a> |
|
112 | <a class="addthis_button_google_plusone_share"></a> | |
113 | <a class="addthis_button_linkedin"></a> |
|
113 | <a class="addthis_button_linkedin"></a> | |
114 | <a class="addthis_button_facebook"></a> |
|
114 | <a class="addthis_button_facebook"></a> | |
115 | <a class="addthis_button_more"></a> |
|
115 | <a class="addthis_button_more"></a> | |
116 | </div> |
|
116 | </div> | |
117 | --> |
|
117 | --> | |
118 |
|
118 | |||
119 | <script src="{{resources.reveal.url_prefix}}/lib/js/head.min.js"></script> |
|
119 | <script src="{{resources.reveal.url_prefix}}/lib/js/head.min.js"></script> | |
120 |
|
120 | |||
121 | <script src="{{resources.reveal.url_prefix}}/js/reveal.js"></script> |
|
121 | <script src="{{resources.reveal.url_prefix}}/js/reveal.js"></script> | |
122 |
|
122 | |||
123 | <script> |
|
123 | <script> | |
124 |
|
124 | |||
125 | // Full list of configuration options available here: https://github.com/hakimel/reveal.js#configuration |
|
125 | // Full list of configuration options available here: https://github.com/hakimel/reveal.js#configuration | |
126 | Reveal.initialize({ |
|
126 | Reveal.initialize({ | |
127 | controls: true, |
|
127 | controls: true, | |
128 | progress: true, |
|
128 | progress: true, | |
129 | history: true, |
|
129 | history: true, | |
130 |
|
130 | |||
131 | theme: Reveal.getQueryHash().theme, // available themes are in /css/theme |
|
131 | theme: Reveal.getQueryHash().theme, // available themes are in /css/theme | |
132 | transition: Reveal.getQueryHash().transition || 'linear', // default/cube/page/concave/zoom/linear/none |
|
132 | transition: Reveal.getQueryHash().transition || 'linear', // default/cube/page/concave/zoom/linear/none | |
133 |
|
133 | |||
134 | // Optional libraries used to extend on reveal.js |
|
134 | // Optional libraries used to extend on reveal.js | |
135 | dependencies: [ |
|
135 | dependencies: [ | |
136 | { src: "{{resources.reveal.url_prefix}}/lib/js/classList.js", condition: function() { return !document.body.classList; } }, |
|
136 | { src: "{{resources.reveal.url_prefix}}/lib/js/classList.js", condition: function() { return !document.body.classList; } }, | |
137 | { src: "{{resources.reveal.url_prefix}}/plugin/highlight/highlight.js", async: true, callback: function() { hljs.initHighlightingOnLoad(); } }, |
|
137 | { src: "{{resources.reveal.url_prefix}}/plugin/highlight/highlight.js", async: true, callback: function() { hljs.initHighlightingOnLoad(); } }, | |
138 |
{ src: "{{resources.reveal. |
|
138 | { src: "{{resources.reveal.notes_prefix}}/plugin/notes/notes.js", async: true, condition: function() { return !!document.body.classList; } } | |
139 | // { src: 'http://s7.addthis.com/js/300/addthis_widget.js', async: true}, |
|
139 | // { src: 'http://s7.addthis.com/js/300/addthis_widget.js', async: true}, | |
140 | ] |
|
140 | ] | |
141 | }); |
|
141 | }); | |
142 | </script> |
|
142 | </script> | |
143 |
|
143 | |||
144 | <!-- MathJax configuration --> |
|
144 | <!-- MathJax configuration --> | |
145 | <script type="text/x-mathjax-config"> |
|
145 | <script type="text/x-mathjax-config"> | |
146 | MathJax.Hub.Config({ |
|
146 | MathJax.Hub.Config({ | |
147 | tex2jax: { |
|
147 | tex2jax: { | |
148 | inlineMath: [ ['$','$'], ["\\(","\\)"] ], |
|
148 | inlineMath: [ ['$','$'], ["\\(","\\)"] ], | |
149 | displayMath: [ ['$$','$$'], ["\\[","\\]"] ] |
|
149 | displayMath: [ ['$$','$$'], ["\\[","\\]"] ] | |
150 | }, |
|
150 | }, | |
151 | displayAlign: 'left', // Change this to 'center' to center equations. |
|
151 | displayAlign: 'left', // Change this to 'center' to center equations. | |
152 | "HTML-CSS": { |
|
152 | "HTML-CSS": { | |
153 | styles: {'.MathJax_Display': {"margin": 0}} |
|
153 | styles: {'.MathJax_Display': {"margin": 0}} | |
154 | } |
|
154 | } | |
155 | }); |
|
155 | }); | |
156 | </script> |
|
156 | </script> | |
157 | <!-- End of mathjax configuration --> |
|
157 | <!-- End of mathjax configuration --> | |
158 |
|
158 | |||
159 | <script> |
|
159 | <script> | |
160 | // We wait for the onload function to load MathJax after the page is completely loaded. |
|
160 | // We wait for the onload function to load MathJax after the page is completely loaded. | |
161 | // MathJax is loaded 1 unit of time after the page is ready. |
|
161 | // MathJax is loaded 1 unit of time after the page is ready. | |
162 | // This hack prevent problems when you load multiple js files (i.e. social button from addthis). |
|
162 | // This hack prevent problems when you load multiple js files (i.e. social button from addthis). | |
163 | // |
|
163 | // | |
164 | window.onload = function () { |
|
164 | window.onload = function () { | |
165 | setTimeout(function () { |
|
165 | setTimeout(function () { | |
166 | var script = document.createElement("script"); |
|
166 | var script = document.createElement("script"); | |
167 | script.type = "text/javascript"; |
|
167 | script.type = "text/javascript"; | |
168 | script.src = "https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"; |
|
168 | script.src = "https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"; | |
169 | document.getElementsByTagName("head")[0].appendChild(script); |
|
169 | document.getElementsByTagName("head")[0].appendChild(script); | |
170 | },1) |
|
170 | },1) | |
171 | } |
|
171 | } | |
172 | </script> |
|
172 | </script> | |
173 |
|
173 | |||
174 | <script> |
|
174 | <script> | |
175 | Reveal.addEventListener( 'slidechanged', function( event ) { |
|
175 | Reveal.addEventListener( 'slidechanged', function( event ) { | |
176 | MathJax.Hub.Rerender(event.currentSlide); |
|
176 | MathJax.Hub.Rerender(event.currentSlide); | |
177 | }); |
|
177 | }); | |
178 | </script> |
|
178 | </script> | |
179 |
|
179 | |||
180 | </body> |
|
180 | </body> | |
181 | {% endblock body %} |
|
181 | {% endblock body %} | |
182 |
|
182 | |||
183 | {% block footer %} |
|
183 | {% block footer %} | |
184 | </html> |
|
184 | </html> | |
185 | {% endblock footer %} No newline at end of file |
|
185 | {% endblock footer %} |
@@ -1,61 +1,94 | |||||
1 | """Module that pre-processes the notebook for export via Reveal. |
|
1 | """Module that pre-processes the notebook for export via Reveal. | |
2 | """ |
|
2 | """ | |
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 | # Copyright (c) 2013, the IPython Development Team. |
|
4 | # Copyright (c) 2013, the IPython Development Team. | |
5 | # |
|
5 | # | |
6 | # Distributed under the terms of the Modified BSD License. |
|
6 | # Distributed under the terms of the Modified BSD License. | |
7 | # |
|
7 | # | |
8 | # The full license is in the file COPYING.txt, distributed with this software. |
|
8 | # The full license is in the file COPYING.txt, distributed with this software. | |
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 |
|
10 | |||
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Imports |
|
12 | # Imports | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
|
15 | import os | |||
|
16 | import urllib2 | |||
|
17 | ||||
15 | from .base import Transformer |
|
18 | from .base import Transformer | |
16 | from IPython.utils.traitlets import Unicode |
|
19 | from IPython.utils.traitlets import Unicode, Bool | |
17 |
|
20 | |||
18 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
19 | # Classes and functions |
|
22 | # Classes and functions | |
20 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
21 |
|
24 | |||
22 | class RevealHelpTransformer(Transformer): |
|
25 | class RevealHelpTransformer(Transformer): | |
23 |
|
26 | |||
24 | url_prefix = Unicode('//cdn.jsdelivr.net/reveal.js/2.4.0', |
|
27 | url_prefix = Unicode('//cdn.jsdelivr.net/reveal.js/2.4.0', | |
25 | config=True, |
|
28 | config=True, | |
26 | help="""If you want to use a local reveal.js library, |
|
29 | help="""If you want to use a local reveal.js library, | |
27 |
use 'url_prefix':'reveal.js' in your config object |
|
30 | use 'url_prefix':'reveal.js' in your config object or | |
|
31 | --local reveal.js in command line.""") | |||
|
32 | ||||
|
33 | speaker_notes = Bool(False, config=True, help=""" | |||
|
34 | If you want to use the speaker notes set speaker_notes:True | |||
|
35 | in your config object or --notes True in command line.""") | |||
28 |
|
36 | |||
29 | def call(self, nb, resources): |
|
37 | def call(self, nb, resources): | |
30 | """ |
|
38 | """ | |
31 | Called once to 'transform' contents of the notebook. |
|
39 | Called once to 'transform' contents of the notebook. | |
32 |
|
40 | |||
33 | Parameters |
|
41 | Parameters | |
34 | ---------- |
|
42 | ---------- | |
35 | nb : NotebookNode |
|
43 | nb : NotebookNode | |
36 | Notebook being converted |
|
44 | Notebook being converted | |
37 | resources : dictionary |
|
45 | resources : dictionary | |
38 | Additional resources used in the conversion process. Allows |
|
46 | Additional resources used in the conversion process. Allows | |
39 | transformers to pass variables into the Jinja engine. |
|
47 | transformers to pass variables into the Jinja engine. | |
40 | """ |
|
48 | """ | |
41 |
|
49 | |||
42 | for worksheet in nb.worksheets : |
|
50 | for worksheet in nb.worksheets : | |
43 | for index, cell in enumerate(worksheet.cells): |
|
51 | for index, cell in enumerate(worksheet.cells): | |
44 |
|
52 | |||
45 | #Make sure the cell has slideshow metadata. |
|
53 | #Make sure the cell has slideshow metadata. | |
46 | cell.metadata.align_type = cell.get('metadata', {}).get('slideshow', {}).get('align_type', 'Left') |
|
54 | cell.metadata.align_type = cell.get('metadata', {}).get('slideshow', {}).get('align_type', 'Left') | |
47 | cell.metadata.slide_type = cell.get('metadata', {}).get('slideshow', {}).get('slide_type', '-') |
|
55 | cell.metadata.slide_type = cell.get('metadata', {}).get('slideshow', {}).get('slide_type', '-') | |
48 |
|
56 | |||
49 | #Get the slide type. If type is start of subslide or slide, |
|
57 | #Get the slide type. If type is start of subslide or slide, | |
50 | #end the last subslide/slide. |
|
58 | #end the last subslide/slide. | |
51 | if cell.metadata.slide_type in ['slide']: |
|
59 | if cell.metadata.slide_type in ['slide']: | |
52 | worksheet.cells[index - 1].metadata.slide_helper = 'slide_end' |
|
60 | worksheet.cells[index - 1].metadata.slide_helper = 'slide_end' | |
53 | if cell.metadata.slide_type in ['subslide']: |
|
61 | if cell.metadata.slide_type in ['subslide']: | |
54 | worksheet.cells[index - 1].metadata.slide_helper = 'subslide_end' |
|
62 | worksheet.cells[index - 1].metadata.slide_helper = 'subslide_end' | |
55 |
|
63 | |||
56 |
|
64 | |||
57 | if 'reveal' not in resources: |
|
65 | if 'reveal' not in resources: | |
58 | resources['reveal'] = {} |
|
66 | resources['reveal'] = {} | |
59 | resources['reveal']['url_prefix'] = self.url_prefix |
|
67 | resources['reveal']['url_prefix'] = self.url_prefix | |
|
68 | resources['reveal']['notes_prefix'] = self.url_prefix | |||
|
69 | ||||
|
70 | cdn = 'http://cdn.jsdelivr.net/reveal.js/2.4.0' | |||
|
71 | local = 'local' | |||
|
72 | html_path = 'plugin/notes/notes.html' | |||
|
73 | js_path = 'plugin/notes/notes.js' | |||
|
74 | ||||
|
75 | html_infile = os.path.join(cdn, html_path) | |||
|
76 | js_infile = os.path.join(cdn, js_path) | |||
|
77 | html_outfile = os.path.join(local, html_path) | |||
|
78 | js_outfile = os.path.join(local, js_path) | |||
|
79 | ||||
|
80 | if self.speaker_notes: | |||
|
81 | if 'outputs' not in resources: | |||
|
82 | resources['outputs'] = {} | |||
|
83 | resources['outputs'][html_outfile] = self.notes_helper(html_infile) | |||
|
84 | resources['outputs'][js_outfile] = self.notes_helper(js_infile) | |||
|
85 | resources['reveal']['notes_prefix'] = local | |||
60 |
|
86 | |||
61 | return nb, resources |
|
87 | return nb, resources | |
|
88 | ||||
|
89 | def notes_helper(self, infile): | |||
|
90 | """Helper function to get the content from an url.""" | |||
|
91 | ||||
|
92 | content = urllib2.urlopen(infile).read() | |||
|
93 | ||||
|
94 | return content |
@@ -1,212 +1,216 | |||||
1 | .. _nbconvert: |
|
1 | .. _nbconvert: | |
2 |
|
2 | |||
3 | Converting notebooks to other formats |
|
3 | Converting notebooks to other formats | |
4 | ===================================== |
|
4 | ===================================== | |
5 |
|
5 | |||
6 | Newly added in the 1.0 release of IPython is the ``nbconvert`` tool, which |
|
6 | Newly added in the 1.0 release of IPython is the ``nbconvert`` tool, which | |
7 | allows you to convert an ``.ipynb`` notebook document file into various static |
|
7 | allows you to convert an ``.ipynb`` notebook document file into various static | |
8 | formats. |
|
8 | formats. | |
9 |
|
9 | |||
10 | Currently, ``nbconvert`` is provided as a command line tool, run as a script |
|
10 | Currently, ``nbconvert`` is provided as a command line tool, run as a script | |
11 | using IPython. A direct export capability from within the |
|
11 | using IPython. A direct export capability from within the | |
12 | IPython Notebook web app is planned. |
|
12 | IPython Notebook web app is planned. | |
13 |
|
13 | |||
14 | The command-line syntax to run the ``nbconvert`` script is:: |
|
14 | The command-line syntax to run the ``nbconvert`` script is:: | |
15 |
|
15 | |||
16 | $ ipython nbconvert --to FORMAT notebook.ipynb |
|
16 | $ ipython nbconvert --to FORMAT notebook.ipynb | |
17 |
|
17 | |||
18 | This will convert the IPython document file ``notebook.ipynb`` into the output |
|
18 | This will convert the IPython document file ``notebook.ipynb`` into the output | |
19 | format given by the ``FORMAT`` string. |
|
19 | format given by the ``FORMAT`` string. | |
20 |
|
20 | |||
21 | The default output format is html, for which the ``--to`` argument may be |
|
21 | The default output format is html, for which the ``--to`` argument may be | |
22 | omitted:: |
|
22 | omitted:: | |
23 |
|
23 | |||
24 | $ ipython nbconvert notebook.ipynb |
|
24 | $ ipython nbconvert notebook.ipynb | |
25 |
|
25 | |||
26 | IPython provides a few templates for some output formats, and these can be |
|
26 | IPython provides a few templates for some output formats, and these can be | |
27 | specified via an additional ``--template`` argument. |
|
27 | specified via an additional ``--template`` argument. | |
28 |
|
28 | |||
29 | The currently supported export formats are: |
|
29 | The currently supported export formats are: | |
30 |
|
30 | |||
31 | * ``--to html`` |
|
31 | * ``--to html`` | |
32 |
|
32 | |||
33 | - ``--template full`` (default) |
|
33 | - ``--template full`` (default) | |
34 |
|
34 | |||
35 | A full static HTML render of the notebook. |
|
35 | A full static HTML render of the notebook. | |
36 | This looks very similar to the interactive view. |
|
36 | This looks very similar to the interactive view. | |
37 |
|
37 | |||
38 | - ``--template basic`` |
|
38 | - ``--template basic`` | |
39 |
|
39 | |||
40 | Simplified HTML, useful for embedding in webpages, blogs, etc. |
|
40 | Simplified HTML, useful for embedding in webpages, blogs, etc. | |
41 | This excludes HTML headers. |
|
41 | This excludes HTML headers. | |
42 |
|
42 | |||
43 | * ``--to latex`` |
|
43 | * ``--to latex`` | |
44 |
|
44 | |||
45 | Latex export. This generates ``NOTEBOOK_NAME.tex`` file, |
|
45 | Latex export. This generates ``NOTEBOOK_NAME.tex`` file, | |
46 | ready for export. You can automatically run latex on it to generate a PDF |
|
46 | ready for export. You can automatically run latex on it to generate a PDF | |
47 | by adding ``--post PDF``. |
|
47 | by adding ``--post PDF``. | |
48 |
|
48 | |||
49 | - ``--template article`` (default) |
|
49 | - ``--template article`` (default) | |
50 |
|
50 | |||
51 | Latex article, derived from Sphinx's howto template. |
|
51 | Latex article, derived from Sphinx's howto template. | |
52 |
|
52 | |||
53 | - ``--template book`` |
|
53 | - ``--template book`` | |
54 |
|
54 | |||
55 | Latex book, derived from Sphinx's manual template. |
|
55 | Latex book, derived from Sphinx's manual template. | |
56 |
|
56 | |||
57 | - ``--template basic`` |
|
57 | - ``--template basic`` | |
58 |
|
58 | |||
59 | Very basic latex output - mainly meant as a starting point for custom templates. |
|
59 | Very basic latex output - mainly meant as a starting point for custom templates. | |
60 |
|
60 | |||
61 | * ``--to slides`` |
|
61 | * ``--to slides`` | |
62 |
|
62 | |||
63 | This generates a Reveal.js HTML slideshow. |
|
63 | This generates a Reveal.js HTML slideshow. | |
64 | It must be served by an HTTP server. The easiest way to get this is to add |
|
64 | It must be served by an HTTP server. The easiest way to get this is to add | |
65 | ``--post serve`` on the command-line. |
|
65 | ``--post serve`` on the command-line. | |
|
66 | If you want to use the speaker notes plugin, just add | |||
|
67 | ``--notes True`` on the command-line. | |||
|
68 | For low connectivity environments, you can use a local copy of the reveal.js library, just add | |||
|
69 | ``--local reveal.js`` on the command-line, and do not forget to move your downloaded ``reveal.js`` library to the same folder where your slides are located. | |||
66 |
|
70 | |||
67 | * ``--to markdown`` |
|
71 | * ``--to markdown`` | |
68 |
|
72 | |||
69 | Simple markdown output. Markdown cells are unaffected, |
|
73 | Simple markdown output. Markdown cells are unaffected, | |
70 | and code cells are placed in triple-backtick (``\`\`\```) blocks. |
|
74 | and code cells are placed in triple-backtick (``\`\`\```) blocks. | |
71 |
|
75 | |||
72 | * ``--to rst`` |
|
76 | * ``--to rst`` | |
73 |
|
77 | |||
74 | Basic reStructuredText output. Useful as a starting point for embedding notebooks |
|
78 | Basic reStructuredText output. Useful as a starting point for embedding notebooks | |
75 | in Sphinx docs. |
|
79 | in Sphinx docs. | |
76 |
|
80 | |||
77 | * ``--to python`` |
|
81 | * ``--to python`` | |
78 |
|
82 | |||
79 | Convert a notebook to an executable Python script. |
|
83 | Convert a notebook to an executable Python script. | |
80 | This is the simplest way to get a Python script out of a notebook. |
|
84 | This is the simplest way to get a Python script out of a notebook. | |
81 | If there were any magics in the notebook, this may only be executable from |
|
85 | If there were any magics in the notebook, this may only be executable from | |
82 | an IPython session. |
|
86 | an IPython session. | |
83 |
|
87 | |||
84 | .. note:: |
|
88 | .. note:: | |
85 |
|
89 | |||
86 | nbconvert uses pandoc_ to convert between various markup languages, |
|
90 | nbconvert uses pandoc_ to convert between various markup languages, | |
87 | so pandoc is a dependency of most nbconvert transforms, |
|
91 | so pandoc is a dependency of most nbconvert transforms, | |
88 | excluding Markdown and Python. |
|
92 | excluding Markdown and Python. | |
89 |
|
93 | |||
90 | .. _pandoc: http://johnmacfarlane.net/pandoc/ |
|
94 | .. _pandoc: http://johnmacfarlane.net/pandoc/ | |
91 |
|
95 | |||
92 | The output file created by ``nbconvert`` will have the same base name as |
|
96 | The output file created by ``nbconvert`` will have the same base name as | |
93 | the notebook and will be placed in the current working directory. Any |
|
97 | the notebook and will be placed in the current working directory. Any | |
94 | supporting files (graphics, etc) will be placed in a new directory with the |
|
98 | supporting files (graphics, etc) will be placed in a new directory with the | |
95 | same base name as the notebook, suffixed with ``_files``:: |
|
99 | same base name as the notebook, suffixed with ``_files``:: | |
96 |
|
100 | |||
97 | $ ipython nbconvert notebook.ipynb |
|
101 | $ ipython nbconvert notebook.ipynb | |
98 | $ ls |
|
102 | $ ls | |
99 | notebook.ipynb notebook.html notebook_files/ |
|
103 | notebook.ipynb notebook.html notebook_files/ | |
100 |
|
104 | |||
101 | For simple single-file output, such as html, markdown, etc., |
|
105 | For simple single-file output, such as html, markdown, etc., | |
102 | the output may be sent to standard output with:: |
|
106 | the output may be sent to standard output with:: | |
103 |
|
107 | |||
104 | $ ipython nbconvert --to markdown notebook.ipynb --stdout |
|
108 | $ ipython nbconvert --to markdown notebook.ipynb --stdout | |
105 |
|
109 | |||
106 | Multiple notebooks can be specified from the command line:: |
|
110 | Multiple notebooks can be specified from the command line:: | |
107 |
|
111 | |||
108 | $ ipython nbconvert notebook*.ipynb |
|
112 | $ ipython nbconvert notebook*.ipynb | |
109 | $ ipython nbconvert notebook1.ipynb notebook2.ipynb |
|
113 | $ ipython nbconvert notebook1.ipynb notebook2.ipynb | |
110 |
|
114 | |||
111 | or via a list in a configuration file, say ``mycfg.py``, containing the text:: |
|
115 | or via a list in a configuration file, say ``mycfg.py``, containing the text:: | |
112 |
|
116 | |||
113 | c = get_config() |
|
117 | c = get_config() | |
114 | c.NbConvertApp.notebooks = ["notebook1.ipynb", "notebook2.ipynb"] |
|
118 | c.NbConvertApp.notebooks = ["notebook1.ipynb", "notebook2.ipynb"] | |
115 |
|
119 | |||
116 | and using the command:: |
|
120 | and using the command:: | |
117 |
|
121 | |||
118 | $ ipython nbconvert --config mycfg.py |
|
122 | $ ipython nbconvert --config mycfg.py | |
119 |
|
123 | |||
120 |
|
124 | |||
121 | .. _notebook_format: |
|
125 | .. _notebook_format: | |
122 |
|
126 | |||
123 | Notebook JSON file format |
|
127 | Notebook JSON file format | |
124 | ------------------------- |
|
128 | ------------------------- | |
125 |
|
129 | |||
126 | Notebook documents are JSON files with an ``.ipynb`` extension, formatted |
|
130 | Notebook documents are JSON files with an ``.ipynb`` extension, formatted | |
127 | as legibly as possible with minimal extra indentation and cell content broken |
|
131 | as legibly as possible with minimal extra indentation and cell content broken | |
128 | across lines to make them reasonably friendly to use in version-control |
|
132 | across lines to make them reasonably friendly to use in version-control | |
129 | workflows. You should be very careful if you ever manually edit this JSON |
|
133 | workflows. You should be very careful if you ever manually edit this JSON | |
130 | data, as it is extremely easy to corrupt its internal structure and make the |
|
134 | data, as it is extremely easy to corrupt its internal structure and make the | |
131 | file impossible to load. In general, you should consider the notebook as a |
|
135 | file impossible to load. In general, you should consider the notebook as a | |
132 | file meant only to be edited by the IPython Notebook app itself, not for |
|
136 | file meant only to be edited by the IPython Notebook app itself, not for | |
133 | hand-editing. |
|
137 | hand-editing. | |
134 |
|
138 | |||
135 | .. note:: |
|
139 | .. note:: | |
136 |
|
140 | |||
137 | Binary data such as figures are also saved directly in the JSON file. |
|
141 | Binary data such as figures are also saved directly in the JSON file. | |
138 | This provides convenient single-file portability, but means that the |
|
142 | This provides convenient single-file portability, but means that the | |
139 | files can be large; a ``diff`` of binary data is also not very |
|
143 | files can be large; a ``diff`` of binary data is also not very | |
140 | meaningful. Since the binary blobs are encoded in a single line, they |
|
144 | meaningful. Since the binary blobs are encoded in a single line, they | |
141 | affect only one line of the ``diff`` output, but they are typically very |
|
145 | affect only one line of the ``diff`` output, but they are typically very | |
142 | long lines. You can use the ``Cell | All Output | Clear`` menu option to |
|
146 | long lines. You can use the ``Cell | All Output | Clear`` menu option to | |
143 | remove all output from a notebook prior to committing it to version |
|
147 | remove all output from a notebook prior to committing it to version | |
144 | control, if this is a concern. |
|
148 | control, if this is a concern. | |
145 |
|
149 | |||
146 | The notebook server can also generate a pure Python version of your notebook, |
|
150 | The notebook server can also generate a pure Python version of your notebook, | |
147 | using the ``File | Download as`` menu option. The resulting ``.py`` file will |
|
151 | using the ``File | Download as`` menu option. The resulting ``.py`` file will | |
148 | contain all the code cells from your notebook verbatim, and all Markdown cells |
|
152 | contain all the code cells from your notebook verbatim, and all Markdown cells | |
149 | prepended with a comment marker. The separation between code and Markdown |
|
153 | prepended with a comment marker. The separation between code and Markdown | |
150 | cells is indicated with special comments and there is a header indicating the |
|
154 | cells is indicated with special comments and there is a header indicating the | |
151 | format version. All output is removed when exporting to Python. |
|
155 | format version. All output is removed when exporting to Python. | |
152 |
|
156 | |||
153 | As an example, consider a simple notebook called ``simple.ipynb`` which |
|
157 | As an example, consider a simple notebook called ``simple.ipynb`` which | |
154 | contains one Markdown cell, with the content ``The simplest notebook.``, one |
|
158 | contains one Markdown cell, with the content ``The simplest notebook.``, one | |
155 | code input cell with the content ``print "Hello, IPython!"``, and the |
|
159 | code input cell with the content ``print "Hello, IPython!"``, and the | |
156 | corresponding output. |
|
160 | corresponding output. | |
157 |
|
161 | |||
158 | The contents of the notebook document ``simple.ipynb`` is the following JSON |
|
162 | The contents of the notebook document ``simple.ipynb`` is the following JSON | |
159 | container:: |
|
163 | container:: | |
160 |
|
164 | |||
161 | { |
|
165 | { | |
162 | "metadata": { |
|
166 | "metadata": { | |
163 | "name": "simple" |
|
167 | "name": "simple" | |
164 | }, |
|
168 | }, | |
165 | "nbformat": 3, |
|
169 | "nbformat": 3, | |
166 | "nbformat_minor": 0, |
|
170 | "nbformat_minor": 0, | |
167 | "worksheets": [ |
|
171 | "worksheets": [ | |
168 | { |
|
172 | { | |
169 | "cells": [ |
|
173 | "cells": [ | |
170 | { |
|
174 | { | |
171 | "cell_type": "markdown", |
|
175 | "cell_type": "markdown", | |
172 | "metadata": {}, |
|
176 | "metadata": {}, | |
173 | "source": "The simplest notebook." |
|
177 | "source": "The simplest notebook." | |
174 | }, |
|
178 | }, | |
175 | { |
|
179 | { | |
176 | "cell_type": "code", |
|
180 | "cell_type": "code", | |
177 | "collapsed": false, |
|
181 | "collapsed": false, | |
178 | "input": "print \"Hello, IPython\"", |
|
182 | "input": "print \"Hello, IPython\"", | |
179 | "language": "python", |
|
183 | "language": "python", | |
180 | "metadata": {}, |
|
184 | "metadata": {}, | |
181 | "outputs": [ |
|
185 | "outputs": [ | |
182 | { |
|
186 | { | |
183 | "output_type": "stream", |
|
187 | "output_type": "stream", | |
184 | "stream": "stdout", |
|
188 | "stream": "stdout", | |
185 | "text": "Hello, IPython\n" |
|
189 | "text": "Hello, IPython\n" | |
186 | } |
|
190 | } | |
187 | ], |
|
191 | ], | |
188 | "prompt_number": 1 |
|
192 | "prompt_number": 1 | |
189 | } |
|
193 | } | |
190 | ], |
|
194 | ], | |
191 | "metadata": {} |
|
195 | "metadata": {} | |
192 | } |
|
196 | } | |
193 | ] |
|
197 | ] | |
194 | } |
|
198 | } | |
195 |
|
199 | |||
196 |
|
200 | |||
197 | The corresponding Python script is:: |
|
201 | The corresponding Python script is:: | |
198 |
|
202 | |||
199 | # -*- coding: utf-8 -*- |
|
203 | # -*- coding: utf-8 -*- | |
200 | # <nbformat>3.0</nbformat> |
|
204 | # <nbformat>3.0</nbformat> | |
201 |
|
205 | |||
202 | # <markdowncell> |
|
206 | # <markdowncell> | |
203 |
|
207 | |||
204 | # The simplest notebook. |
|
208 | # The simplest notebook. | |
205 |
|
209 | |||
206 | # <codecell> |
|
210 | # <codecell> | |
207 |
|
211 | |||
208 | print "Hello, IPython" |
|
212 | print "Hello, IPython" | |
209 |
|
213 | |||
210 | Note that indeed the output of the code cell, which is present in the JSON |
|
214 | Note that indeed the output of the code cell, which is present in the JSON | |
211 | container, has been removed in the ``.py`` script. |
|
215 | container, has been removed in the ``.py`` script. | |
212 |
|
216 |
General Comments 0
You need to be logged in to leave comments.
Login now