Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement env var for extra_template_basedirs #2028

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion nbconvert/exporters/templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,13 @@ def _raw_template_changed(self, change):

@default("extra_template_basedirs")
def _default_extra_template_basedirs(self):
return [os.getcwd()]
basedirs = [os.getcwd()]
env = os.environ
if "NBCONVERT_EXTRA_TEMPLATE_BASEDIR" in env:
extra_template_path = env["NBCONVERT_EXTRA_TEMPLATE_BASEDIR"]
if os.path.exists(extra_template_path):
basedirs.append(extra_template_path)
return basedirs

# Extension that the template files use.
template_extension = Unicode().tag(config=True, affects_environment=True)
Expand Down
33 changes: 33 additions & 0 deletions nbconvert/exporters/tests/test_templateexporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import json
import os
from concurrent.futures import ProcessPoolExecutor
from contextlib import contextmanager
from tempfile import TemporaryDirectory
from unittest.mock import patch

Expand All @@ -32,6 +33,20 @@
"""


@contextmanager
def temp_environ(**env):
for key, value in env.items():
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
try:
yield
finally:
for key, _ in env.items():
os.environ.pop(key, None)


class SampleExporter(TemplateExporter):
"""
Exports a Python code file.
Expand Down Expand Up @@ -268,6 +283,24 @@ def test_absolute_template_dir(self):
assert exporter.template_name == template
assert os.path.join(td, template) in exporter.template_paths

def test_env_var_template_dir(self):
with TemporaryDirectory() as td:
template = "env-var"
template_file = os.path.join(td, template, "index.py.j2")
template_dir = os.path.dirname(template_file)
os.mkdir(template_dir)
test_output = "env-var!"
with open(template_file, "w") as f:
f.write(test_output)
with temp_environ(NBCONVERT_EXTRA_TEMPLATE_BASEDIRS=template_dir):
config = Config()
config.TemplateExporter.template_name = template
config.TemplateExporter.extra_template_basedirs = [td, template_dir]
exporter = self._make_exporter(config=config)
assert exporter.template.filename == template_file
assert exporter.template_name == template
assert template_dir in exporter.template_paths

def test_local_template_dir(self):
with TemporaryDirectory() as td, _contextlib_chdir.chdir(td): # noqa
with patch("os.getcwd", return_value=os.path.abspath(td)):
Expand Down