コード例 #1
0
def class_files(coursedir):
    # copy files from the user guide
    source_path = os.path.join(os.path.dirname(__file__), "..", "..", "docs",
                               "source", "user_guide", "source")
    shutil.copytree(os.path.join(os.path.dirname(__file__), source_path),
                    os.path.join(coursedir, "source"))

    # rename to old names -- we do this rather than changing all the tests
    # because I want the tests to operate on files with spaces in the names
    os.rename(os.path.join(coursedir, "source", "ps1"),
              os.path.join(coursedir, "source", "Problem Set 1"))
    os.rename(
        os.path.join(coursedir, "source", "Problem Set 1", "problem1.ipynb"),
        os.path.join(coursedir, "source", "Problem Set 1", "Problem 1.ipynb"))
    os.rename(
        os.path.join(coursedir, "source", "Problem Set 1", "problem2.ipynb"),
        os.path.join(coursedir, "source", "Problem Set 1", "Problem 2.ipynb"))

    # create a fake ps1
    os.mkdir(os.path.join(coursedir, "source", "ps.01"))
    with open(os.path.join(coursedir, "source", "ps.01", "problem 1.ipynb"),
              "w") as fh:
        write_nb(new_notebook(), fh, 4)

    return coursedir
コード例 #2
0
def class_files(coursedir):
    # copy files from the user guide
    source_path = os.path.join(os.path.dirname(__file__), "..", "..", "docs",
                               "source", "user_guide", "source")
    shutil.copytree(os.path.join(os.path.dirname(__file__), source_path),
                    os.path.join(coursedir, "source"))

    # rename to old names -- we do this rather than changing all the tests
    # because I want the tests to operate on files with spaces in the names
    os.rename(os.path.join(coursedir, "source", "ps1"),
              os.path.join(coursedir, "source", "Problem Set 1"))
    os.rename(
        os.path.join(coursedir, "source", "Problem Set 1", "problem1.ipynb"),
        os.path.join(coursedir, "source", "Problem Set 1", "Problem 1.ipynb"))
    os.rename(
        os.path.join(coursedir, "source", "Problem Set 1", "problem2.ipynb"),
        os.path.join(coursedir, "source", "Problem Set 1", "Problem 2.ipynb"))

    # create a fake ps1
    os.mkdir(os.path.join(coursedir, "source", "ps.01"))
    with io.open(os.path.join(coursedir, "source", "ps.01", "problem 1.ipynb"),
                 mode="w",
                 encoding="utf-8") as fh:
        write_nb(new_notebook(), fh, 4)

    with open("nbgrader_config.py", "a") as fh:
        fh.write(
            dedent("""
            c.CourseDirectory.root = '{}'
            """.format(coursedir)))

    return coursedir
コード例 #3
0
def clear_file(fname, target_version):
    with io.open(fname, 'r', encoding='utf-8') as f:
        nb = read_nb(f)
    remove_outputs(nb)
    with io.open(fname, 'w', encoding='utf-8') as f:
        write_nb(nb, f, version=target_version)
    print("wrote %s" % fname)
コード例 #4
0
ファイル: nb_tools.py プロジェクト: jbn/dissertate
def transform(nb_path, *fs):
    with open(nb_path, "r") as fp:
        nb = read_nb(fp, as_version=4)

    for f in fs:
        f(nb)

    with open(nb_path, "w") as fp:
        write_nb(nb, nb_path)
コード例 #5
0
ファイル: base.py プロジェクト: vanceeasleaf/nbgrader
 def _empty_notebook(self, path):
     nb = new_notebook()
     full_dest = os.path.join(os.getcwd(), path)
     if not os.path.exists(os.path.dirname(full_dest)):
         os.makedirs(os.path.dirname(full_dest))
     if os.path.exists(full_dest):
         os.remove(full_dest)
     with open(full_dest, 'w') as f:
         write_nb(nb, f, 4)
コード例 #6
0
ファイル: nbcreator.py プロジェクト: javimdr/PyHotAD
 def save_notebook(self, path, add_ext=False):
         if add_ext:
             path += self.EXT
         try:
             write_nb(self._notebook, path)
             return True
         except IOError:
             print('Error creating the ipynb document')
             return False
コード例 #7
0
ファイル: base.py プロジェクト: parente/nbgrader
 def _empty_notebook(self, path):
     nb = new_notebook()
     full_dest = os.path.abspath(path)
     if not os.path.exists(os.path.dirname(full_dest)):
         os.makedirs(os.path.dirname(full_dest))
     if os.path.exists(full_dest):
         os.remove(full_dest)
     with open(full_dest, "w") as f:
         write_nb(nb, f, 4)
コード例 #8
0
ファイル: base.py プロジェクト: parente/nbgrader
 def _empty_notebook(self, path):
     nb = new_notebook()
     full_dest = os.path.abspath(path)
     if not os.path.exists(os.path.dirname(full_dest)):
         os.makedirs(os.path.dirname(full_dest))
     if os.path.exists(full_dest):
         os.remove(full_dest)
     with open(full_dest, 'w') as f:
         write_nb(nb, f, 4)
コード例 #9
0
    def _empty_notebook(self, path, kernel=None):
        nb = new_notebook()
        if kernel is not None:
            nb.metadata.kernelspec = {
                "display_name": "kernel",
                "language": kernel,
                "name": kernel
            }

        full_dest = os.path.abspath(path)
        if not os.path.exists(os.path.dirname(full_dest)):
            os.makedirs(os.path.dirname(full_dest))
        if os.path.exists(full_dest):
            remove(full_dest)
        with io.open(full_dest, mode='w', encoding='utf-8') as f:
            write_nb(nb, f, 4)
