How to use the nbconvert.preprocessors 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 mwouts / jupytext / tests / test_execute.py View on Github external
def test_execute_readme_not_ok(tmpdir):
    tmp_md = str(tmpdir.join('notebook.md'))

    with open(tmp_md, 'w') as fp:
        fp.write("""
A readme with incorrect instructions (a is not defined)

```python
a + 1

""")

import nbconvert
with pytest.raises(nbconvert.preprocessors.execute.CellExecutionError, match="is not defined"):
    jupytext(args=[tmp_md, '--execute'])
github cuthbertLab / music21 / music21 / ext / nbconvert / nbconvertapp.py View on Github external
def _classes_default(self):
        classes = [NbConvertBase]
        for pkg in (exporters, preprocessors, writers, postprocessors):
            for name in dir(pkg):
                cls = getattr(pkg, name)
                if isinstance(cls, type) and issubclass(cls, Configurable):
                    classes.append(cls)
        
        return classes
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
github crew102 / reprexpy / reprexpy / reprex.py View on Github external
def _run_nb(statement_chunks, kernel_name):
    scode = _get_setup_code()
    statement_chunks = scode + statement_chunks

    nb = nbformat.v4.new_notebook()
    nb['cells'] = [
        nbformat.v4.new_code_cell('\n'.join(i))
        for i in statement_chunks
    ]
    if kernel_name is None:
        ep = nbconvert.preprocessors.ExecutePreprocessor(
            timeout=600, allow_errors=True
        )
    else:
        ep = nbconvert.preprocessors.ExecutePreprocessor(
            timeout=600, allow_errors=True, kernel_name=kernel_name
        )
    node_out, _ = ep.preprocess(nb, {})
    return node_out
github Chilipp / sphinx-nbexamples / sphinx_nbexamples / __init__.py View on Github external
if cell['cell_type'] == 'code':
                        cell = cell.copy()
                        break
                cell = cell.copy()
                cell.source = """
import logging
logging.captureWarnings(True)
logging.getLogger('py.warnings').setLevel(logging.ERROR)
"""
                nb.cells.insert(i, cell)

            t = dt.datetime.now()
            logger.info('Processing %s', self.infile)
            try:
                ep.preprocess(nb, {'metadata': {'path': in_dir}})
            except nbconvert.preprocessors.execute.CellExecutionError:
                logger.critical(
                    'Error while processing %s!', self.infile, exc_info=True)
            else:
                logger.info('Done. Seconds needed: %i',
                            (dt.datetime.now() - t).seconds)
            if disable_warnings:
                nb.cells.pop(i)

        if self.remove_tags:
            tp = nbconvert.preprocessors.TagRemovePreprocessor(timeout=300)
            for key, val in self.tag_options.items():
                setattr(tp, key, set(val))
            nb4rst = deepcopy(nb)
            tp.preprocess(nb4rst, {'metadata': {'path': in_dir}})
        else:
            nb4rst = nb
github crew102 / reprexpy / reprexpy / reprex.py View on Github external
def _run_nb(statement_chunks, kernel_name):
    scode = _get_setup_code()
    statement_chunks = scode + statement_chunks

    nb = nbformat.v4.new_notebook()
    nb['cells'] = [
        nbformat.v4.new_code_cell('\n'.join(i))
        for i in statement_chunks
    ]
    if kernel_name is None:
        ep = nbconvert.preprocessors.ExecutePreprocessor(
            timeout=600, allow_errors=True
        )
    else:
        ep = nbconvert.preprocessors.ExecutePreprocessor(
            timeout=600, allow_errors=True, kernel_name=kernel_name
        )
    node_out, _ = ep.preprocess(nb, {})
    return node_out
github Chilipp / sphinx-nbexamples / sphinx_nbexamples / __init__.py View on Github external
t = dt.datetime.now()
            logger.info('Processing %s', self.infile)
            try:
                ep.preprocess(nb, {'metadata': {'path': in_dir}})
            except nbconvert.preprocessors.execute.CellExecutionError:
                logger.critical(
                    'Error while processing %s!', self.infile, exc_info=True)
            else:
                logger.info('Done. Seconds needed: %i',
                            (dt.datetime.now() - t).seconds)
            if disable_warnings:
                nb.cells.pop(i)

        if self.remove_tags:
            tp = nbconvert.preprocessors.TagRemovePreprocessor(timeout=300)
            for key, val in self.tag_options.items():
                setattr(tp, key, set(val))
            nb4rst = deepcopy(nb)
            tp.preprocess(nb4rst, {'metadata': {'path': in_dir}})
        else:
            nb4rst = nb

        self.create_rst(nb4rst, in_dir, odir)

        if self.clear:
            cp.preprocess(nb, {'metadata': {'path': in_dir}})
        # write notebook file
        nbformat.write(nb, outfile)
        self.create_py(nb)