Skip to content

Commit

Permalink
Make execute_ignore a list (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfrg authored Mar 18, 2023
1 parent ccb2040 commit 1799f8c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 15 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,16 @@ You can tell the plugin to execute the notebook before converting, default is `F
```yaml
plugins:
- mkdocs-jupyter:
execute: True
execute: true
```

You can tell the plugin to ignore the execution of some files (with glob matching):

```yaml
plugins:
- mkdocs-jupyter:
execute_ignore: "my-secret-files/*.ipynb"
execute_ignore:
- "my-secret-files/*.ipynb"
```

To fail when notebook execution fails set `allow_errors` to `false`:
Expand Down
20 changes: 12 additions & 8 deletions mkdocs_jupyter/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Plugin(mkdocs.plugins.BasePlugin):
("include", config_options.Type(list, default=["*.py", "*.ipynb"])),
("ignore", config_options.Type(list, default=[])),
("execute", config_options.Type(bool, default=False)),
("execute_ignore", config_options.Type(str, default="")),
("execute_ignore", config_options.Type(list, default=[])),
("theme", config_options.Type(str, default="")),
("kernel_name", config_options.Type(str, default="")),
("include_source", config_options.Type(bool, default=False)),
Expand Down Expand Up @@ -87,13 +87,17 @@ def on_pre_page(self, page, config, files):
no_input = self.config["no_input"]
remove_tag_config = self.config["remove_tag_config"]

if self.config["execute_ignore"]:
ignore_pattern = self.config["execute_ignore"]
execute_ignore = pathlib.PurePath(
page.file.abs_src_path
).match(ignore_pattern)
if execute_ignore:
exec_nb = False
print(self.config)
if (
self.config["execute_ignore"]
and len(self.config["execute_ignore"]) > 0
):
for ignore_pattern in self.config["execute_ignore"]:
ignore_this = pathlib.PurePath(
page.file.abs_src_path
).match(ignore_pattern)
if ignore_this:
exec_nb = False

theme = self.config["theme"]

Expand Down
3 changes: 2 additions & 1 deletion mkdocs_jupyter/tests/mkdocs/base-with-nbs-failure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ nav:
plugins:
- mkdocs-jupyter:
execute: true
execute_ignore: "*.py"
execute_ignore:
- "*.py"
include: ["fail.ipynb"]
ignore: ["demo.ipynb"]
allow_errors: false
Expand Down
39 changes: 39 additions & 0 deletions mkdocs_jupyter/tests/mkdocs/material-execute-ignore.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
site_name: site-with-notebooks-and-python-files
site_description: mkdocs-jupyter test site

nav:
- Home: index.md
- Demo (nb): demo.ipynb
- Equations (py): variational-inference-script.py

plugins:
- mkdocs-jupyter:
execute: true
execute_ignore:
- "ruby.ipynb" # We won't have the ruby kernel on the tests

markdown_extensions:
- toc:
permalink: true
- codehilite:
guess_lang: false
- pymdownx.highlight:
use_pygments: true
- pymdownx.arithmatex

theme:
name: material
# custom_dir: overrides
palette:
- scheme: default
toggle:
icon: material/toggle-switch-off-outline
name: Switch to dark mode
- scheme: slate
toggle:
icon: material/toggle-switch
name: Switch to light mode

extra_css:
- extras/material.css
- extras/styles.css
3 changes: 2 additions & 1 deletion mkdocs_jupyter/tests/mkdocs/material-with-pys.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ plugins:
- mkdocs-jupyter:
include_source: True
execute: True
execute_ignore: "*.ipynb"
execute_ignore:
- "*.ipynb"

markdown_extensions:
- codehilite:
Expand Down
7 changes: 4 additions & 3 deletions mkdocs_jupyter/tests/test_base_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
["base-with-nbs.yml", True],
["base-with-pys.yml", True],
["base-without-nbs.yml", True],
["material-execute-ignore.yml", True],
["material-with-nbs-pys.yml", True],
["material-with-nbs.yml", True],
["material-with-pys.yml", True],
["base-with-nbs-failure.yml", False],
],
)
def test_notebook_renders(input):
filename, renders = input
filename, should_work = input

this_dir = os.path.dirname(os.path.realpath(__file__))
config_file = os.path.join(this_dir, f"mkdocs/{filename}")

try:
build(load_config(config_file))
assert renders
assert should_work
except CellExecutionError:
assert not renders
assert not should_work

0 comments on commit 1799f8c

Please sign in to comment.