Python nbformat.current_nbformat() Examples

The following are 30 code examples of nbformat.current_nbformat(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module nbformat , or try the search function .
Example #1
Source File: test_notebooks.py    From nara_wpe with MIT License 6 votes vote down vote up
def _notebook_run(path):
    """Execute a notebook via nbconvert and collect output.
       :returns (parsed nb object, execution errors)
    """
    dirname = os.path.dirname(str(path))
    os.chdir(dirname)
    with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
        args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
                "--ExecutePreprocessor.timeout=360",
                "--output", fout.name, str(path)]
        subprocess.check_call(args)

        fout.seek(0)
        nb = nbformat.read(fout, nbformat.current_nbformat)

    errors = [
        output for cell in nb.cells if "outputs" in cell
        for output in cell["outputs"]
        if output.output_type == "error"
    ]

    return nb, errors 
Example #2
Source File: test_tutorials.py    From pyam with Apache License 2.0 6 votes vote down vote up
def _notebook_run(path, kernel=None, timeout=60, capsys=None):
    """Execute a notebook via nbconvert and collect output.
    :returns (parsed nb object, execution errors)
    """
    major_version = sys.version_info[0]
    dirname, __ = os.path.split(path)
    os.chdir(dirname)
    fname = os.path.join(here, 'test.ipynb')
    args = [
        "jupyter", "nbconvert", "--to", "notebook", "--execute",
        "--ExecutePreprocessor.timeout={}".format(timeout),
        "--output", fname, path]
    subprocess.check_call(args)

    nb = nbformat.read(io.open(fname, encoding='utf-8'),
                       nbformat.current_nbformat)

    errors = [
        output for cell in nb.cells if "outputs" in cell
        for output in cell["outputs"] if output.output_type == "error"
    ]

    os.remove(fname)

    return nb, errors 
