Skip to content

Commit

Permalink
Merge pull request #68 from SylvainCorlay/document-collect_template_p…
Browse files Browse the repository at this point in the history
…aths

Document collect_template_paths
  • Loading branch information
SylvainCorlay authored Feb 12, 2019
2 parents 0e2ecac + 9a4ea55 commit 82b47b5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
22 changes: 16 additions & 6 deletions voila/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,23 @@
from nbconvert.preprocessors.execute import executenb
from nbconvert import HTMLExporter

from .paths import collect_template_paths


class VoilaHandler(JupyterHandler):
def initialize(self, notebook_path=None, strip_sources=True, nbconvert_template_paths=None, config=None):
self.notebook_path = notebook_path
self.strip_sources = strip_sources
self.template_path = nbconvert_template_paths
self.exporter_config = config

def initialize(self, **kwargs):
self.notebook_path = kwargs.pop('notebook_path', []) # should it be []
self.strip_sources = kwargs.pop('strip_sources', True)
self.nbconvert_template_paths = kwargs.pop('nbconvert_template_paths', [])
self.exporter_config = kwargs.pop('config', None)

collect_template_paths(
self.nbconvert_template_paths,
[], # static_paths,
[], # tornado templates,
'default'
)

@tornado.web.authenticated
@tornado.gen.coroutine
Expand Down Expand Up @@ -64,7 +74,7 @@ def get(self, path=None):

exporter = HTMLExporter(
template_file='voila.tpl',
template_path=self.template_path,
template_path=self.nbconvert_template_paths,
config=self.exporter_config
)

Expand Down
23 changes: 19 additions & 4 deletions voila/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,24 @@
def collect_template_paths(
nbconvert_template_paths,
static_paths,
template_paths,
tornado_template_paths,
template_name='default'):
# we look at the usual jupyter locations, and for development purposes also
"""
Voila supports custom templates for rendering notebooks.
For a specified template name, `collect_template_paths` collects
- nbconvert template paths,
- static paths,
- tornado template paths,
by looking in the standard Jupyter data directories (PREFIX/share/jupyter/voila/template)
with different prefix values (user directory, sys prefix, and then system prefix) which
allows users to override templates locally.
The function will recursively load the base templates upon which the specified template
may be based.
"""

# We look at the usual jupyter locations, and for development purposes also
# relative to the package directory (with highest precedence)
template_directories = \
[os.path.abspath(os.path.join(ROOT, '..', 'share', 'jupyter', 'voila', 'template', template_name))] +\
Expand All @@ -39,7 +54,7 @@ def collect_template_paths(
collect_template_paths(
nbconvert_template_paths,
static_paths,
template_paths,
tornado_template_paths,
conf.get('base_template', 'default'))

extra_nbconvert_path = os.path.join(dirname, 'nbconvert_templates')
Expand All @@ -58,7 +73,7 @@ def collect_template_paths(
# if not os.path.exists(extra_template_path):
# log.warning('template named %s found at path %r, but %s does not exist', template_name,
# dirname, extra_template_path)
template_paths.insert(0, extra_template_path)
tornado_template_paths.insert(0, extra_template_path)

# We don't look at multiple directories, once a directory with a given name is found at a
# given level of precedence (for instance user directory), we don't look further (for instance
Expand Down

0 comments on commit 82b47b5

Please sign in to comment.