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

Add a script to validate jupiter notebooks #848

Merged
merged 32 commits into from
May 10, 2024

Conversation

abhiram6121
Copy link
Contributor

Description

Add Jupyter notebooks to CI #726

Checklist:

  • I have added tests to cover my changes.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.

Copy link

copy-pr-bot bot commented Nov 1, 2023

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Copy link

github-actions bot commented Nov 1, 2023

CLA Assistant Lite bot All Contributors have signed the CLA.

@abhiram6121
Copy link
Contributor Author

I have read the Contributor License Agreement and I hereby accept the Terms.

@bmhowe23
Copy link
Collaborator

bmhowe23 commented Nov 1, 2023

/ok to test

Command Bot: Processing...

Copy link

github-actions bot commented Nov 1, 2023

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Nov 1, 2023
@bmhowe23
Copy link
Collaborator

bmhowe23 commented Nov 1, 2023

Thanks, @abhiram6121. Please take a look at https://github.com/NVIDIA/cuda-quantum/actions/runs/6721623115?pr=848 to see a couple of things that our CI is pointing out ... namely the missing license preamble and Python formatting. The license preamble can be copied from other Python files, and that prior link provides a diff that can be used to patch your changes (simply formatting updates).

Also, take a look at https://github.com/NVIDIA/cuda-quantum/actions/runs/6722600981/job/18271591978?pr=848. It looks like the new script reported the following:

Traceback (most recent call last):
  File "/home/cudaq/notebook_validation.py", line 4, in <module>
    import nbformat
ModuleNotFoundError: No module named 'nbformat'

@abhiram6121
Copy link
Contributor Author

Also, take a look at https://github.com/NVIDIA/cuda-quantum/actions/runs/6722600981/job/18271591978?pr=848. It looks like the new script reported the following:

Traceback (most recent call last):
  File "/home/cudaq/notebook_validation.py", line 4, in <module>
    import nbformat
ModuleNotFoundError: No module named 'nbformat'

It seems nbconvert is not installed in the github runner. How do i install it?

@bettinaheim
Copy link
Collaborator

bettinaheim commented Nov 7, 2023

Also, take a look at https://github.com/NVIDIA/cuda-quantum/actions/runs/6722600981/job/18271591978?pr=848. It looks like the new script reported the following:

Traceback (most recent call last):
  File "/home/cudaq/notebook_validation.py", line 4, in <module>
    import nbformat
ModuleNotFoundError: No module named 'nbformat'

It seems nbconvert is not installed in the github runner. How do i install it?

@abhiram6121 In docker_images.yml, after the line you added, you need to add another line

docker exec -e TERM=xterm cuda-quantum python3 -m pip install nbconvert nbformat

This installs nbconvert in the container environment, where the validation script runs. I see in the error above that it actually complained about nbformat, so just I added that to the install line as well.

@bmhowe23
Copy link
Collaborator

bmhowe23 commented Nov 8, 2023

/ok to test

Command Bot: Processing...

@bettinaheim
Copy link
Collaborator

bettinaheim commented Nov 8, 2023

@abhiram6121 I used your script to validate the notebooks prior to release. Unfortunately, it looks like running the preprocessor doesn't actually execute the notebook code. I know the docs page of nbconvert suggest to do exactly what you did, and states that it will be executed, but that actually seems to be misleading. The docs were not particularly helpful in finding out what function the command line invokes, so I would have had to look through the code to see how the API has to be called. I instead opted to keep things simpler and went with the following script:

import glob, os, sys
import subprocess
from shutil import which
if which('jupyter') is None:
    print("Please install jupyter, e.g. with `pip install notebook`.")
    exit(1)


"""
This script runs all the notebooks in the current directory, and catches and reports any raised exceptions.
"""

def execute(notebook_filename):
    notebook_filename_out = notebook_filename.replace(
        '.ipynb', '.nbconvert.ipynb')

    success = True
    try:
        subprocess.run(["jupyter", "nbconvert", "--to", "notebook", "--execute", notebook_filename], check = True)
        os.remove(notebook_filename_out)
    except subprocess.CalledProcessError:
        print('Error executing the notebook "%s".\n\n' % notebook_filename)
        success = False
    return success


if __name__ == '__main__':
    if len(sys.argv) > 1:
        notebook_filenames = sys.argv[1:]
    else:
        # Use glob to find files with the specified extension in the current directory
        notebook_filenames = [
            fn for fn in glob.glob(f"*.ipynb") if not fn.endswith('.nbconvert.ipynb')]

    if notebook_filenames:
        notebooks_failed = []
        for notebook_filename in notebook_filenames:
            if not execute(notebook_filename):
                notebooks_failed.append(notebook_filename)

        if len(notebooks_failed) > 0:
            print("Failed! The following notebook(s) raised errors:\n" +
                  " ".join(notebooks_failed))
        else:
            print("Success! All the notebook(s) executed successfully.")
    else:
        print('Failed! No notebook found in the current directory.')

github-actions bot pushed a commit that referenced this pull request Nov 8, 2023
@NVIDIA NVIDIA deleted a comment from github-actions bot Nov 8, 2023
Co-authored-by: Bettina Heim <heimb@outlook.com>
@abhiram6121
Copy link
Contributor Author

@bettinaheim Thanks for the help, have updated accordingly. Please take a look and let me know.

@bettinaheim
Copy link
Collaborator

bettinaheim commented Nov 13, 2023

/ok to test

Command Bot: Processing...

Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Nov 13, 2023
@bettinaheim
Copy link
Collaborator

bettinaheim commented Nov 22, 2023

/ok to test

Command Bot: Processing...

Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Nov 22, 2023
github-actions bot pushed a commit that referenced this pull request Nov 23, 2023
github-actions bot pushed a commit that referenced this pull request Nov 24, 2023
@NVIDIA NVIDIA deleted a comment from github-actions bot Nov 24, 2023
@NVIDIA NVIDIA deleted a comment from github-actions bot Nov 24, 2023
@bettinaheim
Copy link
Collaborator

bettinaheim commented Nov 24, 2023

/ok to test

Command Bot: Processing...

Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions bot pushed a commit that referenced this pull request Nov 24, 2023
@bettinaheim
Copy link
Collaborator

bettinaheim commented Nov 24, 2023

/ok to test

Command Bot: Processing...

Copy link

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

@bettinaheim
Copy link
Collaborator

bettinaheim commented May 10, 2024

/ok to test

Command Bot: Processing...

Copy link
Collaborator

@bettinaheim bettinaheim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At long last, I've updated out images to contain the necessary tools, and everything looks good. abhiram6121 Thank you very much for this contribution!

@bettinaheim bettinaheim merged commit 617a752 into NVIDIA:main May 10, 2024
129 checks passed
@bettinaheim bettinaheim added the maintenance Work items to update and improve the code base label May 10, 2024
@github-actions github-actions bot locked and limited conversation to collaborators May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
maintenance Work items to update and improve the code base
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants