Skip to content

Commit

Permalink
fix: allow custom template files outside of the template system (#1429)
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels authored Sep 30, 2020
1 parent 18e1b16 commit 42cfece
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
9 changes: 3 additions & 6 deletions nbconvert/exporters/templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,12 @@ def _template_file_changed(self, change):
full_path = os.path.abspath(new)
if os.path.isfile(full_path):
directory, self.template_file = os.path.split(full_path)
self.extra_template_paths = [directory] + self.extra_template_paths
# While not strictly an invalid template file name, the extension hints that there isn't a template directory involved
if self.template_file.endswith('.tpl'):
warnings.warn(
f"5.x style template file passed '{new}'. Use --template-name for the template directory with a index.<ext>.j2 file and/or --template-file to denote a different template.",
DeprecationWarning)
if directory:
directory, self.template_name = os.path.split(directory)
if directory:
if os.path.isabs(directory):
self.extra_template_basedirs = [directory]

@default('template_file')
def _template_file_default(self):
Expand All @@ -237,6 +233,7 @@ def _raw_template_changed(self, change):

template_paths = List(['.']).tag(config=True, affects_environment=True)
extra_template_basedirs = List().tag(config=True, affects_environment=True)
extra_template_paths = List([]).tag(config=True, affects_environment=True)

@default('extra_template_basedirs')
def _default_extra_template_basedirs(self):
Expand Down Expand Up @@ -549,7 +546,7 @@ def _template_paths(self, prune=True, root_dirs=None):
except OSError:
pass

return additional_paths + paths
return self.extra_template_paths + additional_paths + paths

def get_template_names(self):
# finds a list of template names where each successive template name is the base template
Expand Down
4 changes: 4 additions & 0 deletions nbconvert/exporters/tests/files/lablike.html.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{%- extends 'lab/index.html.j2' -%}
{%- block body_footer -%}
UNIQUE
{%- endblock body_footer -%}
9 changes: 9 additions & 0 deletions nbconvert/exporters/tests/test_templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,15 @@ def test_local_template_dir(self):
assert exporter.template_name == template
assert os.path.join(td, template) in exporter.template_paths

def test_local_template_file_extending_lab(self):
template_file = os.path.join(self._get_files_path(), 'lablike.html.j2')
exporter = HTMLExporter(template_file=template_file, template_name='lab')
nb = v4.new_notebook()
nb.cells.append(v4.new_code_cell("some_text"))
output, resources = exporter.from_notebook_node(nb)
assert "UNIQUE" in output


def test_raw_template_attr(self):
"""
Verify that you can assign a in memory template string by overwriting
Expand Down

0 comments on commit 42cfece

Please sign in to comment.