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