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


Python nbformat.read方法代码示例

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


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

示例1: test_timeseries_controls

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def test_timeseries_controls(self):
        nb_path = Path(_NB_FOLDER).joinpath(_NB_NAME)
        abs_path = Path(_NB_FOLDER).absolute()
        with open(nb_path) as f:
            nb = nbformat.read(f, as_version=4)
        ep = ExecutePreprocessor(timeout=600, kernel_name="python3")

        try:
            ep.preprocess(nb, {"metadata": {"path": abs_path}})
        except CellExecutionError:
            nb_err = str(nb_path).replace(".ipynb", "-err.ipynb")
            msg = f"Error executing the notebook '{nb_path}'.\n"
            msg += f"See notebook '{nb_err}' for the traceback."
            print(msg)
            with open(nb_err, mode="w", encoding="utf-8") as f:
                nbformat.write(nb, f)
            raise 
开发者ID:microsoft,项目名称:msticpy,代码行数:19,代码来源:test_timeseries.py

示例2: get_notebook_checkpoint

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def get_notebook_checkpoint(self, checkpoint_id, path):
        """Get the content of a checkpoint for a notebook.

        Returns a dict of the form:
        {
            "type": "notebook",
            "content": <output of nbformat.read>,
        }
        """
        self.log.info("restoring %s from checkpoint %s", path, checkpoint_id)
        cp = self._get_checkpoint_path(checkpoint_id, path)
        exists, blob = self.parent._fetch(cp)
        if not exists:
            raise web.HTTPError(404, u"No such checkpoint: %s for %s" % (
                checkpoint_id, path))
        nb = self.parent._read_notebook(blob)
        return {
            "type": "notebook",
            "content": nb
        } 
开发者ID:src-d,项目名称:jgscm,代码行数:22,代码来源:__init__.py

示例3: test_folium_map_notebook

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def test_folium_map_notebook(self):
        nb_path = Path(_NB_FOLDER).joinpath(_NB_NAME)
        abs_path = Path(_NB_FOLDER).absolute()
        with open(nb_path) as f:
            nb = nbformat.read(f, as_version=4)
        ep = ExecutePreprocessor(timeout=600, kernel_name="python3")

        try:
            ep.preprocess(nb, {"metadata": {"path": abs_path}})
        except CellExecutionError:
            nb_err = str(nb_path).replace(".ipynb", "-err.ipynb")
            msg = f"Error executing the notebook '{nb_path}'.\n"
            msg += f"See notebook '{nb_err}' for the traceback."
            print(msg)
            with open(nb_err, mode="w", encoding="utf-8") as f:
                nbformat.write(nb, f)
            raise 
开发者ID:microsoft,项目名称:msticpy,代码行数:19,代码来源:test_folium.py

示例4: run_notebook

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def run_notebook(notebook_path):
    nb_name, _ = os.path.splitext(os.path.basename(notebook_path))

    with open(notebook_path) as f:
        nb = nbformat.read(f, as_version=4)

    proc = ExecutePreprocessor(timeout=600, kernel_name='python3')
    proc.allow_errors = True
    proc.preprocess(nb)

    for num, cell in enumerate(nb.cells):
        if 'outputs' in cell:
            for output in cell['outputs']:
                if output.output_type == 'error':
                    return cell.execution_count, output.traceback

    return None

# 7-bit C1 ANSI sequences 
开发者ID:pmelchior,项目名称:scarlet,代码行数:21,代码来源:test_docs.py

示例5: run_notebook

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def run_notebook(path, workdir=None, timeout=_DEFAULT_TIMEOUT):
    resources = {
        'metadata': {
            'path': workdir
        }
    } if workdir is not None else None

    with open(path, 'r') as f:
        nb = nbformat.read(f, as_version=_DEFAULT_IPYNB_VERSION)
    ep = ExecutePreprocessor(timeout=timeout)

    # Some notebooks may generate and automatically open reports in a web browser.
    # This is inconvenient in an automated test suite, so let's disable it.
    # Overwriting $BROWSER with a dummy command will keep the notebook
    # kernel from being able to open a web browser on Linux.
    # TODO find platform-neutral solution to suppress webbrowser.open
    browser = os.environ.get('BROWSER', None)
    os.environ['BROWSER'] = 'echo %s'
    ep.preprocess(nb, resources=resources)
    os.environ['BROWSER'] = browser 
开发者ID:pyGSTio,项目名称:pyGSTi,代码行数:22,代码来源:notebookstestcase.py

