Skip to content

Commit

Permalink
env var for extra_template_basedirs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ndmlny-qs committed Aug 3, 2023
1 parent ca1e94e commit 30af202
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
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

0 comments on commit 30af202

Please sign in to comment.