Skip to content

Commit 7daf879

Browse files
allow configuring the path of the contents directory (#346)
* support customizing the contents dir * cleanup before rerunning instead of after finishing This will make sure that timing problems will never arise, and if the contents are at `_build/contents` (or rather, `_build/jupyterlite-contents`) cleaning up after the end of the process is not really required. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4aa78a3 commit 7daf879

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

jupyterlite_sphinx/jupyterlite_sphinx.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,10 @@ def run(self):
607607

608608
self.env.jupyterlite_notebooks.add(str(notebook_path))
609609

610-
notebooks_dir = Path(self.env.app.srcdir) / CONTENT_DIR
610+
notebooks_dir = (
611+
Path(self.env.app.srcdir) / self.env.config.jupyterlite_content_dir
612+
)
613+
611614
os.makedirs(notebooks_dir, exist_ok=True)
612615

613616
self._assert_no_conflicting_nb_names(notebook_path, notebooks_dir)
@@ -839,7 +842,9 @@ def run(self):
839842
nb.cells.insert(1, new_code_cell(preamble))
840843

841844
self.content = None
842-
notebooks_dir = Path(self.env.app.srcdir) / CONTENT_DIR
845+
notebooks_dir = (
846+
Path(self.env.app.srcdir) / self.env.config.jupyterlite_content_dir
847+
)
843848
notebook_unique_name = f"{uuid4()}.ipynb".replace("-", "_")
844849
self.env.temp_data["generated_notebooks"][
845850
directive_key
@@ -956,8 +961,13 @@ def conditional_process_examples(app, config):
956961

957962

958963
def inited(app: Sphinx, config):
964+
if not app.config.jupyterlite_content_dir:
965+
raise ValueError("jupyterlite_content_dir must be a non-zero string")
966+
content_dir = Path(app.srcdir) / app.config.jupyterlite_content_dir
967+
shutil.rmtree(content_dir, ignore_errors=True)
968+
959969
# Create the content dir
960-
os.makedirs(os.path.join(app.srcdir, CONTENT_DIR), exist_ok=True)
970+
content_dir.mkdir(exist_ok=True, parents=True)
961971

962972
if (
963973
config.jupyterlite_bind_ipynb_suffix
@@ -1071,7 +1081,7 @@ def jupyterlite_build(app: Sphinx, error):
10711081
*overrides,
10721082
*contents,
10731083
"--contents",
1074-
os.path.join(app.srcdir, CONTENT_DIR),
1084+
os.path.join(app.srcdir, app.env.config.jupyterlite_content_dir),
10751085
*ignore_contents,
10761086
"--output-dir",
10771087
os.path.join(app.outdir, JUPYTERLITE_DIR),
@@ -1135,7 +1145,6 @@ def jupyterlite_build(app: Sphinx, error):
11351145

11361146
# Cleanup
11371147
try:
1138-
shutil.rmtree(os.path.join(app.srcdir, CONTENT_DIR))
11391148
os.remove(".jupyterlite.doit.db")
11401149
except FileNotFoundError:
11411150
pass
@@ -1171,6 +1180,7 @@ def setup(app):
11711180
rebuild="html",
11721181
)
11731182
app.add_config_value("try_examples_preamble", default=None, rebuild="html")
1183+
app.add_config_value("jupyterlite_content_dir", default=CONTENT_DIR, rebuild="html")
11741184

11751185
# Allow customising the button text for each directive (this is useful
11761186
# only when "new_tab" is set to True)

0 commit comments

Comments
 (0)