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