-
Notifications
You must be signed in to change notification settings - Fork 429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement --remove-tagged-cells #738
Open
astrofrog
wants to merge
7
commits into
nteract:main
Choose a base branch
from
astrofrog:remove-tagged-cells
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
38a6f06
Initial implementation of remove_tagged_cells
astrofrog 7ad32a0
Added test notebook
astrofrog 332509e
Added test of remove_tagged_cells in execute_notebook
astrofrog f6ff21b
Merge branch 'main' into remove-tagged-cells
MSeal fcbbd84
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bd11e11
Merge branch 'main' into remove-tagged-cells
willingc 5a585ca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from copy import deepcopy | ||
from pathlib import Path | ||
|
||
import nbformat | ||
|
@@ -18,6 +19,7 @@ def execute_notebook( | |
engine_name=None, | ||
request_save_on_cell_execute=True, | ||
prepare_only=False, | ||
remove_tagged_cells=None, | ||
kernel_name=None, | ||
language=None, | ||
progress_bar=True, | ||
|
@@ -47,6 +49,9 @@ def execute_notebook( | |
How often in seconds to save in the middle of long cell executions | ||
prepare_only : bool, optional | ||
Flag to determine if execution should occur or not | ||
remove_tagged_cells : str, optional | ||
If specified, cells with the specified tag will be removed | ||
before execution and will not be present in the output notebook. | ||
kernel_name : str, optional | ||
Name of kernel to execute the notebook against | ||
language : str, optional | ||
|
@@ -105,6 +110,8 @@ def execute_notebook( | |
) | ||
|
||
nb = prepare_notebook_metadata(nb, input_path, output_path, report_mode) | ||
if remove_tagged_cells is not None: | ||
nb = remove_tagged_cells_from_notebook(nb, remove_tagged_cells) | ||
# clear out any existing error markers from previous papermill runs | ||
nb = remove_error_markers(nb) | ||
|
||
|
@@ -164,6 +171,33 @@ def prepare_notebook_metadata(nb, input_path, output_path, report_mode=False): | |
return nb | ||
|
||
|
||
def remove_tagged_cells_from_notebook(nb, tag): | ||
""" | ||
Remove cells with a matching tag. | ||
|
||
Parameters | ||
---------- | ||
nb : NotebookNode | ||
Executable notebook object | ||
tag : str | ||
Tag to used to identify cells to remove. | ||
""" | ||
|
||
# Copy the notebook to avoid changing the input one | ||
nb = deepcopy(nb) | ||
Comment on lines
+186
to
+187
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. E.g. |
||
|
||
# Filter out cells containing the tag | ||
cells = [] | ||
for cell in nb.cells: | ||
if hasattr(cell, 'metadata') and 'tags' in cell.metadata: | ||
if tag not in cell.metadata['tags']: | ||
cells.append(cell) | ||
|
||
nb.cells = cells | ||
|
||
return nb | ||
|
||
|
||
ERROR_MARKER_TAG = "papermill-error-cell-tag" | ||
|
||
ERROR_STYLE = 'style="color:red; font-family:Helvetica Neue, Helvetica, Arial, sans-serif; font-size:2em;"' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "a0730871-38e0-4f31-9466-1a117965e5a1", | ||
"metadata": {}, | ||
"source": [ | ||
"### Markdown cell" | ||
] | ||
}, | ||
{ | ||
"cell_type": "raw", | ||
"id": "00000b67-913e-459a-80dc-2520b2483d7d", | ||
"metadata": {}, | ||
"source": [ | ||
"Raw cell" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "7979d49a-abb1-4815-9534-ad76e4505b56", | ||
"metadata": { | ||
"tags": [ | ||
"assigncell" | ||
] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"a = 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "778581dd-f385-4039-be97-4050615fa271", | ||
"metadata": { | ||
"tags": [ | ||
"printcell" | ||
] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"print(a)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be enough – the empty string isn't a valid tag, right..?