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


Python ExecutePreprocessor.preprocess方法代码示例

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


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

示例1: test_tutorial_nb

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def test_tutorial_nb(file_path):
    """Run tutorial jupyter notebook to catch any execution error.

    Parameters
    ----------
    file_path : str
        path of tutorial markdown file
    """
    tutorial_name = os.path.basename(file_path)
    notebook = nbformat.read(file_path + '.ipynb', as_version=4)
    eprocessor = ExecutePreprocessor(timeout=1800)
    try:
        eprocessor.preprocess(notebook, {'metadata': {}})
    except Exception as err:
        err_msg = str(err)
        fail_dict[tutorial_name] = err_msg
    finally:
        output_nb = open("output.txt", mode='w')
        nbformat.write(notebook, output_nb)
        output_nb.close()
        output_nb = open("output.txt", mode='r')
        for line in output_nb:
            if "Warning:" in line:
                fail_dict[tutorial_name] = "%s has warning." % (tutorial_name)
                return
开发者ID:Piyush3dB,项目名称:mxnet,代码行数:27,代码来源:test_tutorial.py

示例2: execute_nb

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def execute_nb(src, dst, allow_errors=False, timeout=1000, kernel_name=''):
    """
    Execute notebook in `src` and write the output to `dst`

    Parameters
    ----------
    src, dst: str
        path to notebook
    allow_errors: bool
    timeout: int
    kernel_name: str
        defualts to value set in notebook metadata

    Returns
    -------
    dst: str
    """
    import nbformat
    from nbconvert.preprocessors import ExecutePreprocessor

    with io.open(src, encoding='utf-8') as f:
        nb = nbformat.read(f, as_version=4)

    ep = ExecutePreprocessor(allow_errors=allow_errors,
                             timeout=timeout,
                             kernel_name=kernel_name)
    ep.preprocess(nb, resources={})

    with io.open(dst, 'wt', encoding='utf-8') as f:
        nbformat.write(nb, f)
    return dst
开发者ID:RogerThomas,项目名称:pandas,代码行数:33,代码来源:make.py

示例3: _notebook_run

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def _notebook_run(path):
    """Execute a notebook via nbconvert and collect output.
       :returns (parsed nb object, execution errors)
    """
    kernel_name = "python%d" % sys.version_info[0]
    this_file_directory = os.path.dirname(__file__)
    errors = []
    with tempfile.NamedTemporaryFile(suffix=".ipynb", mode="wt") as fout:
        with open(path) as f:
            nb = nbformat.read(f, as_version=4)
            nb.metadata.get("kernelspec", {})["name"] = kernel_name
            ep = ExecutePreprocessor(kernel_name=kernel_name, timeout=10)

            try:
                ep.preprocess(nb, {"metadata": {"path": this_file_directory}})
            except CellExecutionError as e:
                if "SKIP" in e.traceback:
                    print(str(e.traceback).split("\n")[-2])
                else:
                    raise e
            except TimeoutError as e:
                print(e)

            finally:
                nbformat.write(nb, fout)
        # nb = nbformat.read(fout, nbformat.current_nbformat)

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

    return nb, errors
开发者ID:RaRe-Technologies,项目名称:gensim,代码行数:34,代码来源:test_notebooks.py

示例4: _runTest

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
    def _runTest(self):
        kernel = 'python%d' % sys.version_info[0]
        cur_dir = os.path.dirname(self.nbfile)

        with open(self.nbfile) as f:
            nb = nbformat.read(f, as_version=4)
            if self.cov:
                covdict = {'cell_type': 'code', 'execution_count': 1,
                           'metadata': {'collapsed': True}, 'outputs': [],
                           'nbsphinx': 'hidden',
                           'source': 'import coverage\n'
                                     'coverage.process_startup()\n'
                                     'import sys\n'
                                     'sys.path.append("{0}")\n'.format(cur_dir)
                           }
                nb['cells'].insert(0, nbformat.from_dict(covdict))

            exproc = ExecutePreprocessor(kernel_name=kernel, timeout=600)

            try:
                run_dir = os.getenv('WRADLIB_BUILD_DIR', cur_dir)
                exproc.preprocess(nb, {'metadata': {'path': run_dir}})
            except CellExecutionError as e:
                raise e

        if self.cov:
            nb['cells'].pop(0)

        with io.open(self.nbfile, 'wt') as f:
            nbformat.write(nb, f)

        self.assertTrue(True)
