How to use the nbconvert.preprocessors.ClearOutputPreprocessor function in nbconvert

To help you get started, we’ve selected a few nbconvert examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github opengeophysics / testipynb / testipynb / testipynb.py View on Github external
print(
            "\n---------------------"
            " Testing {0}.ipynb "
            "---------------------".format(nbname)
        )

        if (
            (nbname in self.ignore) or
            (nbname in self.py2_ignore and sys.version_info[0] == 2)
        ):
            print(" Skipping {}".format(nbname))
            return

        run_path = os.path.sep.join(nbpath.split(os.path.sep)[:-1])
        os.chdir(run_path)
        ep = ClearOutputPreprocessor(
            resources={'metadata': {'path': run_path}}
        )

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

            ep.preprocess(nb, {})

            ex = ExecutePreprocessor(
                timeout=timeout,
                kernel_name='python{}'.format(sys.version_info[0]),
                allow_errors=True,
                resources={'metadata': {'path': run_path}}
            )

            out = ex.preprocess(nb, {})
github geoscixyz / em_examples / tests / test_notebooks.py View on Github external
def test_func(self):
        passing = True
        print("\n--------------- Testing {0} ---------------".format(nbname))
        print("   {0}".format(nbpath))

        if nbname in py2Ignore and sys.version_info[0] == 2:
            print(" Skipping {}".format(nbname))
            return

        ep = ClearOutputPreprocessor()

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

            ep.preprocess(nb, {})

            ex = ExecutePreprocessor(
                timeout=600,
                kernel_name='python{}'.format(sys.version_info[0]),
                allow_errors=True
            )

            out = ex.preprocess(nb, {})

            for cell in out[0]['cells']:
                if 'outputs' in cell.keys():
github Chilipp / sphinx-nbexamples / sphinx_nbexamples / __init__.py View on Github external
def process_notebook(self, disable_warnings=True):
        """Process the notebook and create all the pictures and files

        This method runs the notebook using the :mod:`nbconvert` and
        :mod:`nbformat` modules. It creates the :attr:`outfile` notebook,
        a python and a rst file"""
        infile = self.infile
        outfile = self.outfile
        in_dir = os.path.dirname(infile) + os.path.sep
        odir = os.path.dirname(outfile) + os.path.sep
        create_dirs(os.path.join(odir, 'images'))
        ep = nbconvert.preprocessors.ExecutePreprocessor(
            timeout=300)
        cp = nbconvert.preprocessors.ClearOutputPreprocessor(
            timeout=300)

        self.nb = nb = nbformat.read(infile, nbformat.current_nbformat)

        language_info = getattr(nb.metadata, 'language_info', {})
        ext = language_info.get('file_extension', 'py')
        self.script = self.get_out_file(ext.lstrip('.'))

        disable_warnings = disable_warnings and self.script.endswith('.py')

        # write and process rst_file
        if self.preprocess:

            # disable warnings in the rst file
            if disable_warnings:
                for i, cell in enumerate(nb.cells):
github newaetech / chipwhisperer / jupyter / Test_Notebook.py View on Github external
def clear_notebook(path):
    real_path = Path(path)
    body = ""
    with open(real_path, "r", encoding="utf-8") as nbfile:
        nb = nbformat.read(nbfile, as_version=4)
        co = ClearOutputPreprocessor()
        exporter = NotebookExporter()
        node, resources = co.preprocess(nb, {'metadata': {'path': './'}})
        body, resources = exporter.from_notebook_node(node, resources)
    with open(real_path, "w", encoding="utf-8") as nbfile:
        nbfile.write(body)
github Metatab / metatab / metatab / jupyter / exporters.py View on Github external
def clear_output(self, nb):

        from nbconvert.preprocessors import ClearOutputPreprocessor
        return ClearOutputPreprocessor().preprocess(nb, {})
github voila-dashboards / voila / voila / execute.py View on Github external
def executenb(nb, cwd=None, km=None, **kwargs):
    resources = {}
    if cwd is not None:
        resources['metadata'] = {'path': cwd}  # pragma: no cover
    # Clear any stale output, in case of exception
    nb, resources = ClearOutputPreprocessor().preprocess(nb, resources)
    ep = VoilaExecutePreprocessor(**kwargs)
    return ep.preprocess(nb, resources, km=km)[0]
github jupyter / nbgrader / nbgrader / docs / source / clear_docs.py View on Github external
# cleanup ignored files
    run(['git', 'clean', '-fdX', root])

    # remove release/autograded/feedback
    if os.path.exists(os.path.join(root, "user_guide", "release")):
        shutil.rmtree(os.path.join(root, "user_guide", "release"))
    if os.path.exists(os.path.join(root, "user_guide", "autograded")):
        shutil.rmtree(os.path.join(root, "user_guide", "autograded"))
    if os.path.exists(os.path.join(root, "user_guide", "feedback")):
        shutil.rmtree(os.path.join(root, "user_guide", "feedback"))
    if os.path.exists(os.path.join(root, "user_guide", "downloaded", "ps1", "extracted")):
        shutil.rmtree(os.path.join(root, "user_guide", "downloaded", "ps1", "extracted"))

    print("Clearing outputs of notebooks in '{}'...".format(os.path.abspath(root)))
    preprocessor = ClearOutputPreprocessor()

    for dirpath, dirnames, filenames in os.walk(root):
        is_submitted = _check_if_directory_in_path(dirpath, 'submitted')

        for filename in sorted(filenames):
            if os.path.splitext(filename)[1] == '.ipynb':
                # read in the notebook
                pth = os.path.join(dirpath, filename)
                with open(pth, 'r') as fh:
                    orig_nb = read(fh, 4)

                # copy the original notebook
                new_nb = clean_notebook(orig_nb)

                # check outputs of all the cells
                if not is_submitted: