##// END OF EJS Templates
don't reload template on every export...
MinRK -
Show More
@@ -28,10 +28,10 b' import datetime'
28 from jinja2 import Environment, FileSystemLoader, ChoiceLoader, TemplateNotFound
28 from jinja2 import Environment, FileSystemLoader, ChoiceLoader, TemplateNotFound
29
29
30 # IPython imports
30 # IPython imports
31 from IPython.config.configurable import Configurable
31 from IPython.config.configurable import LoggingConfigurable
32 from IPython.config import Config
32 from IPython.config import Config
33 from IPython.nbformat import current as nbformat
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 from IPython.utils.importstring import import_item
35 from IPython.utils.importstring import import_item
36 from IPython.utils.text import indent
36 from IPython.utils.text import indent
37 from IPython.utils import py3compat
37 from IPython.utils import py3compat
@@ -78,7 +78,7 b' class ResourcesDict(collections.defaultdict):'
78 return ''
78 return ''
79
79
80
80
81 class Exporter(Configurable):
81 class Exporter(LoggingConfigurable):
82 """
82 """
83 Exports notebooks into other file formats. Uses Jinja 2 templating engine
83 Exports notebooks into other file formats. Uses Jinja 2 templating engine
84 to output new formats. Inherit from this class if you are creating a new
84 to output new formats. Inherit from this class if you are creating a new
@@ -102,7 +102,11 b' class Exporter(Configurable):'
102 self.template_file = self.default_template
102 self.template_file = self.default_template
103 else:
103 else:
104 self.template_file = new
104 self.template_file = new
105 self._load_template()
106
105 default_template = Unicode(u'')
107 default_template = Unicode(u'')
108 template = Any()
109 environment = Any()
106
110
107 file_extension = Unicode(
111 file_extension = Unicode(
108 'txt', config=True,
112 'txt', config=True,
@@ -110,6 +114,8 b' class Exporter(Configurable):'
110 )
114 )
111
115
112 template_path = List(['.'], config=True)
116 template_path = List(['.'], config=True)
117 def _template_path_changed(self, name, old, new):
118 self._load_template()
113
119
114 default_template_path = Unicode(
120 default_template_path = Unicode(
115 os.path.join("..", "templates"),
121 os.path.join("..", "templates"),
@@ -183,6 +189,30 b' class Exporter(Configurable):'
183 def default_config(self):
189 def default_config(self):
184 return Config()
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 def from_notebook_node(self, nb, resources=None, **kw):
217 def from_notebook_node(self, nb, resources=None, **kw):
188 """
218 """
@@ -201,23 +231,9 b' class Exporter(Configurable):'
201 # Preprocess
231 # Preprocess
202 nb_copy, resources = self._transform(nb_copy, resources)
232 nb_copy, resources = self._transform(nb_copy, resources)
203
233
204 # Try different template names during conversion. First try to load the
234 self._load_template()
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
219
235
220 if hasattr(self, 'template'):
236 if self.template is not None:
221 output = self.template.render(nb=nb_copy, resources=resources)
237 output = self.template.render(nb=nb_copy, resources=resources)
222 else:
238 else:
223 raise IOError('template file "%s" could not be found' % self.template_file)
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