##// END OF EJS Templates
Merge pull request #179 from Carreau/global-configurable...
Matthias Bussonnier -
r10931:425cd964 merge
parent child Browse files
Show More
@@ -1,215 +1,217 b''
1 1 #!/usr/bin/env python
2 2 """NBConvert is a utility for conversion of IPYNB files.
3 3
4 4 Commandline interface for the NBConvert conversion utility. Read the
5 5 readme.rst for usage information
6 6 """
7 7 #-----------------------------------------------------------------------------
8 8 #Copyright (c) 2013, the IPython Development Team.
9 9 #
10 10 #Distributed under the terms of the Modified BSD License.
11 11 #
12 12 #The full license is in the file COPYING.txt, distributed with this software.
13 13 #-----------------------------------------------------------------------------
14 14
15 15 #-----------------------------------------------------------------------------
16 16 #Imports
17 17 #-----------------------------------------------------------------------------
18 18
19 19 #Stdlib imports
20 20 from __future__ import print_function
21 21 import sys
22 22 import io
23 23 import os
24 24
25 25 #From IPython
26 26 #All the stuff needed for the configurable things
27 27 from IPython.config.application import Application
28 28 from IPython.utils.traitlets import (Bool)
29 29
30 30 #Local imports
31 31 from nbconvert.exporters.export import export_by_name
32 32 from nbconvert.exporters.exporter import Exporter
33 33 from nbconvert.transformers import extractfigure
34 from nbconvert.utils.config import GlobalConfigurable
34 35
35 36 #-----------------------------------------------------------------------------
36 37 #Globals and constants
37 38 #-----------------------------------------------------------------------------
38 39
39 40 #'Keys in resources' user prompt.
40 41 KEYS_PROMPT_HEAD = "====================== Keys in Resources =================================="
41 42 KEYS_PROMPT_BODY = """
42 43 ===========================================================================
43 44 You are responsible for writting these files into the appropriate
44 45 directorie(s) if need be. If you do not want to see this message, enable
45 46 the 'write' (boolean) flag of the converter.
46 47 ===========================================================================
47 48 """
48 49
49 50 #-----------------------------------------------------------------------------
50 51 #Classes and functions
51 52 #-----------------------------------------------------------------------------
52 53
53 54 class NbConvertApp(Application):
54 55 """Application used to convert to and from notebook file type (*.ipynb)"""
55 56
56 57 stdout = Bool(
57 58 False, config=True,
58 59 help="""Whether to print the converted IPYNB file to stdout
59 60 use full do diff files without actually writing a new file"""
60 61 )
61 62
62 63 write = Bool(
63 64 True, config=True,
64 65 help="""Should the converted notebook file be written to disk
65 66 along with potential extracted resources."""
66 67 )
67 68
68 69 aliases = {
69 70 'stdout':'NbConvertApp.stdout',
70 71 'write':'NbConvertApp.write',
71 72 }
72 73
73 74 flags = {}
74 75
75 76 flags['stdout'] = (
76 77 {'NbConvertApp' : {'stdout' : True}},
77 78 """Print converted file to stdout, equivalent to --stdout=True
78 79 """
79 80 )
80 81
81 82 flags['no-write'] = (
82 83 {'NbConvertApp' : {'write' : True}},
83 84 """Do not write to disk, eauivalent to --write=False
84 85 """
85 86 )
86 87
87 88
88 89 def __init__(self, **kwargs):
89 90 """Public constructor"""
90 91
91 92 #Call base class
92 93 super(NbConvertApp, self).__init__(**kwargs)
93 94
94 95 #Register class here to have help with help all
95 96 self.classes.insert(0, Exporter)
97 self.classes.insert(0, GlobalConfigurable)
96 98
97 99
98 100 def start(self, argv=None):
99 101 """Entrypoint of NbConvert application.
100 102
101 103 Parameters
102 104 ----------
103 105 argv : list
104 106 Commandline arguments
105 107 """
106 108
107 109 #Parse the commandline options.
108 110 self.parse_command_line(argv)
109 111
110 112 #Call base
111 113 super(NbConvertApp, self).start()
112 114
113 115 #The last arguments in list will be used by nbconvert
114 116 if len(self.extra_args) is not 3:
115 117 print( "Wrong number of arguments, use --help flag for usage", file=sys.stderr)
116 118 sys.exit(-1)
117 119 export_type = (self.extra_args)[1]
118 120 ipynb_file = (self.extra_args)[2]
119 121
120 122 #Export
121 123 return_value = export_by_name(export_type, ipynb_file)
122 124 if return_value is None:
123 125 print("Error: '%s' template not found." % export_type)
124 126 return
125 127 else:
126 128 (output, resources, exporter) = return_value
127 129
128 130 #TODO: Allow user to set output directory and file.
129 131 destination_filename = None
130 132 destination_directory = None
131 133 if self.write:
132 134
133 135 #Get the file name without the '.ipynb' (6 chars) extension and then
134 136 #remove any addition periods and spaces. The resulting name will
135 137 #be used to create the directory that the files will be exported
136 138 #into.
137 139 out_root = ipynb_file[:-6].replace('.', '_').replace(' ', '_')
138 140 destination_filename = os.path.join(out_root+'.'+exporter.file_extension)
139 141
140 142 destination_directory = out_root+'_files'
141 143 if not os.path.exists(destination_directory):
142 144 os.mkdir(destination_directory)
143 145
144 146 #Write the results
145 147 if self.stdout or not (destination_filename is None and destination_directory is None):
146 148 self._write_results(output, resources, destination_filename, destination_directory)
147 149
148 150
149 151 def _write_results(self, output, resources, destination_filename=None, destination_directory=None):
150 152 """Output the conversion results to the console and/or filesystem
151 153
152 154 Parameters
153 155 ----------
154 156 output : str
155 157 Output of conversion
156 158 resources : dictionary
157 159 Additional input/output used by the transformers. For
158 160 example, the ExtractFigure transformer outputs the
159 161 figures it extracts into this dictionary. This method
160 162 relies on the figures being in this dictionary when
161 163 attempting to write the figures to the file system.
162 164 destination_filename : str, Optional
163 165 Filename to write output into. If None, output is not
164 166 written to a file.
165 167 destination_directory : str, Optional
166 168 Directory to write notebook data (i.e. figures) to. If
167 169 None, figures are not written to the file system.
168 170 """
169 171
170 172 if self.stdout:
171 173 print(output.encode('utf-8'))
172 174
173 175 #Write file output from conversion.
174 176 if not destination_filename is None:
175 177 with io.open(destination_filename, 'w') as f:
176 178 f.write(output)
177 179
178 180 #Get the key names used by the extract figure transformer
179 181 figures_key = extractfigure.FIGURES_KEY
180 182 binary_key = extractfigure.BINARY_KEY
181 183 text_key = extractfigure.TEXT_KEY
182 184
183 185 #Output any associate figures into the same "root" directory.
184 186 binkeys = resources.get(figures_key, {}).get(binary_key,{}).keys()
185 187 textkeys = resources.get(figures_key, {}).get(text_key,{}).keys()
186 188 if binkeys or textkeys :
187 189 if not destination_directory is None:
188 190 for key in binkeys:
189 191 with io.open(os.path.join(destination_directory, key), 'wb') as f:
190 192 f.write(resources[figures_key][binary_key][key])
191 193 for key in textkeys:
192 194 with io.open(os.path.join(destination_directory, key), 'w') as f:
193 195 f.write(resources[figures_key][text_key][key])
194 196
195 197 #Figures that weren't exported which will need to be created by the
196 198 #user. Tell the user what figures these are.
197 199 if self.stdout:
198 200 print(KEYS_PROMPT_HEAD, file=sys.stderr)
199 201 print(resources[figures_key].keys(), file=sys.stderr)
200 202 print(KEYS_PROMPT_BODY , file=sys.stderr)
201 203
202 204 #-----------------------------------------------------------------------------
203 205 #Script main
204 206 #-----------------------------------------------------------------------------
205 207
206 208 def main():
207 209 """Application entry point"""
208 210
209 211 app = NbConvertApp.instance()
210 212 app.description = __doc__
211 213 app.start(argv=sys.argv)
212 214
213 215 #Check to see if python is calling this file directly.
214 216 if __name__ == '__main__':
215 217 main()
@@ -1,31 +1,33 b''
1 1 """Filter used to select the first preferred output format available.
2 2
3 3 The filter contained in the file allows the converter templates to select
4 4 the output format that is most valuable to the active export format. The
5 5 value of the different formats is set via
6 6 GlobalConfigurable.display_data_priority
7 7 """
8 8 #-----------------------------------------------------------------------------
9 9 # Copyright (c) 2013, the IPython Development Team.
10 10 #
11 11 # Distributed under the terms of the Modified BSD License.
12 12 #
13 13 # The full license is in the file COPYING.txt, distributed with this software.
14 14 #-----------------------------------------------------------------------------
15 15
16 16 #-----------------------------------------------------------------------------
17 17 # Classes and functions
18 18 #-----------------------------------------------------------------------------
19 19
20 class DataTypeFilter(object):
20 from ..utils.config import GlobalConfigurable
21
22 class DataTypeFilter(GlobalConfigurable):
21 23 """ Returns the preferred display format """
22 24
23 25 display_data_priority = ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text']
24 26
25 27 def __call__(self, output):
26 28 """ Return the first available format in the priority """
27 29
28 30 for fmt in self.display_data_priority:
29 31 if fmt in output:
30 32 return [fmt]
31 33 return []
@@ -1,101 +1,102 b''
1 1 """
2 2 Module that re-groups transformer that would be applied to iPyNB files
3 3 before going through the templating machinery.
4 4
5 5 It exposes a convenient class to inherit from to access configurability.
6 6 """
7 7 #-----------------------------------------------------------------------------
8 8 # Copyright (c) 2013, the IPython Development Team.
9 9 #
10 10 # Distributed under the terms of the Modified BSD License.
11 11 #
12 12 # The full license is in the file COPYING.txt, distributed with this software.
13 13 #-----------------------------------------------------------------------------
14 14
15 15 #-----------------------------------------------------------------------------
16 16 # Imports
17 17 #-----------------------------------------------------------------------------
18 18
19 19 from __future__ import print_function, absolute_import
20 20
21 21 from IPython.config.configurable import Configurable
22 from ..utils.config import GlobalConfigurable
22 23
23 24 #-----------------------------------------------------------------------------
24 25 # Classes and Functions
25 26 #-----------------------------------------------------------------------------
26 27
27 class ConfigurableTransformer(Configurable):
28 class ConfigurableTransformer(GlobalConfigurable):
28 29 """ A configurable transformer
29 30
30 31 Inherit from this class if you wish to have configurability for your
31 32 transformer.
32 33
33 34 Any configurable traitlets this class exposed will be configurable in profiles
34 35 using c.SubClassName.atribute=value
35 36
36 37 you can overwrite cell_transform to apply a transformation independently on each cell
37 38 or __call__ if you prefer your own logic. See orresponding docstring for informations.
38 39 """
39 40
40 41 def __init__(self, config=None, **kw):
41 42 """
42 43 Public constructor
43 44
44 45 Parameters
45 46 ----------
46 47 config : Config
47 48 Configuration file structure
48 49 **kw : misc
49 50 Additional arguments
50 51 """
51 52
52 53 super(ConfigurableTransformer, self).__init__(config=config, **kw)
53 54
54 55
55 56 def __call__(self, nb, resources):
56 57 return self.call(nb,resources)
57 58
58 59 def call(self, nb, resources):
59 60 """
60 61 Transformation to apply on each notebook.
61 62
62 63 You should return modified nb, resources.
63 64 If you wish to apply your transform on each cell, you might want to
64 65 overwrite cell_transform method instead.
65 66
66 67 Parameters
67 68 ----------
68 69 nb : NotebookNode
69 70 Notebook being converted
70 71 resources : dictionary
71 72 Additional resources used in the conversion process. Allows
72 73 transformers to pass variables into the Jinja engine.
73 74 """
74 75 try :
75 76 for worksheet in nb.worksheets :
76 77 for index, cell in enumerate(worksheet.cells):
77 78 worksheet.cells[index], resources = self.cell_transform(cell, resources, index)
78 79 return nb, resources
79 80 except NotImplementedError:
80 81 raise NotImplementedError('should be implemented by subclass')
81 82
82 83
83 84 def cell_transform(self, cell, resources, index):
84 85 """
85 86 Overwrite if you want to apply a transformation on each cell. You
86 87 should return modified cell and resource dictionary.
87 88
88 89 Parameters
89 90 ----------
90 91 cell : NotebookNode cell
91 92 Notebook cell being processed
92 93 resources : dictionary
93 94 Additional resources used in the conversion process. Allows
94 95 transformers to pass variables into the Jinja engine.
95 96 index : int
96 97 Index of the cell being processed
97 98 """
98 99
99 100 raise NotImplementedError('should be implemented by subclass')
100 101 return cell, resources
101 102
1 NO CONTENT: file renamed from nbconvert1/converters/config.py to nbconvert/utils/config.py
General Comments 0
You need to be logged in to leave comments. Login now