24
24
25
25
from jinja2 import Environment , FileSystemLoader
26
26
27
- from traitlets import Any , Dict , Set , Unicode , default
27
+ from traitlets import Any , Bool , Dict , Int , List , Set , Unicode , default
28
28
from traitlets .config import Application
29
29
30
30
from .handlers import init_handlers
31
31
from .cache import DummyAsyncCache , AsyncMultipartMemcache , MockCache , pylibmc
32
32
from .index import NoSearch
33
- from .formats import configure_formats
33
+ from .formats import default_formats
34
+ from nbconvert .exporters .export import exporter_map
35
+
34
36
from .providers import default_providers , default_rewrites
35
37
from .providers .url .client import NBViewerAsyncHTTPClient as HTTPClientClass
36
38
from .ratelimit import RateLimiter
@@ -122,10 +124,11 @@ def _load_max_cache_uris(self):
122
124
max_cache_uris .add ('/' + link ['target' ])
123
125
return max_cache_uris
124
126
127
+ processes = Int (default_value = 0 , help = "use processes instead of threads for rendering" ).tag (config = True )
128
+
125
129
static_path = Unicode (default_value = os .environ .get ("NBVIEWER_STATIC_PATH" , "" ), help = "Custom path for loading additional static files." ).tag (config = True )
126
130
127
131
static_url_prefix = Unicode (default_value = '/static/' ).tag (config = True )
128
-
129
132
# Not exposed to end user for configuration, since needs to access base_url
130
133
_static_url_prefix = Unicode ()
131
134
@default ('_static_url_prefix' )
@@ -204,8 +207,7 @@ def fetch_kwargs(self):
204
207
205
208
@cached_property
206
209
def formats (self ):
207
- formats = configure_formats (options , self .config , log .app_log )
208
- return formats
210
+ return self .configure_formats (log .app_log )
209
211
210
212
# load frontpage sections
211
213
@cached_property
@@ -223,8 +225,8 @@ def frontpage_setup(self):
223
225
224
226
@cached_property
225
227
def pool (self ):
226
- if options .processes :
227
- pool = ProcessPoolExecutor (options .processes )
228
+ if self .processes :
229
+ pool = ProcessPoolExecutor (self .processes )
228
230
else :
229
231
pool = ThreadPoolExecutor (options .threads )
230
232
return pool
@@ -254,6 +256,31 @@ def template_paths(self):
254
256
template_paths = [default_template_path ]
255
257
return template_paths
256
258
259
+ def configure_formats (self , log , formats = None ):
260
+ """
261
+ Format-specific configuration.
262
+ """
263
+ if formats is None :
264
+ formats = default_formats ()
265
+
266
+ # This would be better defined in a class
267
+ self .config .HTMLExporter .template_file = 'basic'
268
+ self .config .SlidesExporter .template_file = 'slides_reveal'
269
+
270
+ self .config .TemplateExporter .template_path = [
271
+ os .path .join (os .path .dirname (__file__ ), "templates" , "nbconvert" )
272
+ ]
273
+
274
+ for key , format in formats .items ():
275
+ exporter_cls = format .get ("exporter" , exporter_map [key ])
276
+ if self .processes :
277
+ # can't pickle exporter instances,
278
+ formats [key ]["exporter" ] = exporter_cls
279
+ else :
280
+ formats [key ]["exporter" ] = exporter_cls (config = self .config , log = log )
281
+
282
+ return formats
283
+
257
284
def init_tornado_application (self ):
258
285
# handle handlers
259
286
handler_names = dict (
@@ -415,7 +442,6 @@ def default_endpoint():
415
442
define ("no_cache" , default = False , help = "Do not cache results" , type = bool )
416
443
define ("no_check_certificate" , default = False , help = "Do not validate SSL certificates" , type = bool )
417
444
define ("port" , default = default_endpoint ()['port' ], help = "run on the given port" , type = int )
418
- define ("processes" , default = 0 , help = "use processes instead of threads for rendering" , type = int )
419
445
define ("provider_rewrites" , default = default_rewrites , help = "Full dotted package(s) that provide `uri_rewrites`" , type = str , multiple = True , group = "provider" )
420
446
define ("providers" , default = default_providers , help = "Full dotted package(s) that provide `default_handlers`" , type = str , multiple = True , group = "provider" )
421
447
define ("proxy_host" , default = "" , help = "The proxy URL." , type = str )
0 commit comments