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