Esempio n. 1
0
def run_notebooks(nb_paths, allow_errors=False, **kwargs):
    print(f'>> Running {len(nb_paths)} notebooks...')
    nb_paths.sort()
    for nb_path in nb_paths:
        try:
            jupyter_utils.nbconvert(nb_path,
                                    execute=True,
                                    inplace=True,
                                    allow_errors=allow_errors)
        except subprocess.CalledProcessError:
            error_msg = \
                f"Got errors while executing notebook {nb_path}. " \
                f"Make sure you've implemented everything and that all " \
                f"tests pass."
            print(error_msg, file=sys.stderr)
            sys.exit(1)
Esempio n. 2
0
def prepare_submission(hw_dir, out_dir, submitter_ids, skip_run, **kwargs):
    """
    Creates a submission zip file students can submit.
    This function will run all the notebooks in the assignment, merge them into
    a single html file for submission and wrap everything in a zip file.
    :param hw_dir: Root directory of the assignment.
    :param out_dir: Where to write the zip file to.
    :param submitter_ids: list of ID numbers of submitters.
    :param skip_run: Whether to skip running the notebooks.
    """
    submission_name = create_submission_name(hw_dir, submitter_ids)

    nb_paths = glob.glob(os.path.join(hw_dir, '*.ipynb'))
    nb_paths.sort()

    # 1. Run all notebooks and save outputs within them
    if not skip_run:
        run_notebooks(nb_paths, **kwargs)

    # 2. Run nbnmerge to merge them
    nb_merged = os.path.join(hw_dir, f'{submission_name}.ipynb')
    jupyter_utils.nbmerge(nb_paths, nb_merged)

    # 3. Run nbconvert to convert the merged notebook to html
    jupyter_utils.nbconvert(nb_merged)
    os.remove(nb_merged)

    # 4. Zip the hw folder, excluding 'data' folder and temp files.
    dest_dir = os.path.join(out_dir, submission_name)
    if os.path.exists(dest_dir):
        shutil.rmtree(dest_dir)

    try:

        def ignore_fn(src, names):
            return copytree_ignore_fn(src, names, is_distribution=False)

        # Copy assignment directory to a temporary folder
        shutil.copytree(hw_dir, dest_dir, symlinks=False, ignore=ignore_fn)

        # Create an archive for submission
        sub_zip = zipdir(dest_dir)
        print(f'>> Created submission {sub_zip}. Good luck!')

    finally:
        shutil.rmtree(dest_dir)
Esempio n. 3
0
def run_nb(name):
    nb_path = os.path.join(NB_DIR, name)
    jupyter_utils.nbconvert(nb_path, execute=True, debug=True, stdout=True)
Esempio n. 4
0
def clear_notebooks(nb_paths, **kwargs):
    print(f'>> Clearing {len(nb_paths)} notebooks...')
    nb_paths.sort()
    for nb_path in nb_paths:
        jupyter_utils.nbconvert(nb_path, clear_output=True)