开发者ID:heistermann,项目名称:wradlib,代码行数:34,代码来源:testrunner.py

示例5: execute_nb

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def execute_nb(src, dst, allow_errors=False, timeout=1000, kernel_name=None):
    '''
    Execute notebook in `src` and write the output to `dst`

    Parameters
    ----------
    src, dst: str
        path to notebook
    allow_errors: bool
    timeout: int
    kernel_name: str
        defualts to value set in notebook metadata

    Returns
    -------
    dst: str
    '''
    with io.open(src, encoding='utf-8') as f:
        nb = nbformat.read(f, as_version=4)

    ep = ExecutePreprocessor(allow_errors=False,
                             timeout=timeout,
                             kernel_name=kernel_name)
    ep.preprocess(nb, {'metadata': {'path': SOURCE_DIR}})

    with io.open(dst, 'wt', encoding='utf-8') as f:
        nbformat.write(nb, f)
    return dst
开发者ID:ChadFulton,项目名称:statsmodels,代码行数:30,代码来源:nbgenerate.py

示例6: nb_to_html

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def nb_to_html(root, template='basic', version=4, timeout=600, kernel='python3'):
    '''
    This functions executes a Jupyter notebook and creates the related
    HTML file.

    Args:
        root (str): name of the file without the .ipynb extension
        template (str): name of the template (to be in the current folder as template.tpl)
        version (int): version of the notebook
        timeout (float): maximum time spent per cell
        kernel (str)

    Returns:
        None

    The function executes root.ipynb into root_exe.ipynb and creates the file root.html.
    '''
    with open(root + '.ipynb') as f:
        nb = nbformat.read(f, as_version=version)

    ep = ExecutePreprocessor(timeout=timeout, kernel_name=kernel)
    ep.preprocess(nb, {'metadata': {'path': '.'}})

    with open(root + '_exe.ipynb', 'wt') as f:
        nbformat.write(nb, f)

    html_exporter = HTMLExporter()
    html_exporter.template_file = template

    with open(root + '_exe.ipynb', mode='r') as f:
        notebook = nbformat.reads(''.join(f.readlines()), as_version=version)
        (body, _) = html_exporter.from_notebook_node(notebook)
        codecs.open(root + '.html', 'w', encoding='utf-8').write(body)
开发者ID:PetitLepton,项目名称:design,代码行数:35,代码来源:nb_to_report.py

示例7: run_notebook

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def run_notebook(notebook_name, nb_kwargs=None,
                 insert_pos=5, timeout=120, execute_kwargs=None):
    """Runs a notebook and displays the output in the master notebook.

    Executes a notebook, optionally passing "arguments" in a way roughly
    similar to passing arguments to a function.
    Notebook arguments are passed in a dictionary (`nb_kwargs`) which is
    converted to a string containing python code, then inserted in the notebook
    as a code cell. The code contains only assignments of variables which
    can be used to control the execution of a suitably written notebook. When
    calling a notebook, you need to know which arguments (variables) to pass.
    Differently from functions, no check on the input arguments is performed.

    Arguments:
        notebook_name (string): name of the notebook to be executed.
        nb_kwargs (dict or None): If not None, this dict is converted to a
            string of python assignments with keys representing variables
            names and values variables content. This string is inserted as
            code-cell in the notebook to be executed.
        insert_pos (int): position of insertion of the code-cell containing
            the input arguments. Default is 5 (i.e. sixth cell). With this
            default, the input notebook can define, in the first cell, default
            values of input arguments (used when the notebook is executed
            with no arguments or through the Notebook GUI).
        timeout (int): timeout in seconds after which the execution is aborted.
        execute_kwargs (dict): additional arguments passed to
            `ExecutePreprocessor`.
    """
    if nb_kwargs is not None:
        header = '# Cell inserted during automated execution.'
        code = dict_to_code(nb_kwargs)
        code_cell = '\n'.join((header, code))

    if execute_kwargs is None:
        execute_kwargs = {}
    ep = ExecutePreprocessor(timeout=timeout, **execute_kwargs)
    nb = nbformat.read(notebook_name, as_version=4)
    if len(nb_kwargs) > 0:
        nb['cells'].insert(insert_pos, nbformat.v4.new_code_cell(code_cell))

    try:
        # Execute the notebook
        ep.preprocess(nb, {'metadata': {'path': './'}})
