Skip to content

Commit 30af202

Browse files
committed
env var for extra_template_basedirs
This commit add the following three items. - Updates the method `_default_extra_template_basedirs` in the module `templateexporter.py` to search for a new environment variable called `NBCONVERT_EXTRA_TEMPLATE_BASEDIRS`. This environment variable will be added to the basedirs list if it is found, and it exists. - Updates the tests to ensure if the new environment variable exists then it is included in the `TemplateExporter.template_paths`. - Adds a context manager method that modifies the `os.environ` dictionary. Closes jupyter#2026
1 parent ca1e94e commit 30af202

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

nbconvert/exporters/templateexporter.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,13 @@ def _raw_template_changed(self, change):
260260

261261
@default("extra_template_basedirs")
262262
def _default_extra_template_basedirs(self):
263-
return [os.getcwd()]
263+
basedirs = [os.getcwd()]
264+
env = os.environ
265+
if "NBCONVERT_EXTRA_TEMPLATE_BASEDIR" in env:
266+
extra_template_path = env["NBCONVERT_EXTRA_TEMPLATE_BASEDIR"]
267+
if os.path.exists(extra_template_path):
268+
basedirs.append(extra_template_path)
269+
return basedirs
264270

265271
# Extension that the template files use.
266272
template_extension = Unicode().tag(config=True, affects_environment=True)

nbconvert/exporters/tests/test_templateexporter.py

+33
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import json
99
import os
1010
from concurrent.futures import ProcessPoolExecutor
11+
from contextlib import contextmanager
1112
from tempfile import TemporaryDirectory
1213
from unittest.mock import patch
1314

@@ -32,6 +33,20 @@
3233
"""
3334

3435

36+
@contextmanager
37+
def temp_environ(**env):
38+
for key, value in env.items():
39+
if value is None:
40+
os.environ.pop(key, None)
41+
else:
42+
os.environ[key] = value
43+
try:
44+
yield
45+
finally:
46+
for key, _ in env.items():
47+
os.environ.pop(key, None)
48+
49+
3550
class SampleExporter(TemplateExporter):
3651
"""
3752
Exports a Python code file.
@@ -268,6 +283,24 @@ def test_absolute_template_dir(self):
268283
assert exporter.template_name == template
269284
assert os.path.join(td, template) in exporter.template_paths
270285

286+
def test_env_var_template_dir(self):
287+
with TemporaryDirectory() as td:
288+
template = "env-var"
289+
template_file = os.path.join(td, template, "index.py.j2")
290+
template_dir = os.path.dirname(template_file)
291+
os.mkdir(template_dir)
292+
test_output = "env-var!"
293+
with open(template_file, "w") as f:
294+
f.write(test_output)
295+
with temp_environ(NBCONVERT_EXTRA_TEMPLATE_BASEDIRS=template_dir):
296+
config = Config()
297+
config.TemplateExporter.template_name = template
298+
config.TemplateExporter.extra_template_basedirs = [td, template_dir]
299+
exporter = self._make_exporter(config=config)
300+
assert exporter.template.filename == template_file
301+
assert exporter.template_name == template
302+
assert template_dir in exporter.template_paths
303+
271304
def test_local_template_dir(self):
272305
with TemporaryDirectory() as td, _contextlib_chdir.chdir(td): # noqa
273306
with patch("os.getcwd", return_value=os.path.abspath(td)):

0 commit comments

Comments
 (0)