示例6: _read_notebook

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def _read_notebook(self, os_path, as_version=4):
        """Read a notebook from an os path."""
        with self.open(os_path, 'r', encoding='utf-8') as f:
            try:

                # NEW
                file_ext = _file_extension(os_path)
                if file_ext == '.ipynb':
                    return nbformat.read(f, as_version=as_version)
                else:
                    return convert(os_path, from_=self.format, to='notebook')

            except Exception as e:
                raise HTTPError(
                    400,
                    u"Unreadable Notebook: %s %r" % (os_path, e),
                ) 
开发者ID:rossant,项目名称:ipymd,代码行数:19,代码来源:contents_manager.py

示例7: test_clustering_nbdisplay_notebook

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def test_clustering_nbdisplay_notebook(self):
        nb_path = Path(_NB_FOLDER).joinpath(_NB_NAME)
        abs_path = Path(_NB_FOLDER).absolute()
        with open(nb_path) as f:
            nb = nbformat.read(f, as_version=4)
        ep = ExecutePreprocessor(timeout=600, kernel_name="python3")

        try:
            ep.preprocess(nb, {"metadata": {"path": abs_path}})
        except CellExecutionError:
            nb_err = str(nb_path).replace(".ipynb", "-err.ipynb")
            msg = f"Error executing the notebook '{nb_path}'.\n"
            msg += f"See notebook '{nb_err}' for the traceback."
            print(msg)
            with open(nb_err, mode="w", encoding="utf-8") as f:
                nbformat.write(nb, f)
            raise 
开发者ID:microsoft,项目名称:msticpy,代码行数:19,代码来源:test_nbdisplay.py

示例8: test_process_tree_notebook

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def test_process_tree_notebook():
    nb_path = Path(_NB_FOLDER).joinpath(_NB_NAME)
    abs_path = Path(_NB_FOLDER).absolute()
    with open(nb_path) as f:
        nb = nbformat.read(f, as_version=4)
    ep = ExecutePreprocessor(timeout=600, kernel_name="python3")

    try:
        ep.preprocess(nb, {"metadata": {"path": abs_path}})
    except CellExecutionError:
        nb_err = str(nb_path).replace(".ipynb", "-err.ipynb")
        msg = f"Error executing the notebook '{nb_path}'.\n"
        msg += f"See notebook '{nb_err}' for the traceback."
        print(msg)
        with open(nb_err, mode="w", encoding="utf-8") as f:
            nbformat.write(nb, f)
        raise 
开发者ID:microsoft,项目名称:msticpy,代码行数:19,代码来源:test_process_tree_utils.py

示例9: test_timeline_controls

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def test_timeline_controls(self):
        nb_path = Path(_NB_FOLDER).joinpath(_NB_NAME)
        abs_path = Path(_NB_FOLDER).absolute()
        with open(nb_path) as f:
            nb = nbformat.read(f, as_version=4)
        ep = ExecutePreprocessor(timeout=600, kernel_name="python3")

        try:
            ep.preprocess(nb, {"metadata": {"path": abs_path}})
        except CellExecutionError:
            nb_err = str(nb_path).replace(".ipynb", "-err.ipynb")
            msg = f"Error executing the notebook '{nb_path}'.\n"
            msg += f"See notebook '{nb_err}' for the traceback."
            print(msg)
            with open(nb_err, mode="w", encoding="utf-8") as f:
                nbformat.write(nb, f)
            raise 
开发者ID:microsoft,项目名称:msticpy,代码行数:19,代码来源:test_timeline.py

示例10: test_main_with_options

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def test_main_with_options(tmpdir):
    # Given
    code = dedent("""
    from tex2ipy.tex2cells import Tex2Cells
    class Converter(Tex2Cells):
        def _handle_frame(self, node):
            super(Converter, self)._handle_frame(node)
            self.current['source'].append('## Overloaded\\n')
    """)
    convert = tmpdir.join('extra.py')
    convert.write(code)
    src = tmpdir.join('test.tex')
    src.write(DOCUMENT)
    dest = tmpdir.join('test.ipynb')

    # When
    main(args=[str(src), str(dest), '-c', str(convert)])

    # Then
    assert dest.check(file=1)
    nb = nbformat.read(str(dest), 4)

    src = nb.cells[0].source.splitlines()
    assert src[0] == '## Overloaded'
    assert src[1] == '## Foo' 
开发者ID:prabhuramachandran,项目名称:tex2ipy,代码行数:27,代码来源:test_tex2ipy.py

