-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Add PR action to validate notebook format #1793
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a216e3d
Add PR action to validate notebook format
brandonbaker-openai 05f37b5
Invalid formatting test
brandonbaker-openai c0d0297
Valid change
brandonbaker-openai d586b40
PR comment
brandonbaker-openai 8b4b111
Registry fix
brandonbaker-openai 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,61 @@ | ||
import subprocess | ||
import sys | ||
from pathlib import Path | ||
|
||
import nbformat | ||
|
||
|
||
def get_changed_notebooks(base_ref: str = "origin/main") -> list[Path]: | ||
""" | ||
Returns a list of changed notebook paths in the current git branch | ||
compared to the specified base reference. | ||
""" | ||
result = subprocess.run( | ||
["git", "diff", "--name-only", base_ref, "--", "*.ipynb"], | ||
capture_output=True, | ||
text=True, | ||
check=True, | ||
) | ||
return [Path(line.strip()) for line in result.stdout.splitlines() if line.strip()] | ||
|
||
|
||
def is_valid_notebook(path: Path) -> bool: | ||
""" | ||
Checks if the notebook at the given path is valid by attempting to read it | ||
with nbformat. | ||
""" | ||
try: | ||
with open(path, "r", encoding="utf-8") as f: | ||
nbformat.read(f, as_version=4) | ||
return True | ||
except Exception as e: | ||
print(f"{path}: INVALID - {e}") | ||
return False | ||
|
||
|
||
def main() -> None: | ||
""" | ||
Main function to validate the format of changed notebooks. | ||
""" | ||
changed_notebooks = get_changed_notebooks() | ||
if not changed_notebooks: | ||
print("No changed .ipynb files to validate.") | ||
sys.exit(0) | ||
|
||
print(f"Validating {len(changed_notebooks)} notebook(s)...") | ||
errors = 0 | ||
for path in changed_notebooks: | ||
if not path.exists(): | ||
continue # skip deleted files | ||
if not is_valid_notebook(path): | ||
errors += 1 | ||
|
||
if errors: | ||
print(f"{errors} invalid notebook(s) found.") | ||
sys.exit(1) | ||
else: | ||
print("All changed notebooks are valid.") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,25 @@ | ||
name: Validate Changed Notebooks | ||
|
||
on: [pull_request] | ||
|
||
jobs: | ||
validate-notebooks: | ||
name: Validate Notebooks | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
with: | ||
fetch-depth: 0 # needed for git diff to work | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.12' | ||
|
||
- name: Install dependencies | ||
run: pip install nbformat | ||
|
||
- name: Validate changed .ipynb files | ||
run: python .github/scripts/check_notebooks.py |
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 |
---|---|---|
|
@@ -140,3 +140,6 @@ examples/fine-tuned_qa/local_cache/* | |
|
||
# PyCharm files | ||
.idea/ | ||
|
||
# VS Code files | ||
.vscode/ |
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
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.