当前位置: 首页>>代码示例>>Python>>正文


Python nbformat.current_nbformat方法代码示例

本文整理汇总了Python中nbformat.current_nbformat方法的典型用法代码示例。如果您正苦于以下问题:Python nbformat.current_nbformat方法的具体用法?Python nbformat.current_nbformat怎么用?Python nbformat.current_nbformat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在nbformat的用法示例。


在下文中一共展示了nbformat.current_nbformat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _notebook_run

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
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 
开发者ID:fgnt,项目名称:nara_wpe,代码行数:24,代码来源:test_notebooks.py

示例2: _notebook_run

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
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 
开发者ID:IAMconsortium,项目名称:pyam,代码行数:27,代码来源:test_tutorials.py

示例3: _run_notebook

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
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 
开发者ID:openstack,项目名称:storlets,代码行数:27,代码来源:test_jupyter_execution.py

示例4: _notebook_read

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
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 
开发者ID:deepchem,项目名称:deepchem,代码行数:30,代码来源:tests.py

示例5: test_demo

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def test_demo(self):
        """Execute a notebook via nbconvert and collect output."""

        # Determine if python 2 or 3 is being used.
        major_version, minor_version = sys.version_info[:2]
        if major_version == 2 or minor_version == 6:
            version = str(major_version)

            # Run ipython notebook.
            with tempfile.NamedTemporaryFile(suffix='.ipynb') as output_file:
                args = ['jupyter',
                        'nbconvert',
                        '--to',
                        'notebook',
                        '--execute',
                        '--ExecutePreprocessor.timeout=600',
                        '--ExecutePreprocessor.kernel_name=python{}'.format(
                            version),
                        '--output',
                        output_file.name,
                        self.path]
                subprocess.check_call(args)
                output_file.seek(0)
                nb = nbformat.read(output_file, nbformat.current_nbformat)

            # Parse output and make sure there are no errors.
            errors = [output for cell in nb.cells if "outputs" in cell for
                      output in cell["outputs"] if
                      output.output_type == "error"]
        else:
            errors = []
        self.assertEqual(errors, []) 
开发者ID:quantumlib,项目名称:OpenFermion-ProjectQ,代码行数:34,代码来源:_demo_test.py

示例6: _exec_notebook

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def _exec_notebook(notebook_filename, nbpath):
    pythonkernel = 'python' + str(sys.version_info[0])
    ep = ExecutePreprocessor(timeout=600, kernel_name=pythonkernel, interrupt_on_timeout=True)
    with open(notebook_filename) as f:
        nb = nbformat.read(f, as_version=nbformat.current_nbformat)
        try:
            ep.preprocess(nb, {'metadata': {'path': nbpath}})
        except CellExecutionError:
            print('-' * 60)
            traceback.print_exc(file=sys.stdout)
            print('-' * 60)
            assert False, 'Error executing the notebook %s. See above for error.' % notebook_filename 
开发者ID:GPflow,项目名称:GPflowOpt,代码行数:14,代码来源:test_notebooks.py

示例7: _notebook_run

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
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 
开发者ID:amzn,项目名称:xfer,代码行数:35,代码来源:test_notebooks.py

示例8: notebook_run

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
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 
开发者ID:IBM,项目名称:AIF360,代码行数:28,代码来源:notebook_runner.py

示例9: _notebook_run

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
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 
开发者ID:landlab,项目名称:landlab,代码行数:38,代码来源:test_notebooks.py

示例10: test_notebook

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
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) 
开发者ID:openstack,项目名称:storlets,代码行数:14,代码来源:test_jupyter_execution.py

示例11: preprocess

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
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 
开发者ID:jupyter,项目名称:nbgrader,代码行数:28,代码来源:headerfooter.py

示例12: test_read

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def test_read():
    currdir = os.path.split(__file__)[0]
    path = os.path.join(currdir, "..", "apps", "files", "test-v2.ipynb")
    read_v2(path, current_nbformat) 
开发者ID:jupyter,项目名称:nbgrader,代码行数:6,代码来源:test_v2.py

示例13: test_reads

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
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) 
开发者ID:jupyter,项目名称:nbgrader,代码行数:7,代码来源:test_v2.py

示例14: test_write

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
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) 
开发者ID:jupyter,项目名称:nbgrader,代码行数:8,代码来源:test_v2.py

示例15: test_too_old

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
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) 
开发者ID:jupyter,项目名称:nbgrader,代码行数:7,代码来源:test_v2.py


注:本文中的nbformat.current_nbformat方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。