##// END OF EJS Templates
Merge pull request #1 from minrk/nbconvert...
Jonathan Frederic -
r11470:6bbbdb48 merge
parent child Browse files
Show More
@@ -6,6 +6,6 b' from .reveal import RevealExporter'
6 from .latex import LatexExporter
6 from .latex import LatexExporter
7 from .markdown import MarkdownExporter
7 from .markdown import MarkdownExporter
8 from .python import PythonExporter
8 from .python import PythonExporter
9 from .rst import RstExporter
9 from .rst import RSTExporter
10 from .sphinx_howto import SphinxHowtoExporter
10 from .sphinx_howto import SphinxHowtoExporter
11 from .sphinx_manual import SphinxManualExporter
11 from .sphinx_manual import SphinxManualExporter
@@ -25,7 +25,7 b' from .latex import LatexExporter'
25 from .markdown import MarkdownExporter
25 from .markdown import MarkdownExporter
26 from .python import PythonExporter
26 from .python import PythonExporter
27 from .reveal import RevealExporter
27 from .reveal import RevealExporter
28 from .rst import RstExporter
28 from .rst import RSTExporter
29 from .sphinx_howto import SphinxHowtoExporter
29 from .sphinx_howto import SphinxHowtoExporter
30 from .sphinx_manual import SphinxManualExporter
30 from .sphinx_manual import SphinxManualExporter
31
31
@@ -33,7 +33,7 b' from .sphinx_manual import SphinxManualExporter'
33 # Classes
33 # Classes
34 #-----------------------------------------------------------------------------
34 #-----------------------------------------------------------------------------
35
35
36 def DocDecorator(f):
36 def DocDecorator(f):
37
37
38 #Set docstring of function
38 #Set docstring of function
39 f.__doc__ = f.__doc__ + """
39 f.__doc__ = f.__doc__ + """
@@ -184,7 +184,7 b' def export_python(nb, **kw):'
184 @DocDecorator
184 @DocDecorator
185 def export_reveal(nb, **kw):
185 def export_reveal(nb, **kw):
186 """
186 """
187 Export a notebook object to Reveal
187 Export a notebook object to a Reveal.js presentation
188 """
188 """
189 return export(RevealExporter, nb, **kw)
189 return export(RevealExporter, nb, **kw)
190
190
@@ -192,9 +192,9 b' def export_reveal(nb, **kw):'
192 @DocDecorator
192 @DocDecorator
193 def export_rst(nb, **kw):
193 def export_rst(nb, **kw):
194 """
194 """
195 Export a notebook object to RST
195 Export a notebook object to reStructuredText
196 """
196 """
197 return export(RstExporter, nb, **kw)
197 return export(RSTExporter, nb, **kw)
198
198
199
199
200 @DocDecorator
200 @DocDecorator
@@ -22,7 +22,7 b' from .exporter import Exporter'
22 # Classes
22 # Classes
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24
24
25 class RstExporter(Exporter):
25 class RSTExporter(Exporter):
26 """
26 """
27 Exports restructured text documents.
27 Exports restructured text documents.
28 """
28 """
@@ -38,5 +38,5 b' class RstExporter(Exporter):'
38 @property
38 @property
39 def default_config(self):
39 def default_config(self):
40 c = Config({'ExtractFigureTransformer':{'enabled':True}})
40 c = Config({'ExtractFigureTransformer':{'enabled':True}})
41 c.merge(super(RstExporter,self).default_config)
41 c.merge(super(RSTExporter,self).default_config)
42 return c
42 return c
@@ -1,8 +1,7 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 """NBConvert is a utility for conversion of IPYNB files.
2 """NBConvert is a utility for conversion of .ipynb files.
3
3
4 Commandline interface for the NBConvert conversion utility. Read the
4 Command-line interface for the NbConvert conversion utility.
5 readme.rst for usage information
6 """
5 """
7 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
8 #Copyright (c) 2013, the IPython Development Team.
7 #Copyright (c) 2013, the IPython Development Team.
@@ -23,42 +22,87 b' import os'
23 import glob
22 import glob
24
23
25 #From IPython
24 #From IPython
26 from IPython.core.application import BaseIPythonApplication
25 from IPython.core.application import BaseIPythonApplication, base_aliases, base_flags
27 from IPython.config.application import catch_config_error
26 from IPython.config import catch_config_error, Configurable
28 from IPython.utils.traitlets import Unicode, List, Instance, DottedObjectName, Type
27 from IPython.utils.traitlets import (
28 Unicode, List, Instance, DottedObjectName, Type, CaselessStrEnum,
29 )
29 from IPython.utils.importstring import import_item
30 from IPython.utils.importstring import import_item
30
31
31 from .exporters.export import export_by_name, get_export_names, ExporterNameError
32 from .exporters.export import export_by_name, get_export_names, ExporterNameError
32 from .exporters.exporter import Exporter
33 from IPython.nbconvert import exporters, transformers, writers
33 from .writers.base import WriterBase
34 from .utils.base import NbConvertBase
34 from .utils.base import NbConvertBase
35
35
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37 #Classes and functions
37 #Classes and functions
38 #-----------------------------------------------------------------------------
38 #-----------------------------------------------------------------------------
39
39
40 nbconvert_aliases = {}
41 nbconvert_aliases.update(base_aliases)
42 nbconvert_aliases.update({
43 'format' : 'NbConvertApp.export_format',
44 'notebooks' : 'NbConvertApp.notebooks',
45 'writer' : 'NbConvertApp.writer_class',
46 })
47
48 nbconvert_flags = {}
49 nbconvert_flags.update(base_flags)
50 nbconvert_flags.update({
51 'stdout' : (
52 {'NbConvertApp' : {'writer_class' : "StdoutWriter"}},
53 "Write notebook output to stdout instead of files."
54 )
55 })
56
57
40 class NbConvertApp(BaseIPythonApplication):
58 class NbConvertApp(BaseIPythonApplication):
41 """Application used to convert to and from notebook file type (*.ipynb)"""
59 """Application used to convert to and from notebook file type (*.ipynb)"""
42
60
43 name = 'ipython-nbconvert'
61 name = 'ipython-nbconvert'
62 aliases = nbconvert_aliases
63 flags = nbconvert_flags
64
65 def _classes_default(self):
66 classes = [NbConvertBase]
67 for pkg in (exporters, transformers, writers):
68 for name in dir(pkg):
69 cls = getattr(pkg, name)
70 if isinstance(cls, type) and issubclass(cls, Configurable):
71 classes.append(cls)
72 return classes
44
73
45 description = Unicode(
74 description = Unicode(
46 u"""This application is used to convert notebook files (*.ipynb).
75 u"""This application is used to convert notebook files (*.ipynb)
47 An ipython config file can be used to batch convert notebooks in the
76 to various other formats.""")
48 current directory.""")
49
77
50 examples = Unicode(u"""
78 examples = Unicode(u"""
51 Running `ipython nbconvert` will read the directory config file and then
79 The simplest way to use nbconvert is
52 apply it to one or more notebooks.
80
53
81 > ipython nbconvert mynotebook.ipynb
82
83 which will convert mynotebook.ipynb to the default format (probably HTML).
84
85 You can specify the export format with `--format`.
86 Options include {0}
87
88 > ipython nbconvert --format latex mynotebook.ipnynb
89
90 You can also pipe the output to stdout, rather than a file
91
92 > ipython nbconvert mynotebook.ipynb --stdout
93
54 Multiple notebooks can be given at the command line in a couple of
94 Multiple notebooks can be given at the command line in a couple of
55 different ways:
95 different ways:
56
96
57 > ipython nbconvert notebook*.ipynb
97 > ipython nbconvert notebook*.ipynb
58 > ipython nbconvert notebook1.ipynb notebook2.ipynb
98 > ipython nbconvert notebook1.ipynb notebook2.ipynb
59 > ipython nbconvert # this will use the config file to fill in the notebooks
99
60 """)
100 or you can specify the notebooks list in a config file, containing::
61
101
102 c.NbConvertApp.notebooks = ["my_notebook.ipynb"]
103
104 > ipython nbconvert --config mycfg.py
105 """.format(get_export_names()))
62 #Writer specific variables
106 #Writer specific variables
63 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
107 writer = Instance('IPython.nbconvert.writers.base.WriterBase',
64 help="""Instance of the writer class used to write the
108 help="""Instance of the writer class used to write the
@@ -78,55 +122,45 b' class NbConvertApp(BaseIPythonApplication):'
78
122
79
123
80 #Other configurable variables
124 #Other configurable variables
81 export_format = Unicode(
125 export_format = CaselessStrEnum(get_export_names(),
82 "", config=True,
126 default_value="full_html",
83 help="""If specified, nbconvert will convert the document(s) specified
127 config=True,
84 using this format.""")
128 help="""The export format to be used."""
129 )
85
130
86 notebooks = List([], config=True, help="""List of notebooks to convert.
131 notebooks = List([], config=True, help="""List of notebooks to convert.
87 Search patterns are supported.""")
132 Wildcards are supported.
88
133 Filenames passed positionally will be added to the list.
89 nbconvert_aliases = {'format':'NbConvertApp.export_format',
134 """)
90 'notebooks':'NbConvertApp.notebooks',
91 'writer':'NbConvertApp.writer_class'}
92
93
135
94 @catch_config_error
136 @catch_config_error
95 def initialize(self, argv=None):
137 def initialize(self, argv=None):
96 self.aliases.update(self.nbconvert_aliases)
97
98 super(NbConvertApp, self).initialize(argv)
138 super(NbConvertApp, self).initialize(argv)
99
139 self.init_notebooks()
100 #Register class here to have help with help all
101 self.classes.insert(0, Exporter)
102 self.classes.insert(0, WriterBase)
103 self.classes.insert(0, NbConvertBase)
104
105 #Init
106 self.init_config(self.extra_args)
107 self.init_writer()
140 self.init_writer()
108
141
109
142 def init_notebooks(self):
110 def init_config(self, extra_args):
143 """Construct the list of notebooks.
111 """
144 If notebooks are passed on the command-line,
112 Add notebooks to the config if needed. Glob each notebook to replace
145 they override notebooks specified in config files.
113 notebook patterns with filenames.
146 Glob each notebook to replace notebook patterns with filenames.
114 """
147 """
115
148
116 #Get any additional notebook patterns from the commandline
149 # Specifying notebooks on the command-line overrides (rather than adds)
117 if len(extra_args) > 0:
150 # the notebook list
118 for pattern in extra_args:
151 if self.extra_args:
119 self.notebooks.append(pattern)
152 patterns = self.extra_args
153 else:
154 patterns = self.notebooks
120
155
121 #Use glob to replace all the notebook patterns with filenames.
156 #Use glob to replace all the notebook patterns with filenames.
122 filenames = []
157 filenames = []
123 for pattern in self.notebooks:
158 for pattern in patterns:
124 for filename in glob.glob(pattern):
159 for filename in glob.glob(pattern):
125 if not filename in filenames:
160 if not filename in filenames:
126 filenames.append(filename)
161 filenames.append(filename)
127 self.notebooks = filenames
162 self.notebooks = filenames
128
163
129
130 def init_writer(self):
164 def init_writer(self):
131 """
165 """
132 Initialize the writer (which is stateless)
166 Initialize the writer (which is stateless)
@@ -134,15 +168,13 b' class NbConvertApp(BaseIPythonApplication):'
134 self._writer_class_changed(None, self.writer_class, self.writer_class)
168 self._writer_class_changed(None, self.writer_class, self.writer_class)
135 self.writer = self.writer_factory(parent=self)
169 self.writer = self.writer_factory(parent=self)
136
170
137
171 def start(self):
138 def start(self, argv=None):
139 """
172 """
140 Ran after initiialization completed
173 Ran after initialization completed
141 """
174 """
142 super(NbConvertApp, self).start()
175 super(NbConvertApp, self).start()
143 self.convert_notebooks()
176 self.convert_notebooks()
144
177
145
146 def convert_notebooks(self):
178 def convert_notebooks(self):
147 """
179 """
148 Convert the notebooks in the self.notebook traitlet
180 Convert the notebooks in the self.notebook traitlet
@@ -1,5 +1,5 b''
1 # Class base Transformers
1 # Class base Transformers
2 from .base import ConfigurableTransformer
2 from .base import Transformer
3 from .convertfigures import ConvertFiguresTransformer
3 from .convertfigures import ConvertFiguresTransformer
4 from .svg2pdf import SVG2PDFTransformer
4 from .svg2pdf import SVG2PDFTransformer
5 from .extractfigure import ExtractFigureTransformer
5 from .extractfigure import ExtractFigureTransformer
@@ -23,7 +23,7 b' from IPython.utils.traitlets import Bool'
23 # Classes and Functions
23 # Classes and Functions
24 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
25
25
26 class ConfigurableTransformer(NbConvertBase):
26 class Transformer(NbConvertBase):
27 """ A configurable transformer
27 """ A configurable transformer
28
28
29 Inherit from this class if you wish to have configurability for your
29 Inherit from this class if you wish to have configurability for your
@@ -53,7 +53,7 b' class ConfigurableTransformer(NbConvertBase):'
53 Additional arguments
53 Additional arguments
54 """
54 """
55
55
56 super(ConfigurableTransformer, self).__init__(**kw)
56 super(Transformer, self).__init__(**kw)
57
57
58
58
59 def __call__(self, nb, resources):
59 def __call__(self, nb, resources):
@@ -13,14 +13,14 b' one format to another.'
13 # Imports
13 # Imports
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15
15
16 from .base import ConfigurableTransformer
16 from .base import Transformer
17 from IPython.utils.traitlets import Unicode
17 from IPython.utils.traitlets import Unicode
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Classes
20 # Classes
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22
22
23 class ConvertFiguresTransformer(ConfigurableTransformer):
23 class ConvertFiguresTransformer(Transformer):
24 """
24 """
25 Converts all of the outputs in a notebook from one format to another.
25 Converts all of the outputs in a notebook from one format to another.
26 """
26 """
@@ -19,7 +19,7 b' from pygments.formatters import HtmlFormatter'
19
19
20 from IPython.utils import path
20 from IPython.utils import path
21
21
22 from .base import ConfigurableTransformer
22 from .base import Transformer
23
23
24 from IPython.utils.traitlets import Unicode
24 from IPython.utils.traitlets import Unicode
25
25
@@ -27,7 +27,7 b' from IPython.utils.traitlets import Unicode'
27 # Classes and functions
27 # Classes and functions
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29
29
30 class CSSHTMLHeaderTransformer(ConfigurableTransformer):
30 class CSSHTMLHeaderTransformer(Transformer):
31 """
31 """
32 Transformer used to pre-process notebook for HTML output. Adds IPython notebook
32 Transformer used to pre-process notebook for HTML output. Adds IPython notebook
33 front-end CSS and Pygments CSS to HTML output.
33 front-end CSS and Pygments CSS to HTML output.
@@ -15,13 +15,13 b" notebook file. The extracted figures are returned in the 'resources' dictionary"
15
15
16 import sys
16 import sys
17 from IPython.utils.traitlets import Unicode
17 from IPython.utils.traitlets import Unicode
18 from .base import ConfigurableTransformer
18 from .base import Transformer
19
19
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21 # Classes
21 # Classes
22 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
23
23
24 class ExtractFigureTransformer(ConfigurableTransformer):
24 class ExtractFigureTransformer(Transformer):
25 """
25 """
26 Extracts all of the figures from the notebook file. The extracted
26 Extracts all of the figures from the notebook file. The extracted
27 figures are returned in the 'resources' dictionary.
27 figures are returned in the 'resources' dictionary.
@@ -17,14 +17,14 b' from __future__ import print_function, absolute_import'
17
17
18 # Our own imports
18 # Our own imports
19 # Needed to override transformer
19 # Needed to override transformer
20 from .base import (ConfigurableTransformer)
20 from .base import (Transformer)
21 from IPython.nbconvert import filters
21 from IPython.nbconvert import filters
22
22
23 #-----------------------------------------------------------------------------
23 #-----------------------------------------------------------------------------
24 # Classes
24 # Classes
25 #-----------------------------------------------------------------------------
25 #-----------------------------------------------------------------------------
26
26
27 class LatexTransformer(ConfigurableTransformer):
27 class LatexTransformer(Transformer):
28 """
28 """
29 Converter for latex destined documents.
29 Converter for latex destined documents.
30 """
30 """
@@ -12,14 +12,14 b''
12 # Imports
12 # Imports
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 from .base import ConfigurableTransformer
15 from .base import Transformer
16 from IPython.utils.traitlets import Unicode
16 from IPython.utils.traitlets import Unicode
17
17
18 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
19 # Classes and functions
19 # Classes and functions
20 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
21
21
22 class RevealHelpTransformer(ConfigurableTransformer):
22 class RevealHelpTransformer(Transformer):
23
23
24 url_prefix = Unicode('//cdn.jsdelivr.net/reveal.js/2.4.0',
24 url_prefix = Unicode('//cdn.jsdelivr.net/reveal.js/2.4.0',
25 config=True,
25 config=True,
@@ -33,7 +33,7 b' from pygments.formatters import LatexFormatter'
33 from IPython.utils.traitlets import Unicode, Bool
33 from IPython.utils.traitlets import Unicode, Bool
34
34
35 # Needed to override transformer
35 # Needed to override transformer
36 from .base import (ConfigurableTransformer)
36 from .base import (Transformer)
37
37
38 from IPython.nbconvert.utils import console
38 from IPython.nbconvert.utils import console
39
39
@@ -41,7 +41,7 b' from IPython.nbconvert.utils import console'
41 # Classes and functions
41 # Classes and functions
42 #-----------------------------------------------------------------------------
42 #-----------------------------------------------------------------------------
43
43
44 class SphinxTransformer(ConfigurableTransformer):
44 class SphinxTransformer(Transformer):
45 """
45 """
46 Sphinx utility transformer.
46 Sphinx utility transformer.
47
47
General Comments 0
You need to be logged in to leave comments. Login now