Github 작업으로 Jupyter Notebook diff 가져오기

여러분, 안녕하세요! 저는 Canas이고 이것은 DEV에 대한 제 첫 번째 게시물입니다. 데이터 과학 및 머신 러닝 분야에서 일하는 사람들에게 유용할 수 있기를 바랍니다 :)


노트북으로 작업할 때 알려진 문제 중 하나는 버전 제어가 이상적이지 않으며 실제로 이를 처리할 수 있는 도구가 거의 없다는 것입니다. 이로 인해 저는 ndime이라는 기존 오픈 소스 도구를 활용하여 Github Actions를 사용하여 이러한 파일로 작업하는 일부 엔지니어의 부담을 줄이고자 노력했습니다.

내 워크플로우



일반적인 아이디어는 대상 브랜치와 관련하여 모든 노트북에 대한 변경 사항을 포함하는 PR에 대한 코멘트를 Github Action 포스트로 만드는 것입니다.

이를 위해 다음과 같은 기존 조치를 사용할 것입니다.
  • checkout@v2 , 코드 가져오기용
  • actions/setup-python@v1 , Python 설치용
  • peter-evans/create-or-update-comment@v1 , nbdiff 의 출력으로 PR에 댓글을 작성합니다.

  • 제출 카테고리:



    이것은 노트북이 공유 저장소에 제출될 때(예: 연구 또는 지속 실험을 위해) 훨씬 더 나은 컨텍스트를 제공하기 때문에 유지 관리자 필수 항목에 속할 것이라고 생각합니다.

    Yaml 파일 또는 코드 링크



    this repository 에서 작동하는 구현을 볼 수 있습니다.
  • YAML file
  • PR with GH Action comment


  • name: Generate notebook diff
    
    on: ["pull_request"]
    
    jobs:
      check-diff:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
            with:
              fetch-depth: 0
    
          - name: Fetch target branch
            run: git fetch origin ${{ github.event.pull_request.base.ref }}:${{ github.event.pull_request.base.ref }}
    
          - name: Setup Python
            uses: actions/setup-python@v1
            with:
              python-version: "3.6"
    
          - name: Install requirements
            run: pip3 install nbdime
    
          - name: Run and store diff
            run: |
              nbdiff ${{ github.event.pull_request.base.ref }} --no-color > diff.log
              sed -i '1s/^/\`\`\`diff\n&/' diff.log
              sed -i '$s/$/\n&\`\`\`/' diff.log
    
          - name: Get comment body
            id: get-comment-body
            run: |
              body=$(cat diff.log)
              body="${body//'%'/'%25'}"
              body="${body//$'\n'/'%0A'}"
              body="${body//$'\r'/'%0D'}"
              echo ::set-output name=body::$body
    
          - name: Create comment
            uses: peter-evans/create-or-update-comment@v1
            with:
              issue-number: ${{ github.event.pull_request.number }}
              body: ${{ steps.get-comment-body.outputs.body }}
    
    

    간단히 말해서 nbdiff를 사용하여 diff.log라는 파일을 생성합니다. 그런 다음 sed를 사용하여 마크다운 둘러싸는 문자를 추가하고 앞에 추가합니다. 다음 단계에서는 diff.log를 가져오고 PR 주석will not truncate newlinesbody 변수에 저장되도록 추가 교체를 수행합니다. 마지막으로 body 변수를 create-or-update-comment 작업에 전달하여 형식이 지정된 출력을 PR에 게시합니다.


    카나스 / 테스트-nbdiff-github-액션






    이 리포지토리는 사용 가능한 경우 Jupyter Notebook diff(vía nbdime)로 PR에 주석을 다는 Github 작업을 시도하기 위한 것입니다. 샘플은 공개 PR에서만 제공됩니다.
    nbdiff.yaml
    name: Generate notebook diff
    on: ["pull_request"]
    jobs
      check-diff
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
            with:
              fetch-depth: 0
    
          - name: Fetch target branch
            run: git fetch origin ${{ github.event.pull_request.base.ref }}:${{ github.event.pull_request.base.ref }}
    
          - name: Setup Python
            uses: actions/setup-python@v1
            with:
              python-version: "3.6"
    
          - name: Install requirements
            run: pip3 install nbdime
    
          - name: Run and store diff
            run: |
              nbdiff ${{ github.event.pull_request.base.ref }} --no-color > diff.log
              sed -i '1s/^/```diff\n&/' diff.log
              sed -i '$s/$/\n&```/' diff.log
    
          - name: Get comment body
            id: get-comment-body
            run: |
              body=$(cat diff.log)
              body="${body//'%'/'%25'}"
              body="${body//$'\n'/'%0A'}"
              body="${body//$'\r'/'%0D'}"
              echo ::set-output name=body::$body
    
          - name: Create comment
            uses: peter-evans/create-or-update-comment@v1


    View on GitHub

    *실제 파일을 보고 싶다면 change 브랜치로 체크아웃하세요!

    추가 리소스/정보



    사용한 작업:
  • checkout@v2
  • actions/setup-python@v1
  • peter-evans/create-or-update-comment@v1

  • 사용한 라이브러리:
  • nbdime

  • 가능한 향후 작업:
  • 대형 노트북에 대한 테스트 및 벤치마크
  • nbdime의 웹 버전을 배포하는 방법을 찾습니다. nbdiff-web
  • 좋은 웹페이지 즐겨찾기