コード例 #10
0
ファイル: conftest.py プロジェクト: 0905huhy/nbgrader
def class_files(coursedir):
    # copy files from the user guide
    source_path = os.path.join(os.path.dirname(__file__), "..", "..", "docs", "source", "user_guide", "source")
    shutil.copytree(os.path.join(os.path.dirname(__file__), source_path), os.path.join(coursedir, "source"))

    # rename to old names -- we do this rather than changing all the tests
    # because I want the tests to operate on files with spaces in the names
    os.rename(os.path.join(coursedir, "source", "ps1"), os.path.join(coursedir, "source", "Problem Set 1"))
    os.rename(os.path.join(coursedir, "source", "Problem Set 1", "problem1.ipynb"), os.path.join(coursedir, "source", "Problem Set 1", "Problem 1.ipynb"))
    os.rename(os.path.join(coursedir, "source", "Problem Set 1", "problem2.ipynb"), os.path.join(coursedir, "source", "Problem Set 1", "Problem 2.ipynb"))

    # create a fake ps1
    os.mkdir(os.path.join(coursedir, "source", "ps.01"))
    with open(os.path.join(coursedir, "source", "ps.01", "problem 1.ipynb"), "w") as fh:
        write_nb(new_notebook(), fh, 4)

    return coursedir
コード例 #11
0
ファイル: conftest.py プロジェクト: vanceeasleaf/nbgrader
def class_files(request, tempdir):
    # copy files from the user guide
    source_path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "docs", "source", "user_guide", "source")
    shutil.copytree(os.path.join(os.path.dirname(__file__), source_path), "source")

    # create a fake ps1
    os.mkdir(os.path.join("source", "ps1"))
    with open(os.path.join("source", "ps1", "problem 1.ipynb"), "w") as fh:
        write_nb(new_notebook(), fh, 4)

    # create the gradebook
    gb = Gradebook("sqlite:///gradebook.db")
    gb.add_assignment("Problem Set 1")
    gb.add_assignment("ps1")
    gb.add_student("Bitdiddle", first_name="Ben", last_name="B")
    gb.add_student("Hacker", first_name="Alyssa", last_name="H")
    gb.add_student("Reasoner", first_name="Louis", last_name="R")

    return tempdir
コード例 #12
0
ファイル: conftest.py プロジェクト: minrk/nbgrader
def class_files(request, tempdir):
    # copy files from the user guide
    source_path = os.path.join(os.path.dirname(__file__), "..", "..", "..", "docs", "source", "user_guide", "source")
    shutil.copytree(os.path.join(os.path.dirname(__file__), source_path), "source")

    # create a fake ps1
    os.mkdir(os.path.join("source", "ps.01"))
    with open(os.path.join("source", "ps.01", "problem 1.ipynb"), "w") as fh:
        write_nb(new_notebook(), fh, 4)

    # create the gradebook
    gb = Gradebook("sqlite:///gradebook.db")
    gb.add_assignment("Problem Set 1")
    gb.add_assignment("ps.01")
    gb.add_student("Bitdiddle", first_name="Ben", last_name="B")
    gb.add_student("Hacker", first_name="Alyssa", last_name="H")
    gb.add_student("Reasoner", first_name="Louis", last_name="R")

    return tempdir
コード例 #13
0
def class_files(coursedir):
    # copy files from the user guide
    source_path = os.path.join(os.path.dirname(__file__), "..", "..", "docs", "source", "user_guide", "source")
    shutil.copytree(os.path.join(os.path.dirname(__file__), source_path), os.path.join(coursedir, "source"))

    # rename to old names -- we do this rather than changing all the tests
    # because I want the tests to operate on files with spaces in the names
    os.rename(os.path.join(coursedir, "source", "ps1"), os.path.join(coursedir, "source", "Problem Set 1"))
    os.rename(os.path.join(coursedir, "source", "Problem Set 1", "problem1.ipynb"), os.path.join(coursedir, "source", "Problem Set 1", "Problem 1.ipynb"))
    os.rename(os.path.join(coursedir, "source", "Problem Set 1", "problem2.ipynb"), os.path.join(coursedir, "source", "Problem Set 1", "Problem 2.ipynb"))

    # create a fake ps1
    os.mkdir(os.path.join(coursedir, "source", "ps.01"))
    with io.open(os.path.join(coursedir, "source", "ps.01", "problem 1.ipynb"), mode="w", encoding="utf-8") as fh:
        write_nb(new_notebook(), fh, 4)

    with open("nbgrader_config.py", "a") as fh:
        fh.write(dedent(
            """
            c.CourseDirectory.root = '{}'
            """.format(coursedir)
        ))

    return coursedir
コード例 #14
0
def clear_directory(dname, target_version):
    assert os.path.isdir(dname)
    fnames = os.listdir(dname)
    fpaths = [os.path.join(dname, fname) for fname in fnames
              if not fname.startswith('.')]
    clear_paths([fpath for fpath in fpaths
                 if os.path.isdir(fpath) or fpath.endswith('.ipynb')],
                target_version=target_version)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description="Clear all outputs in Jupyter notebooks.")
    parser.add_argument(
        '--target-version', type=int, default=4,
        help="Version of notebook format to save.")
    parser.add_argument(
        'fnames', nargs='*',
        help="Files to process. Will recursively descend into directories. "
        "If not provided, notebook will be read from stdin and written to "
        "stdout.")
    args = parser.parse_args()

    if len(args.fnames) > 0:
        clear_paths(args.fnames, target_version=args.target_version)
    else:
        nb = read_nb(sys.stdin)
        remove_outputs(nb)
        write_nb(nb, sys.stdout, version=args.target_version)