From b62ad8b4bc90e8f7a4782396768f6e1c29cadfc9 Mon Sep 17 00:00:00 2001 From: Pan Date: Thu, 11 Jul 2024 16:17:27 -0400 Subject: [PATCH] added script and workflow --- .github/workflows/lint.py | 36 ++++++++++++++++++++++++ .github/workflows/notebook-lint.yaml | 41 ++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 .github/workflows/lint.py create mode 100644 .github/workflows/notebook-lint.yaml diff --git a/.github/workflows/lint.py b/.github/workflows/lint.py new file mode 100644 index 0000000..3f91768 --- /dev/null +++ b/.github/workflows/lint.py @@ -0,0 +1,36 @@ +import nbformat +from nbformat.v4 import new_notebook + +def clean_notebook(file_path): + with open(file_path, 'r', encoding='utf-8') as f: + notebook = nbformat.read(f, as_version=4) + + # Clean cells + for cell in notebook.cells: + if 'outputs' in cell: + cell['outputs'] = [] + if 'execution_count' in cell: + cell['execution_count'] = None + if 'metadata' in cell: + cell['metadata'] = {} + + # Clean notebook metadata + if 'metadata' in notebook: + notebook['metadata'] = {} + + with open(file_path, 'w', encoding='utf-8') as f: + nbformat.write(notebook, f) + + +if __name__ == "__main__": + import os + + # Change this to the directory containing your notebooks + notebook_dir = '../../' + + for root, dirs, files in os.walk(notebook_dir): + for file in files: + if file.endswith('.ipynb'): + file_path = os.path.join(root, file) + clean_notebook(file_path) + print(f'Cleaned {file_path}') diff --git a/.github/workflows/notebook-lint.yaml b/.github/workflows/notebook-lint.yaml new file mode 100644 index 0000000..634e0b3 --- /dev/null +++ b/.github/workflows/notebook-lint.yaml @@ -0,0 +1,41 @@ +name: 'Lint Notebook' +on: + push: + branches: + - dev/lint +permissions: + contents: write + id-token: write + +jobs: + gather_stats: + name: 'Gather Stats' + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up Python + working-directory: .github/workflows + uses: actions/setup-python@v5 + with: + python-version: '3.12' + cache: 'pip' + + - name: Install requirements.txt + run: | + python3 -m pip install --upgrade pip + pip3 install nbformat + + - name: Generate reports + working-directory: .github/workflows + run: | + python3 lint.py + + - name: Commit changes + uses: EndBug/add-and-commit@v9 + with: + author_name: github-action + author_email: cbiit-github-action@github.com + message: 'Github Action: Refresh stats'