Skip to content
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

Update .ipynb notebooks from .py in pre-commit hook locally #537

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 4 additions & 57 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,65 +14,16 @@ defaults:
shell: bash

jobs:
convert_scripts:
name: Translate scripts to notebooks
runs-on: ubuntu-latest
permissions:
pull-requests: write
contents: write
outputs:
commit_hash: ${{ steps.add-commit-push.outputs.commit_hash }}
container:
image: ghcr.io/ptb-mr/mrpro_py311:latest
options: --user runner
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
token: ${{ secrets.commit }}
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: 0

- name: Install mrpro and dependencies
run: pip install --upgrade --upgrade-strategy "eager" .[notebook]

- name: Translate scripts to notebooks
run: |
scripts=$(ls ./examples/*.py)
for script in $scripts
do jupytext --set-kernel "python3" --update --output ${script//.py/.ipynb} $script
done

- name: Check if any notebooks have been changed
uses: tj-actions/verify-changed-files@v20
id: verify-changed-notebooks
with:
files: ./examples/*.ipynb

- name: Commit notebooks
if: steps.verify-changed-notebooks.outputs.files_changed == 'true'
uses: actions4git/add-commit-push@v1
with:
commit-message: Notebooks updated

- name: Get hash of last commit
id: add-commit-push
run: echo "commit_hash=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT

get_notebooks:
name: Get list of notebooks
needs: convert_scripts
runs-on: ubuntu-latest
steps:
- name: Checkout mrpro repo
uses: actions/checkout@v4
with:
ref: ${{ needs.convert_scripts.outputs.commit_hash }}

- id: set-matrix
run: |
cd ./examples/
ls
cd ./examples/notebooks
echo "notebooks=$(ls *.ipynb | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT

- name: Notebook overview
Expand All @@ -83,7 +34,7 @@ jobs:

run_notebook:
name: Run notebook
needs: [convert_scripts, get_notebooks]
needs: get_notebooks
runs-on: ubuntu-latest
permissions:
pull-requests: write
Expand All @@ -98,8 +49,6 @@ jobs:
steps:
- name: Checkout repo
uses: actions/checkout@v4
with:
ref: ${{ needs.convert_scripts.outputs.commit_hash }}

- name: Install mrpro and dependencies
run: pip install --upgrade --upgrade-strategy "eager" .[notebook]
Expand All @@ -113,7 +62,7 @@ jobs:
env:
RUNNER: ${{ toJson(runner) }}
with:
notebook: ./examples/${{ matrix.notebook }}
notebook: ./examples/notebooks/${{ matrix.notebook }}

- name: Get artifact names
id: artifact_names
Expand All @@ -133,7 +82,7 @@ jobs:

create_documentation:
name: Build and deploy documentation
needs: [convert_scripts, run_notebook]
needs: run_notebook
runs-on: ubuntu-latest
container:
image: ghcr.io/ptb-mr/mrpro_py311:latest
Expand All @@ -144,8 +93,6 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
ref: ${{ needs.convert_scripts.outputs.commit_hash }}

- name: Install mrpro and dependencies
run: pip install --upgrade --upgrade-strategy "eager" .[docs]
Expand Down
22 changes: 22 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ repos:
- "--index-url=https://download.pytorch.org/whl/cpu"
- "--extra-index-url=https://pypi.python.org/simple"

- repo: https://github.com/kynan/nbstripout
rev: 0.8.0
hooks:
# cleans the .ipynbs (removes outputs, resets all cell-ids to 0..N, cleans steps)
# also clean any kernel information left after execution
- id: nbstripout
name: clean .ipynb output
args: [--extra-keys, "metadata.language_info"]
files: examples/notebooks

- repo: https://github.com/mwouts/jupytext
rev: v1.16.4
hooks:
- id: jupytext
name: sync .py and .ipynb
args:
- --sync
- --update-metadata
- '{"kernelspec": {"display_name": "Python 3 (ipykernel)","language": "python","name": "python3"}}'
- --set-formats
- examples/notebooks//ipynb,examples/scripts//py:percent
files: '^examples/(notebooks/.*\.ipynb|scripts/.*\.py)$'
ci:
autofix_commit_msg: |
[pre-commit] auto fixes from pre-commit hooks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "markdown",
"id": "7ce284e1",
"id": "0",
"metadata": {
"lines_to_next_cell": 0
},
Expand All @@ -14,7 +14,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "a203bdc2",
"id": "1",
"metadata": {
"lines_to_next_cell": 0
},
Expand All @@ -28,7 +28,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "0c08b3fd",
"id": "2",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -45,7 +45,7 @@
},
{
"cell_type": "markdown",
"id": "e0126c1e",
"id": "3",
"metadata": {
"lines_to_next_cell": 0
},
Expand All @@ -59,7 +59,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "f041501b",
"id": "4",
"metadata": {
"lines_to_next_cell": 0
},
Expand All @@ -80,7 +80,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "4108db0a",
"id": "5",
"metadata": {
"lines_to_next_cell": 0
},
Expand All @@ -96,7 +96,7 @@
},
{
"cell_type": "markdown",
"id": "d669bbd9",
"id": "6",
"metadata": {
"lines_to_next_cell": 0
},
Expand All @@ -108,7 +108,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "a4afe6b9",
"id": "7",
"metadata": {
"lines_to_next_cell": 0
},
Expand Down Expand Up @@ -136,7 +136,7 @@
},
{
"cell_type": "markdown",
"id": "b558334f",
"id": "8",
"metadata": {
"lines_to_next_cell": 0
},
Expand All @@ -148,7 +148,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "d96152f2",
"id": "9",
"metadata": {
"lines_to_next_cell": 0
},
Expand Down Expand Up @@ -180,7 +180,7 @@
},
{
"cell_type": "markdown",
"id": "564c18d7",
"id": "10",
"metadata": {
"lines_to_next_cell": 0
},
Expand All @@ -192,7 +192,7 @@
{
"cell_type": "code",
"execution_count": null,
"id": "52d306e3",
"id": "11",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -206,7 +206,8 @@
],
"metadata": {
"jupytext": {
"cell_metadata_filter": "-all"
"cell_metadata_filter": "-all",
"formats": "examples/notebooks//ipynb,examples/scripts//py:percent"
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
Expand Down
Loading