-
Notifications
You must be signed in to change notification settings - Fork 54
Allow Notebook output to be preserved #771
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
657e4a4
Fix diff command
FlorentinD 0583008
Allow keeping certain output cells
FlorentinD a6e21bc
Document possible cell tags
FlorentinD 529e38b
Reflect ruff usage and notebook options in docs
FlorentinD ba40950
Describe ipynb -> doc
FlorentinD 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 hidden or 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,15 @@ | ||
# Examples | ||
|
||
This folder contains example notebooks on how to use the `graphdatascience` python client. | ||
|
||
|
||
## Custom tooling for notebooks | ||
|
||
As for the rest of this repository, you can run `../scripts/makestyle` and `../scripts/checkstyle` to apply and check the style of the notebook. | ||
By default, `makestyle` will remove all cell outputs. If you want to preserve some outputs, tag the cell with `preserve-output`. | ||
FlorentinD marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
To generate documenatation, you can run `../scripts/nb2doc/convert`. | ||
FlorentinD marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
If you only want to let CI run the notebook given a certain condition, tag a given cell in the notebook with `verify-version`. | ||
As the name suggests, the tag was introduced to only run for given GDS server versions. | ||
To make sure certain cells are run even in case of failure, tag the cell with `teardown`. |
This file contains hidden or 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 hidden or 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,75 @@ | ||
# reasons for not using nbconvert cli tool: | ||
# * cannot keep output based on a given tag | ||
|
||
import argparse | ||
import logging | ||
from enum import Enum | ||
from pathlib import Path | ||
|
||
import nbconvert | ||
from nbconvert.preprocessors import Preprocessor | ||
|
||
PRESERVE_CELL_OUTPUT_KEY = "preserve-output" | ||
METADATA_TAG_KEY = "tags" | ||
|
||
|
||
class OutputMode(Enum): | ||
STDOUT = "stdout" | ||
INPLACE = "inplace" | ||
|
||
|
||
class CustomClearOutputPreprocessor(Preprocessor): | ||
""" | ||
Removes the output from all code cells in a notebook. | ||
Option to keep cell output for cells with a given metadata tag | ||
""" | ||
|
||
def preprocess_cell(self, cell, resources, cell_index): | ||
""" | ||
Apply a transformation on each cell. See base.py for details. | ||
""" | ||
if cell.cell_type == "code" and PRESERVE_CELL_OUTPUT_KEY not in cell["metadata"].get(METADATA_TAG_KEY, []): | ||
cell.outputs = [] | ||
cell.execution_count = None | ||
return cell, resources | ||
|
||
|
||
def main(input_path: Path, output_mode: str) -> None: | ||
logger = logging.getLogger("NotebookCleaner") | ||
logger.info(f"Cleaning notebooks from `{input_path}`, mode: `{output_mode}`") | ||
|
||
exporter = nbconvert.NotebookExporter() | ||
|
||
metadata_cleaner = nbconvert.preprocessors.ClearMetadataPreprocessor(preserve_cell_metadata_mask=METADATA_TAG_KEY) | ||
output_cleaner = CustomClearOutputPreprocessor() | ||
|
||
exporter.register_preprocessor(metadata_cleaner, enabled=True) | ||
exporter.register_preprocessor(output_cleaner, enabled=True) | ||
|
||
if input_path.is_file(): | ||
notebooks = [input_path] | ||
else: | ||
notebooks = [f for f in input_path.iterdir() if f.is_file() and f.suffix == ".ipynb"] | ||
|
||
logger.info(f"Formatting {len(notebooks)} notebooks.") | ||
|
||
for notebook in notebooks: | ||
output = exporter.from_filename(notebook) | ||
|
||
formatted_notebook = output[0] | ||
|
||
if output_mode == OutputMode.INPLACE: | ||
with notebook.open(mode="w") as file: | ||
file.write(formatted_notebook) | ||
elif output_mode == OutputMode.STDOUT: | ||
print(formatted_notebook) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-o", "--output", choices=[e.value for e in OutputMode]) | ||
parser.add_argument("-i", "--input", default="examples", help="path to the notebook file or folder") | ||
|
||
args = parser.parse_args() | ||
|
||
main(Path(args.input), OutputMode(args.output)) |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.