#*****************************************
#    Title: analysis.ipynb
#    Author: Benjamin RK
#    Date: April 2013
#    Availability: https://gist.github.com/minrk/5491090

        ip = get_ipython()
        for cell in nb.cells:
            if cell.cell_type != 'code':
                continue
            ip.run_cell(cell.source)
#*****************************************
    except:
        # Execution failed, print a message then raise.
        msg = 'Error executing the notebook "%s".\n\n' % notebook_name
        print(msg)
        raise
开发者ID:shibberu,项目名称:Big-Data-MVP,代码行数:62,代码来源:nbrun.py

示例8: execute

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
    def execute(self):
        print("Cleaning lowfat/reports/html ...")
        old_reports = os.listdir("lowfat/reports/html")
        for old_report in old_reports:
            print("- Removing lowfat/reports/html/{}".format(old_report))
            os.remove("lowfat/reports/html/{}".format(old_report))
        print("Cleaning of lowfat/reports/html is complete.")

        notebook_filenames = os.listdir("lowfat/reports")

        for notebook_filename in notebook_filenames:
            if not notebook_filename.endswith(".ipynb"):
                continue

            print("Processing lowfat/reports/{}".format(notebook_filename))

            # Based on Executing notebooks, nbconvert Documentation by Jupyter Development Team.
            # https://nbconvert.readthedocs.io/en/latest/execute_api.html
            with open("lowfat/reports/{}".format(notebook_filename)) as file_:
                notebook = nbformat.read(file_, as_version=4)

                # Kernel is provided by https://github.com/django-extensions/django-extensions/
                execute_preprocessor = ExecutePreprocessor(timeout=600, kernel_name='django_extensions')
                execute_preprocessor.preprocess(notebook, {'metadata': {'path': '.'}})

                html_exporter = HTMLExporter()
                html_exporter.template_file = 'basic'

                (body, dummy_resources) = html_exporter.from_notebook_node(notebook)

                with open('lowfat/reports/html/{}.html'.format(notebook_filename), 'wt') as file_:
                    file_.write(body)
开发者ID:softwaresaved,项目名称:fat,代码行数:34,代码来源:report.py

示例9: execute_nb

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def execute_nb(src, dst, allow_errors=False, timeout=1000, kernel_name=None):
    """
    Execute notebook in `src` and write the output to `dst`

    Parameters
    ----------
    src, dst: str
        path to notebook
    allow_errors: bool
    timeout: int
    kernel_name: str
        defualts to value set in notebook metadata

    Returns
    -------
    dst: str
    """
    with io.open(src, encoding="utf-8") as f:
        nb = nbformat.read(f, as_version=4)

    ep = ExecutePreprocessor(allow_errors=allow_errors, timeout=timeout, kernel_name=kernel_name)
    ep.preprocess(nb, {"metadta": {"path": "notebooks/"}})

    with io.open(dst, "wt", encoding="utf-8") as f:
        nbformat.write(nb, f)
    return dst
开发者ID:statsmodels,项目名称:statsmodels,代码行数:28,代码来源:nbgenerate.py

示例10: main

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def main():
    arguments = docopt(__doc__, version='nbgen 2.0')

    cmd = subprocess.run([arguments["<path>"]] + arguments["<arguments>"], stdout=subprocess.PIPE)
    cmd.check_returncode()
    cells = json.loads(cmd.stdout.decode("utf-8"))

    nb_dict = {
      "metadata": {},
      "nbformat": 4,
      "nbformat_minor": 0,
      "cells": cells,
    }
    notebook = nbformat.from_dict(nb_dict)

    ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
    ep.preprocess(notebook, {'metadata': {}})

    if arguments["nb"]:
        nbformat.write(notebook, "{}.ipynb".format(arguments["<name>"]))

    elif arguments["slides"]:
        config = Config()
        reveal_cdn = "https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.3.0/"
        config.SlidesExporter.reveal_url_prefix = (arguments["--reveal"] or reveal_cdn)

        slides, __ = export_slides(nb=notebook, config=config)
        with open("{}.html".format(arguments["<name>"]), "w") as html_file:
            html_file.write(slides)
