Skip to content

Commit c40117c

Browse files
authored
Add workflow to validate and fix Jupyter notebooks
This workflow validates and fixes Jupyter notebook files in pull requests by ensuring proper formatting and widget state.
1 parent c5a666a commit c40117c

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Validate and Fix Notebook
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
permissions:
9+
contents: write
10+
11+
jobs:
12+
validate-and-fix-notebook:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Checkout repository
17+
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
20+
21+
- name: Set up Python
22+
uses: actions/setup-python@v4
23+
with:
24+
python-version: '3.x'
25+
26+
- name: Install Jupyter and nbformat
27+
run: |
28+
pip install jupyter nbformat
29+
30+
- name: Validate and Fix Notebook
31+
run: |
32+
python -c "
33+
import nbformat
34+
import glob
35+
for file in glob.glob('**/*.ipynb', recursive=True):
36+
with open(file, 'r') as f:
37+
nb = nbformat.read(f, as_version=4)
38+
nbformat.validate(nb)
39+
if 'widgets' in nb.metadata:
40+
if 'application/vnd.jupyter.widget-state+json' not in nb.metadata['widgets']:
41+
nb.metadata['widgets']['application/vnd.jupyter.widget-state+json'] = {'version': '1.0', 'state': {}}
42+
elif 'state' not in nb.metadata['widgets']['application/vnd.jupyter.widget-state+json']:
43+
nb.metadata['widgets']['application/vnd.jupyter.widget-state+json']['state'] = {}
44+
with open(file, 'w') as f:
45+
nbformat.write(nb, f)
46+
"
47+
48+
- name: Configure Git
49+
run: |
50+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
51+
git config --global user.name "github-actions[bot]"
52+
53+
- name: Commit changes
54+
run: |
55+
git fetch origin
56+
git checkout -b ${{ github.event.pull_request.head.ref }} origin/${{ github.event.pull_request.head.ref }}
57+
git add -A
58+
git commit -m "Fix notebook format issues" || echo "No changes to commit"
59+
git pull --rebase origin ${{ github.event.pull_request.head.ref }} || echo "No rebase needed"
60+
git push origin HEAD:${{ github.event.pull_request.head.ref }}

0 commit comments

Comments
 (0)