From 98c0239bd487ac965ddf3c25b44b3a96e0b41847 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Thu, 5 Aug 2021 00:28:26 +0200 Subject: [PATCH 1/5] Add `skip-execution` tag functionality --- nbclient/client.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/nbclient/client.py b/nbclient/client.py index 40ec273b..04aa3120 100644 --- a/nbclient/client.py +++ b/nbclient/client.py @@ -802,6 +802,10 @@ async def async_execute_cell( if cell.cell_type != 'code' or not cell.source.strip(): self.log.debug("Skipping non-executing cell %s", cell_index) return cell + + if "skip-execution" in cell.metadata.get("tags", []): + self.log.debug("Skipping tagged cell %s", cell_index) + return cell if self.record_timing and 'execution' not in cell['metadata']: cell['metadata']['execution'] = {} From b056ddb94f2103db7e318cf320deb4f513b1e28f Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Wed, 15 Sep 2021 08:57:08 +0200 Subject: [PATCH 2/5] Add skip_execution_tag option and test --- nbclient/client.py | 12 +++++- .../files/Skip Execution with Cell Tag.ipynb | 37 +++++++++++++++++++ nbclient/tests/test_client.py | 1 + 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 nbclient/tests/files/Skip Execution with Cell Tag.ipynb diff --git a/nbclient/client.py b/nbclient/client.py index 04aa3120..bb610b88 100644 --- a/nbclient/client.py +++ b/nbclient/client.py @@ -139,6 +139,16 @@ class NotebookClient(LoggingConfigurable): ), ).tag(config=True) + skip_execution_tag: str = Unicode( + 'skip-execution', + help=dedent( + """ + Name of the cell tag to use to, + to denote a cell that should be skipped. + """ + ), + ).tag(config=True) + extra_arguments: t.List = List(Unicode()).tag(config=True) kernel_name: str = Unicode( @@ -803,7 +813,7 @@ async def async_execute_cell( self.log.debug("Skipping non-executing cell %s", cell_index) return cell - if "skip-execution" in cell.metadata.get("tags", []): + if self.skip_execution_tag in cell.metadata.get("tags", []): self.log.debug("Skipping tagged cell %s", cell_index) return cell diff --git a/nbclient/tests/files/Skip Execution with Cell Tag.ipynb b/nbclient/tests/files/Skip Execution with Cell Tag.ipynb new file mode 100644 index 00000000..8b9c49a6 --- /dev/null +++ b/nbclient/tests/files/Skip Execution with Cell Tag.ipynb @@ -0,0 +1,37 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "source": [ + "print(\"a long running cell\")" + ], + "outputs": [], + "metadata": { + "tags": [ + "skip-execution" + ] + } + }, + { + "cell_type": "code", + "execution_count": 1, + "source": [ + "print('ok')" + ], + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "ok\n" + ] + } + ], + "metadata": {} + } + ], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 1 +} \ No newline at end of file diff --git a/nbclient/tests/test_client.py b/nbclient/tests/test_client.py index fa78f764..52351365 100644 --- a/nbclient/tests/test_client.py +++ b/nbclient/tests/test_client.py @@ -257,6 +257,7 @@ def notebook_resources(): ("JupyterWidgets.ipynb", dict(kernel_name="python")), ("Skip Exceptions with Cell Tags.ipynb", dict(kernel_name="python")), ("Skip Exceptions.ipynb", dict(kernel_name="python", allow_errors=True)), + ("Skip Execution with Cell Tag.ipynb", dict(kernel_name="python")), ("SVG.ipynb", dict(kernel_name="python")), ("Unicode.ipynb", dict(kernel_name="python")), ("UnicodePy3.ipynb", dict(kernel_name="python")), From 0cc31e163c04491fa8886f95e226f709887496b1 Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Wed, 15 Sep 2021 08:58:41 +0200 Subject: [PATCH 3/5] change name of option --- nbclient/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nbclient/client.py b/nbclient/client.py index bb610b88..27a3280d 100644 --- a/nbclient/client.py +++ b/nbclient/client.py @@ -139,7 +139,7 @@ class NotebookClient(LoggingConfigurable): ), ).tag(config=True) - skip_execution_tag: str = Unicode( + skip_cells_with_tag: str = Unicode( 'skip-execution', help=dedent( """ @@ -813,7 +813,7 @@ async def async_execute_cell( self.log.debug("Skipping non-executing cell %s", cell_index) return cell - if self.skip_execution_tag in cell.metadata.get("tags", []): + if self.skip_cells_with_tag in cell.metadata.get("tags", []): self.log.debug("Skipping tagged cell %s", cell_index) return cell From d00ec46de154966a1820bde9c072629367ead91f Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Wed, 15 Sep 2021 09:09:46 +0200 Subject: [PATCH 4/5] fix flake8 --- nbclient/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nbclient/client.py b/nbclient/client.py index 27a3280d..8f5110e6 100644 --- a/nbclient/client.py +++ b/nbclient/client.py @@ -812,7 +812,7 @@ async def async_execute_cell( if cell.cell_type != 'code' or not cell.source.strip(): self.log.debug("Skipping non-executing cell %s", cell_index) return cell - + if self.skip_cells_with_tag in cell.metadata.get("tags", []): self.log.debug("Skipping tagged cell %s", cell_index) return cell From 6f6b03bf60dfe20a608e3d6a23b0ce0f11fcf58f Mon Sep 17 00:00:00 2001 From: Chris Sewell Date: Wed, 15 Sep 2021 09:32:05 +0200 Subject: [PATCH 5/5] Update nbclient/client.py Co-authored-by: David Brochart --- nbclient/client.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/nbclient/client.py b/nbclient/client.py index 8f5110e6..2736f924 100644 --- a/nbclient/client.py +++ b/nbclient/client.py @@ -143,8 +143,7 @@ class NotebookClient(LoggingConfigurable): 'skip-execution', help=dedent( """ - Name of the cell tag to use to, - to denote a cell that should be skipped. + Name of the cell tag to use to denote a cell that should be skipped. """ ), ).tag(config=True)