Example #3
Source File: test_jupyter_execution.py    From storlets with Apache License 2.0 6 votes vote down vote up
def _run_notebook(self, path):
        """Execute a notebook via nbconvert and collect output.
        :returns (parsed nb object, execution errors)
        """
        with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
            args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
                    "--ExecutePreprocessor.timeout=60",
                    "--output", fout.name, path]
            try:
                subprocess.check_output(args, stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                # Note that CalledProcessError will have stdout/stderr in py3
                # instead of output attribute
                self.fail('jupyter nbconvert fails with:\n'
                          'STDOUT: %s\n' % (e.output))

            fout.seek(0)
            nb = nbformat.read(fout, nbformat.current_nbformat)

            # gather all error messages in all cells in the notebook
            errors = [output for cell in nb.cells if "outputs" in cell
                      for output in cell["outputs"]
                      if output.output_type == "error"]

            return nb, errors 
Example #4
Source File: test_v3.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_too_old():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test-v0.ipynb")
    with pytest.raises(SchemaMismatchError):
        read_v3(path, current_nbformat) 
Example #5
Source File: test_v1.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_reads():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test-v1.ipynb")
    contents = open(path, "r").read()
    reads_v1(contents, current_nbformat) 
Example #6
Source File: test_v1.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_writes():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test-v1.ipynb")
    nb = read_v1(path, current_nbformat)
    writes_v1(nb) 
Example #7
Source File: test_v1.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_too_old():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test-v0.ipynb")
    with pytest.raises(SchemaMismatchError):
        read_v1(path, current_nbformat) 
Example #8
Source File: test_v1.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_too_new():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test.ipynb")
    with pytest.raises(SchemaMismatchError):
        read_v1(path, current_nbformat) 
Example #9
Source File: test_v1.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_upgrade_notebook_metadata():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test-v0.ipynb")
    with open(path, "r") as fh:
        nb = read(fh, current_nbformat)
    nb = MetadataValidatorV1().upgrade_notebook_metadata(nb) 
Example #10
Source File: test_v3.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_read():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test.ipynb")
    read_v3(path, current_nbformat) 
Example #11
Source File: test_v3.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_write():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test.ipynb")
    nb = read_v3(path, current_nbformat)
    with tempfile.TemporaryFile(mode="w") as fh:
        write_v3(nb, fh) 
Example #12
Source File: test_v3.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_writes():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test.ipynb")
    nb = read_v3(path, current_nbformat)
    writes_v3(nb) 
Example #13
Source File: test_v2.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_too_new():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test.ipynb")
    with pytest.raises(SchemaMismatchError):
        read_v2(path, current_nbformat) 
Example #14
Source File: test_v3.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_too_new():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test.ipynb")
    nb = read_v3(path, current_nbformat)
    for cell in nb.cells:
        if hasattr(cell.metadata, "nbgrader"):
            cell.metadata.nbgrader.schema_version += 1
    nb = json.dumps(nb)
    with pytest.raises(SchemaMismatchError):
        reads_v3(nb, current_nbformat) 
Example #15
Source File: test_v3.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_upgrade_notebook_metadata():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test-v0.ipynb")
    with open(path, "r") as fh:
        nb = read(fh, current_nbformat)
    nb = MetadataValidatorV3().upgrade_notebook_metadata(nb) 
Example #16
Source File: test_nbgrader_autograde.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_no_execute(self, course_dir):
        run_nbgrader(["db", "assignment", "add", "ps1", "--duedate",
                      "2015-02-02 14:58:23.948203 America/Los_Angeles"])
        run_nbgrader(["db", "student", "add", "foo"])
        run_nbgrader(["db", "student", "add", "bar"])

        self._copy_file(join("files", "test.ipynb"), join(course_dir, "source", "ps1", "p1.ipynb"))
        run_nbgrader(["generate_assignment", "ps1"])

        self._copy_file(join("files", "test-with-output.ipynb"), join(course_dir, "submitted", "foo", "ps1", "p1.ipynb"))
        with io.open(join(os.path.dirname(__file__), "files", "test-with-output.ipynb"), mode="r", encoding='utf-8') as fh:
            orig_contents = reads(fh.read(), as_version=current_nbformat)

        run_nbgrader(["autograde", "ps1"])
        with io.open(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"), mode="r", encoding="utf-8") as fh:
            new_contents = reads(fh.read(), as_version=current_nbformat)

        different = False
        for i in range(len(orig_contents.cells)):
            orig_cell = orig_contents.cells[i]
            new_cell = new_contents.cells[i]
            if 'outputs' in orig_cell:
                if orig_cell.outputs != new_cell.outputs:
                    different = True
                    break
            elif 'outputs' in new_cell:
                different = True

        assert different

        run_nbgrader(["autograde", "ps1", "--force", "--no-execute"])
        with io.open(join(course_dir, "autograded", "foo", "ps1", "p1.ipynb"), mode="r", encoding="utf-8") as fh:
            new_contents = reads(fh.read(), as_version=current_nbformat)

        for i in range(len(orig_contents.cells)):
            orig_cell = orig_contents.cells[i]
            new_cell = new_contents.cells[i]
            if 'outputs' in orig_cell:
                assert orig_cell.outputs == new_cell.outputs
            else:
                assert 'outputs' not in new_cell 
Example #17
Source File: test_create_assignment.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _save_and_validate(browser):
    _wait(browser).until(_save(browser))
    read("blank.ipynb", current_nbformat) 
Example #18
Source File: updateapp.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def start(self):
        super(UpdateApp, self).start()

        if len(self.extra_args) < 1:
            self.fail(
                "No notebooks or directories given. Usage:\n\n"
                "nbgrader update <NOTEBOOK>\n"
                "nbgrader update <DIRECTORY>\n")

        notebooks = set()
        for name in self.extra_args:
            if not os.path.exists(name):
                self.fail("No such file or directory: {}".format(name))
            elif os.path.isdir(name):
                notebooks.update([os.path.join(name, x) for x in find_all_notebooks(name)])
            elif not name.endswith(".ipynb"):
                self.log.warning("{} is not a notebook, ignoring".format(name))
            else:
                notebooks.add(name)

        notebooks = sorted(list(notebooks))
        for notebook in notebooks:
            self.log.info("Updating metadata for notebook: {}".format(notebook))
            nb = orig_read(notebook, current_nbformat)
            nb = MetadataValidator().upgrade_notebook_metadata(nb)
            if self.validate:
                try:
                    write(nb, notebook)
                except ValidationError:
                    self.log.error(traceback.format_exc())
                    self.fail("Notebook '{}' failed to validate, metadata is corrupted".format(notebook))
                except SchemaTooNewError:
                    self.log.error(traceback.format_exc())
                    self.fail((
                        "The notebook '{}' uses a newer version "
                        "of the nbgrader metadata format. Please update your version of "
                        "nbgrader to the latest version to be able to use this notebook."
                    ).format(notebook))
            else:
                orig_write(nb, notebook) 
Example #19
Source File: test_v1.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_read():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test-v1.ipynb")
    read_v1(path, current_nbformat) 
Example #20
Source File: test_v2.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_upgrade_notebook_metadata():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test-v0.ipynb")
    with open(path, "r") as fh:
        nb = read(fh, current_nbformat)
    nb = MetadataValidatorV2().upgrade_notebook_metadata(nb) 
Example #21
Source File: tests.py    From deepchem with MIT License 5 votes vote down vote up
def _notebook_read(path):
  """
  Parameters
  ----------
  path: str
  path to ipython notebook

  Returns
  -------
  nb: notebook object
  errors: list of Exceptions
  """

  with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
    args = [
        "jupyter-nbconvert", "--to", "notebook", "--execute",
        "--ExecutePreprocessor.timeout=600", "--output", fout.name, path
    ]
    subprocess.check_call(args)

    fout.seek(0)
    nb = nbformat.read(fout, nbformat.current_nbformat)

  errors = [output for cell in nb.cells if "outputs" in cell
            for output in cell["outputs"] \
            if output.output_type == "error"]

  return nb, errors 
Example #22
Source File: test_v2.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_too_old():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test-v0.ipynb")
    with pytest.raises(SchemaMismatchError):
        read_v2(path, current_nbformat) 
Example #23
Source File: test_v2.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_write():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test-v2.ipynb")
    nb = read_v2(path, current_nbformat)
    with tempfile.TemporaryFile(mode="w") as fh:
        write_v2(nb, fh) 
Example #24
Source File: test_v2.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_reads():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test-v2.ipynb")
    contents = open(path, "r").read()
    reads_v2(contents, current_nbformat) 
Example #25
Source File: test_v2.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_read():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test-v2.ipynb")
    read_v2(path, current_nbformat) 
Example #26
Source File: headerfooter.py    From nbgrader with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def preprocess(self, nb: NotebookNode, resources: ResourcesDict) -> Tuple[NotebookNode, ResourcesDict]:
        """Concatenates the cells from the header and footer notebooks to the
        given cells.

        """
        new_cells = []

        # header
        if self.header:
            with io.open(self.header, encoding='utf-8') as fh:
                header_nb = read_nb(fh, as_version=current_nbformat)
            new_cells.extend(header_nb.cells)

        # body
        new_cells.extend(nb.cells)

        # footer
        if self.footer:
            with io.open(self.footer, encoding='utf-8') as fh:
                footer_nb = read_nb(fh, as_version=current_nbformat)
            new_cells.extend(footer_nb.cells)

        nb.cells = new_cells
        super(IncludeHeaderFooter, self).preprocess(nb, resources)

        return nb, resources 
Example #27
Source File: test_jupyter_execution.py    From storlets with Apache License 2.0 5 votes vote down vote up
def test_notebook(self):
        test_path = os.path.abspath(__file__)
        test_dir = os.path.dirname(test_path)
        original_notebook = os.path.join(test_dir, 'test_notebook.ipynb')

        with open(original_notebook) as f:
            original_nb = nbformat.read(f, nbformat.current_nbformat)
        expected_output = self._flatten_output_text(original_nb)
        got_nb, errors = self._run_notebook(original_notebook)
        self.assertFalse(errors)
        got = self._flatten_output_text(got_nb)
        self._assert_output(expected_output, got) 
Example #28
Source File: test_notebooks.py    From landlab with MIT License 5 votes vote down vote up
def _notebook_run(path):
    """Execute a notebook via nbconvert and collect output.
       :returns (parsed nb object, execution errors)
    """
    import nbformat

    _, notebook = os.path.split(path)
    base, ext = os.path.splitext(notebook)

    with tempfile.NamedTemporaryFile("w", suffix=".ipynb") as fp:
        args = [
            "jupyter",
            "nbconvert",
            "--to",
            "notebook",
            "--execute",
            "--ExecutePreprocessor.kernel_name=python",
            "--ExecutePreprocessor.timeout=None",
            "--output",
            fp.name,
            "--output-dir=.",
            path,
        ]
        subprocess.check_call(args)

        nb = nbformat.read(fp.name, nbformat.current_nbformat, encoding="UTF-8")

    errors = [
        output
        for cell in nb.cells
        if "outputs" in cell
        for output in cell["outputs"]
        if output.output_type == "error"
    ]

    return nb, errors 
Example #29
Source File: notebook_runner.py    From AIF360 with Apache License 2.0 5 votes vote down vote up
def notebook_run(path):
    """Execute a notebook via nbconvert and collect output.
       :returns (parsed nb object, execution errors)
    """
    dirname, __ = os.path.split(path)
    os.chdir(dirname)

    kername = "python3"

    with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
        args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
                "--ExecutePreprocessor.timeout=600",
                "--ExecutePreprocessor.allow_errors=True",
                "--ExecutePreprocessor.kernel_name={}".format(kername),
                "--output", fout.name, path]

        subprocess.check_call(args)

        fout.seek(0)
        nb = nbformat.read(fout, nbformat.current_nbformat)

    errors = [output for cell in nb.cells if "outputs" in cell
                     for output in cell["outputs"]
                     if output.output_type == "error"]

    return nb, errors 
Example #30
Source File: test_notebooks.py    From xfer with Apache License 2.0 5 votes vote down vote up
def _notebook_run(notebook):
    """
    Execute a notebook via nbconvert and collect output.
       :return: (parsed nb object, execution errors)
    """

    if os.path.isfile(temp_notebook):
        os.remove(temp_notebook)

    with open(temp_notebook, 'w') as fout:
        # with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
        args = ["jupyter", "nbconvert", "--to", "notebook", "--execute", "--allow-errors",
                "--ExecutePreprocessor.timeout=-1",
                "--output", fout.name, notebook]
        try:
            subprocess.check_call(args)
        except subprocess.CalledProcessError as e:
            if e.returncode == 1:
                # print the message and ignore error with code 1 as this indicates there were errors in the notebook
                print(e.output)
                pass
            else:
                # all other codes indicate some other problem, rethrow
                raise

    with open(temp_notebook, 'r') as fout:
        nb = nbformat.read(fout, nbformat.current_nbformat)

    errors = [output for cell in nb.cells if "outputs" in cell for output in cell["outputs"]
              if output.output_type == "error"]

    os.remove(temp_notebook)
    return nb, errors