Skip to content

Commit a9b1502

Browse files
authored
Add jupyterlite_ignore_contents config, mapping to --ignore-contents arg in JupyterLite build (#309)
* Add `jupyterlite_ignore_contents` config, mapping to `--ignore-contents` arg to JupyterLite build * Add documentation for new config * Add appropriate prefix to print call * Format 🔔
1 parent 4b751cf commit a9b1502

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

docs/configuration.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ jupyterlite_contents = ["./path/to/my/notebooks/", "my_other_notebook.ipynb"]
1212

1313
`jupyterlite_contents` can be a string or a list of strings. Each string is expanded using the Python `glob.glob` function with its recursive option. See the [glob documentation](https://docs.python.org/3/library/glob.html#glob.glob) and the [wildcard pattern documentation](https://docs.python.org/3/library/fnmatch.html#fnmatch.fnmatch) for more details. This option supports both paths relative to the docs source directory and absolute ones.
1414

15+
### Ignoring content
16+
17+
You can exclude some contents from your specified contents, for example:
18+
19+
```python
20+
jupyterlite_contents = ["./path/to/my/contents"]
21+
jupyterlite_ignore_contents = [r".*\.txt"]
22+
```
23+
24+
`jupyterlite_ignore_contents` can be a string or a list of strings. Strings are used as Python regular expressions to match and exclude files from your custom content. It's best to use raw string literals for your ignore strings, otherwise you'll need to double-escape (in other words, `r".*\.txt"` is more readable than `".*\\.txt"`).
25+
26+
Each string is passed directly to [the JupyterLite build `--ignore-contents` CLI argument](https://jupyterlite.readthedocs.io/en/stable/reference/cli.html#common-parameters).
27+
1528
## JupyterLite dir
1629

1730
By default, jupyterlite-sphinx runs the `jupyter lite build` command in the docs directory, you can overwrite this behavior and ask jupyterlite to build in a given directory:

jupyterlite_sphinx/jupyterlite_sphinx.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,22 @@ def inited(app: Sphinx, config):
947947
app.add_source_suffix(".ipynb", "jupyterlite_notebook")
948948

949949

950+
def jupyterlite_ignore_contents_args(ignore_contents):
951+
"""Generate `--ignore-contents` argument for each pattern.
952+
953+
NOTE: Unlike generating `--contents` args, we _do not_ expand globs to generate the
954+
arguments. We just hand the config off to the JupyterLite build.
955+
"""
956+
if ignore_contents is None:
957+
ignore_contents = []
958+
elif isinstance(ignore_contents, str):
959+
ignore_contents = [ignore_contents]
960+
961+
return [
962+
arg for pattern in ignore_contents for arg in ["--ignore-contents", pattern]
963+
]
964+
965+
950966
def jupyterlite_build(app: Sphinx, error):
951967
if error is not None:
952968
# Do not build JupyterLite
@@ -1014,6 +1030,10 @@ def jupyterlite_build(app: Sphinx, error):
10141030

10151031
contents.extend(["--contents", contents_path])
10161032

1033+
ignore_contents = jupyterlite_ignore_contents_args(
1034+
app.env.config.jupyterlite_ignore_contents,
1035+
)
1036+
10171037
apps_option = []
10181038
for liteapp in ["notebooks", "edit", "lab", "repl", "tree", "consoles"]:
10191039
apps_option.extend(["--apps", liteapp])
@@ -1032,6 +1052,7 @@ def jupyterlite_build(app: Sphinx, error):
10321052
*contents,
10331053
"--contents",
10341054
os.path.join(app.srcdir, CONTENT_DIR),
1055+
*ignore_contents,
10351056
"--output-dir",
10361057
os.path.join(app.outdir, JUPYTERLITE_DIR),
10371058
*apps_option,
@@ -1064,6 +1085,7 @@ def jupyterlite_build(app: Sphinx, error):
10641085
kwargs["stdout"] = subprocess.PIPE
10651086
kwargs["stderr"] = subprocess.PIPE
10661087

1088+
print(f"[jupyterlite-sphinx] Command: {command}")
10671089
completed_process: CompletedProcess[bytes] = subprocess.run(
10681090
command, cwd=app.srcdir, check=True, **kwargs
10691091
)
@@ -1108,6 +1130,7 @@ def setup(app):
11081130
app.add_config_value("jupyterlite_overrides", None, rebuild="html")
11091131
app.add_config_value("jupyterlite_dir", str(app.srcdir), rebuild="html")
11101132
app.add_config_value("jupyterlite_contents", None, rebuild="html")
1133+
app.add_config_value("jupyterlite_ignore_contents", None, rebuild="html")
11111134
app.add_config_value("jupyterlite_bind_ipynb_suffix", True, rebuild="html")
11121135
app.add_config_value("jupyterlite_silence", True, rebuild=True)
11131136
app.add_config_value("strip_tagged_cells", False, rebuild=True)

0 commit comments

Comments
 (0)