Show More
@@ -28,10 +28,10 b' import datetime' | |||
|
28 | 28 | from jinja2 import Environment, FileSystemLoader, ChoiceLoader, TemplateNotFound |
|
29 | 29 | |
|
30 | 30 | # IPython imports |
|
31 | from IPython.config.configurable import Configurable | |
|
31 | from IPython.config.configurable import LoggingConfigurable | |
|
32 | 32 | from IPython.config import Config |
|
33 | 33 | from IPython.nbformat import current as nbformat |
|
34 | from IPython.utils.traitlets import MetaHasTraits, DottedObjectName, Unicode, List, Dict | |
|
34 | from IPython.utils.traitlets import MetaHasTraits, DottedObjectName, Unicode, List, Dict, Any | |
|
35 | 35 | from IPython.utils.importstring import import_item |
|
36 | 36 | from IPython.utils.text import indent |
|
37 | 37 | from IPython.utils import py3compat |
@@ -78,7 +78,7 b' class ResourcesDict(collections.defaultdict):' | |||
|
78 | 78 | return '' |
|
79 | 79 | |
|
80 | 80 | |
|
81 | class Exporter(Configurable): | |
|
81 | class Exporter(LoggingConfigurable): | |
|
82 | 82 | """ |
|
83 | 83 | Exports notebooks into other file formats. Uses Jinja 2 templating engine |
|
84 | 84 | to output new formats. Inherit from this class if you are creating a new |
@@ -102,7 +102,11 b' class Exporter(Configurable):' | |||
|
102 | 102 | self.template_file = self.default_template |
|
103 | 103 | else: |
|
104 | 104 | self.template_file = new |
|
105 | self._load_template() | |
|
106 | ||
|
105 | 107 | default_template = Unicode(u'') |
|
108 | template = Any() | |
|
109 | environment = Any() | |
|
106 | 110 | |
|
107 | 111 | file_extension = Unicode( |
|
108 | 112 | 'txt', config=True, |
@@ -110,6 +114,8 b' class Exporter(Configurable):' | |||
|
110 | 114 | ) |
|
111 | 115 | |
|
112 | 116 | template_path = List(['.'], config=True) |
|
117 | def _template_path_changed(self, name, old, new): | |
|
118 | self._load_template() | |
|
113 | 119 | |
|
114 | 120 | default_template_path = Unicode( |
|
115 | 121 | os.path.join("..", "templates"), |
@@ -183,6 +189,30 b' class Exporter(Configurable):' | |||
|
183 | 189 | def default_config(self): |
|
184 | 190 | return Config() |
|
185 | 191 | |
|
192 | def _load_template(self): | |
|
193 | if self.template is not None: | |
|
194 | return | |
|
195 | # called too early, do nothing | |
|
196 | if self.environment is None: | |
|
197 | return | |
|
198 | # Try different template names during conversion. First try to load the | |
|
199 | # template by name with extension added, then try loading the template | |
|
200 | # as if the name is explicitly specified, then try the name as a | |
|
201 | # 'flavor', and lastly just try to load the template by module name. | |
|
202 | module_name = self.__module__.rsplit('.', 1)[-1] | |
|
203 | try_names = [self.template_file + self.template_extension, | |
|
204 | self.template_file, | |
|
205 | module_name + '_' + self.template_file + self.template_extension, | |
|
206 | module_name + self.template_extension] | |
|
207 | for try_name in try_names: | |
|
208 | self.log.debug("Attempting to load template %s", try_name) | |
|
209 | try: | |
|
210 | self.template = self.environment.get_template(try_name) | |
|
211 | except TemplateNotFound: | |
|
212 | pass | |
|
213 | else: | |
|
214 | self.log.info("Loaded template %s", try_name) | |
|
215 | break | |
|
186 | 216 | |
|
187 | 217 | def from_notebook_node(self, nb, resources=None, **kw): |
|
188 | 218 | """ |
@@ -201,23 +231,9 b' class Exporter(Configurable):' | |||
|
201 | 231 | # Preprocess |
|
202 | 232 | nb_copy, resources = self._transform(nb_copy, resources) |
|
203 | 233 | |
|
204 | # Try different template names during conversion. First try to load the | |
|
205 | # template by name with extension added, then try loading the template | |
|
206 | # as if the name is explicitly specified, then try the name as a | |
|
207 | # 'flavor', and lastly just try to load the template by module name. | |
|
208 | module_name = self.__module__.split('.')[-1] | |
|
209 | try_names = [self.template_file + self.template_extension, | |
|
210 | self.template_file, | |
|
211 | module_name + '_' + self.template_file + self.template_extension, | |
|
212 | module_name + self.template_extension] | |
|
213 | for try_name in try_names: | |
|
214 | try: | |
|
215 | self.template = self.environment.get_template(try_name) | |
|
216 | break | |
|
217 | except TemplateNotFound: | |
|
218 | pass | |
|
234 | self._load_template() | |
|
219 | 235 | |
|
220 |
if |
|
|
236 | if self.template is not None: | |
|
221 | 237 | output = self.template.render(nb=nb_copy, resources=resources) |
|
222 | 238 | else: |
|
223 | 239 | raise IOError('template file "%s" could not be found' % self.template_file) |
General Comments 0
You need to be logged in to leave comments.
Login now