示例11: test_can_run_examples_jupyter_notebooks

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def test_can_run_examples_jupyter_notebooks(self):
        print("Examples folder ", self.examples_folder)
        for filename in os.listdir(self.examples_folder):
            if not filename.endswith('.ipynb'):
                continue

            path = os.path.join(self.examples_folder, filename)
            notebook = nbformat.read(path, nbformat.NO_CONVERT)
            state = {}

            for cell in notebook.cells:
                if cell.cell_type == 'code' and not is_matplotlib_cell(cell):
                    try:
                        exec(strip_magics_and_shows(cell.source), state)
                    # coverage: ignore
                    except:
                        print('Failed to run {}.'.format(path))
                        raise 
开发者ID:quantumlib,项目名称:OpenFermion,代码行数:20,代码来源:_examples_test.py

示例12: run_notebook

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def run_notebook(filename):

    run_path = os.path.split(filename)[0]

    with open(filename) as f:
        nb = nbformat.read(f, as_version=4)

    try:
        ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
        ep.preprocess(nb, {'metadata': {'path': run_path}})

        # FIXME: use tempfile and mv to avoid interruption
        # better split the source code of the notebook and the compiled targets.
        with open(filename, 'wt') as f:
            nbformat.write(nb, f)

    except Exception as e:
        print('processing', filename, e) 
开发者ID:bccp,项目名称:nbodykit,代码行数:20,代码来源:run_notebooks.py

示例13: convert

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def convert(nbfile):
    basename, _ = os.path.splitext(nbfile)

    meta = {'metadata': {'path': '.'}}
    with open(nbfile, 'r', encoding='utf-8') as nbf:
        nbdata = nbformat.read(nbf, as_version=4, encoding='utf-8')

    runner = ExecutePreprocessor(timeout=600, kernel_name='probscale')
    runner.preprocess(nbdata, meta)

    img_folder = basename + '_files'
    body_raw, images = RSTExporter().from_notebook_node(nbdata)
    body_final = body_raw.replace('.. image:: ', '.. image:: {}/'.format(img_folder))

    with open(basename + '.rst', 'w', encoding='utf-8') as rst_out:
        rst_out.write(body_final)

    for img_name, img_data in images['outputs'].items():
        img_path = os.path.join(img_folder, img_name)
        with open(img_path, 'wb') as img:
            img.write(img_data) 
开发者ID:matplotlib,项目名称:mpl-probscale,代码行数:23,代码来源:make.py

示例14: get_pipeline_parameters

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def get_pipeline_parameters(request, source_notebook_path):
    """Get the pipeline parameters tagged in the notebook."""
    # read notebook
    log = request.log if hasattr(request, "log") else logger
    try:
        notebook = nbformat.read(source_notebook_path,
                                 as_version=nbformat.NO_CONVERT)
        params_source = parser.get_pipeline_parameters_source(notebook)
        if params_source == '':
            raise ValueError("No pipeline parameters found. Please tag a cell"
                             " of the notebook with the `pipeline-parameters`"
                             " tag.")
        # get a dict from the 'pipeline parameters' cell source code
        params_dict = ast.parse_assignments_expressions(params_source)
    except ValueError as e:
        log.exception("Value Error during parsing of pipeline parameters")
        raise RPCInternalError(details=str(e), trans_id=request.trans_id)
    # convert dict in list so its easier to parse in js
    params = [[k, *v] for k, v in params_dict.items()]
    log.info("Pipeline parameters:")
    for ln in tabulate(params, headers=["name", "type", "value"]).split("\n"):
        log.info(ln)
    return params 
开发者ID:kubeflow-kale,项目名称:kale,代码行数:25,代码来源:nb.py

示例15: get_pipeline_metrics

# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def get_pipeline_metrics(request, source_notebook_path):
    """Get the pipeline metrics tagged in the notebook."""
    # read notebook
    log = request.log if hasattr(request, "log") else logger
    try:
        notebook = nbformat.read(source_notebook_path,
                                 as_version=nbformat.NO_CONVERT)
        metrics_source = parser.get_pipeline_metrics_source(notebook)
        if metrics_source == '':
            raise ValueError("No pipeline metrics found. Please tag a cell"
                             " of the notebook with the `pipeline-metrics`"
                             " tag.")
        # get a dict from the 'pipeline parameters' cell source code
        metrics = ast.parse_metrics_print_statements(metrics_source)
    except ValueError as e:
        log.exception("Failed to parse pipeline metrics")
        raise RPCInternalError(details=str(e), trans_id=request.trans_id)
    log.info("Pipeline metrics: {}".format(metrics))
    return metrics 
开发者ID:kubeflow-kale,项目名称:kale,代码行数:21,代码来源:nb.py


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