Skip to content

Commit 9d59522

Browse files
Allow passing additional configuration options to the jupyter lite build command (#169)
Co-authored-by: Albert Steppi <albert.steppi@gmail.com>
1 parent 3313f9f commit 9d59522

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

docs/configuration.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,30 @@ jupyterlite_silence = False
7575
```
7676

7777
in your Sphinx `conf.py`.
78+
79+
## Additional CLI arguments for `jupyter lite build`
80+
81+
Additional arguments can be passed to the `jupyter lite build` command using the configuration
82+
option `jupyterlite_build_command_options` in `conf.py`. The following example shows how to
83+
specify an alternative location for the `xeus` kernel's `environment.yml` file as discussed
84+
[here](https://github.com/jupyterlite/xeus#usage).
85+
86+
```python
87+
jupyterlite_build_command_options = {
88+
"XeusAddon.environment_file": "jupyterlite_environment.yml",
89+
}
90+
```
91+
92+
This causes the additional option `--XeusAddon.environment_file=jupyterlite_environment.yml`
93+
to be passed to `jupyter lite build` internally within `jupyterlite-sphinx`. Note that one
94+
does not include the leading dashes, `--`, in the keys.
95+
96+
The options `--contents`, `--output-dir`, and `--lite-dir` cannot be passed to `jupyter lite build` in this way.
97+
These can instead be set with
98+
the [`jupyterlite_contents`](#jupyterlite-content) and the[`jupyterlite_dir`](#jupyterlite-dir) configuration
99+
options described above.
100+
101+
This is an advanced feature and users are responsible for providing sensible command line options.
102+
The standard precedence between `jupyter lite build` CLI options and other means of configuration apply.
103+
See the [jupyter lite CLI](https://jupyterlite.readthedocs.io/en/latest/reference/cli.html) documentation
104+
for more info.

jupyterlite_sphinx/jupyterlite_sphinx.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,10 @@ def jupyterlite_build(app: Sphinx, error):
572572

573573
jupyterlite_dir = str(app.env.config.jupyterlite_dir)
574574

575+
jupyterlite_build_command_options: Dict[str, Any] = (
576+
app.env.config.jupyterlite_build_command_options
577+
)
578+
575579
config = []
576580
if jupyterlite_config:
577581
config = ["--config", jupyterlite_config]
@@ -614,6 +618,22 @@ def jupyterlite_build(app: Sphinx, error):
614618
jupyterlite_dir,
615619
]
616620

621+
if jupyterlite_build_command_options is not None:
622+
for key, value in jupyterlite_build_command_options.items():
623+
# Check for conflicting options from the default command we use
624+
# while building. We don't want to allow these to be overridden
625+
# unless they are explicitly set through Sphinx config.
626+
if key in ["contents", "output-dir", "lite-dir"]:
627+
jupyterlite_command_error_message = f"""
628+
Additional option, {key}, passed to `jupyter lite build` through
629+
`jupyterlite_build_command_options` in conf.py is already an existing
630+
option. "contents", "output_dir", and "lite_dir" can be configured in
631+
conf.py as described in the jupyterlite-sphinx documentation:
632+
https://jupyterlite-sphinx.readthedocs.io/en/stable/configuration.html
633+
"""
634+
raise RuntimeError(jupyterlite_command_error_message)
635+
command.extend([f"--{key}", str(value)])
636+
617637
assert all(
618638
[isinstance(s, str) for s in command]
619639
), f"Expected all commands arguments to be a str, got {command}"
@@ -669,6 +689,9 @@ def setup(app):
669689
app.add_config_value("jupyterlite_bind_ipynb_suffix", True, rebuild="html")
670690
app.add_config_value("jupyterlite_silence", True, rebuild=True)
671691

692+
# Pass a dictionary of additional options to the JupyterLite build command
693+
app.add_config_value("jupyterlite_build_command_options", None, rebuild="html")
694+
672695
app.add_config_value("global_enable_try_examples", default=False, rebuild=True)
673696
app.add_config_value("try_examples_global_theme", default=None, rebuild=True)
674697
app.add_config_value("try_examples_global_warning_text", default=None, rebuild=True)

0 commit comments

Comments
 (0)