开发者ID:ewjoachim,项目名称:nbgen,代码行数:31,代码来源:__init__.py

示例11: _notebook_run

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def _notebook_run(path):
    """Execute a notebook via nbconvert and collect output.
       :returns (parsed nb object, execution errors)
    """
    kernel_name = 'python%d' % sys.version_info[0]
    this_file_directory = os.path.dirname(__file__)
    errors = []
    with tempfile.NamedTemporaryFile(suffix=".ipynb", mode='wt') as fout:
        with open(path) as f:
            nb = nbformat.read(f, as_version=4)
            nb.metadata.get('kernelspec', {})['name'] = kernel_name
            ep = ExecutePreprocessor(kernel_name=kernel_name, timeout=10)

            try:
                ep.preprocess(nb, {'metadata': {'path': this_file_directory}})
            except CellExecutionError as e:
                if "SKIP" in e.traceback:
                    print(str(e.traceback).split("\n")[-2])
                else:
                    raise e
            except RuntimeError as e:
                print(e)

            finally:
                nbformat.write(nb, fout)

    return nb, errors
开发者ID:abs51295,项目名称:gensim,代码行数:29,代码来源:test_notebooks.py

示例12: test_notebook

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def test_notebook(notebook):
    nb_name = os.path.split(notebook)[-1]
    if nb_name in SLOW_NOTEBOOKS:
        pytest.skip('Notebook is too slow to test')
    nb = nbformat.read(notebook, as_version=4)
    ep = ExecutePreprocessor(allow_errors=False,
                             timeout=240,
                             kernel_name=kernel_name)
    ep.preprocess(nb, {'metadata': {'path': NOTEBOOK_DIR}})
开发者ID:bashtage,项目名称:arch,代码行数:11,代码来源:test_examples.py

示例13: run_ipynb

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def run_ipynb(fs):
    """
    """
    with open(fs) as f:
        nb = nbformat.read(f, as_version=4)
    ep = ExecutePreprocessor()
    
    ep.preprocess(nb, {'metadata': {'path': os.path.dirname(fs)}})
    with open(fs, 'wt') as f:
        nbformat.write(nb, fs)
开发者ID:bartvle,项目名称:Synthacc,代码行数:12,代码来源:run_demos.py

示例14: write_notebook

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def write_notebook(cells, outputfile, execute=True, kernel='python3'):
    kernelspec = get_kernelspec(kernel)
    notebook = new_notebook(cells=cells,
                            metadata={'language': 'python',
                                      'kernelspec': kernelspec})

    if execute:
        ep = ExecutePreprocessor(timeout=600, kernelname='python3')
        ep.preprocess(notebook,
                      {'metadata': {'path': os.path.dirname(outputfile)}})

    nbformat.write(notebook, outputfile)
开发者ID:iliatimofeev,项目名称:altair_notebooks,代码行数:14,代码来源:create_example_notebooks.py

示例15: run_notebook

# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def run_notebook(notebook):
    # See http://nbconvert.readthedocs.io/en/latest/execute_api.html
    # TODO: Specify 'django_extensions' kernel and make it work on the server.
    # The kernel can be set as follows:
    #   ep = ExecutePreprocessor(timeout=120, kernel_name='django_extensions')
    # This works locally, but on server, I wasn't able to create the kernel
    # (list available kernels by `jupyter kernelspec list`).
    # Default kernel currently works, given the `path` (directory from where to
    # execute the notebook) is set to //backend. It may fail if some Django
    # features are used in the notebook, but I haven't explored this.
    ep = ExecutePreprocessor(timeout=120)
    path = os.path.join(settings.REPO_DIR, 'backend')
    ep.preprocess(notebook, {'metadata': {'path': path}})
开发者ID:adaptive-learning,项目名称:robomission,代码行数:15,代码来源:export_monitoring_notebook.py


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