From 13ba4b101305ef8224b5300bbde4c7b57681a39f Mon Sep 17 00:00:00 2001 From: John Turner Date: Wed, 5 Feb 2025 20:56:16 +0000 Subject: [PATCH] added ability to not clean cells with tags of quiz or noclean. Keep kernelspec and language_info metadata for notebook --- .github/workflows/lint.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/lint.py b/.github/workflows/lint.py index ff157b1..36e4218 100644 --- a/.github/workflows/lint.py +++ b/.github/workflows/lint.py @@ -9,6 +9,10 @@ def clean_notebook(file_path): # Clean cells for cell in notebook.cells: + # skip cells with a quiz or no clean tag + if 'metadata' in cell and 'tags' in cell['metadata'] and ( + 'quiz' in cell['metadata']['tags'] or 'noclean' in cell['metadata']['tags']): + continue if 'outputs' in cell: cell['outputs'] = [] if 'execution_count' in cell: @@ -18,7 +22,12 @@ def clean_notebook(file_path): # Clean notebook metadata if 'metadata' in notebook: - notebook['metadata'] = {} + new_metadata = {} + if 'language_info' in notebook['metadata']: + new_metadata['language_info'] = notebook['metadata']['language_info'] + if 'kernelspec' in notebook['metadata']: + new_metadata['kernelspec'] = notebook['metadata']['kernelspec'] + notebook['metadata'] = new_metadata with open(file_path, 'w', encoding='utf-8') as f: nbformat.write(notebook, f)