From 1799f8c9104e4f94637d514067182ccb5ddcec61 Mon Sep 17 00:00:00 2001 From: Daniel Rodriguez Date: Sat, 18 Mar 2023 17:44:35 -0500 Subject: [PATCH] Make execute_ignore a list (#137) --- README.md | 5 ++- mkdocs_jupyter/plugin.py | 20 ++++++---- .../tests/mkdocs/base-with-nbs-failure.yml | 3 +- .../tests/mkdocs/material-execute-ignore.yml | 39 +++++++++++++++++++ .../tests/mkdocs/material-with-pys.yml | 3 +- mkdocs_jupyter/tests/test_base_usage.py | 7 ++-- 6 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 mkdocs_jupyter/tests/mkdocs/material-execute-ignore.yml diff --git a/README.md b/README.md index 1271d57..4b37371 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ 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): @@ -116,7 +116,8 @@ You can tell the plugin to ignore the execution of some files (with glob matchin ```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`: diff --git a/mkdocs_jupyter/plugin.py b/mkdocs_jupyter/plugin.py index 942515d..154b6da 100644 --- a/mkdocs_jupyter/plugin.py +++ b/mkdocs_jupyter/plugin.py @@ -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)), @@ -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"] diff --git a/mkdocs_jupyter/tests/mkdocs/base-with-nbs-failure.yml b/mkdocs_jupyter/tests/mkdocs/base-with-nbs-failure.yml index 6ee3493..efca878 100644 --- a/mkdocs_jupyter/tests/mkdocs/base-with-nbs-failure.yml +++ b/mkdocs_jupyter/tests/mkdocs/base-with-nbs-failure.yml @@ -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 diff --git a/mkdocs_jupyter/tests/mkdocs/material-execute-ignore.yml b/mkdocs_jupyter/tests/mkdocs/material-execute-ignore.yml new file mode 100644 index 0000000..cd53c69 --- /dev/null +++ b/mkdocs_jupyter/tests/mkdocs/material-execute-ignore.yml @@ -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 diff --git a/mkdocs_jupyter/tests/mkdocs/material-with-pys.yml b/mkdocs_jupyter/tests/mkdocs/material-with-pys.yml index 58c16aa..e2dc28e 100644 --- a/mkdocs_jupyter/tests/mkdocs/material-with-pys.yml +++ b/mkdocs_jupyter/tests/mkdocs/material-with-pys.yml @@ -10,7 +10,8 @@ plugins: - mkdocs-jupyter: include_source: True execute: True - execute_ignore: "*.ipynb" + execute_ignore: + - "*.ipynb" markdown_extensions: - codehilite: diff --git a/mkdocs_jupyter/tests/test_base_usage.py b/mkdocs_jupyter/tests/test_base_usage.py index fc19cb5..ba8155a 100644 --- a/mkdocs_jupyter/tests/test_base_usage.py +++ b/mkdocs_jupyter/tests/test_base_usage.py @@ -13,6 +13,7 @@ ["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], @@ -20,13 +21,13 @@ ], ) 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