How to use the nbconvert.preprocessors.Preprocessor 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 Metatab / metatab / metatab / jupyter / preprocessors.py View on Github external
self.doc = MetatabDoc(TextRowGenerator(o))

                # Give all of the sections their standard args, to make the CSV versions of the doc
                # prettier

                for name, s in self.doc.sections.items():
                    try:
                        s.args = self.doc.decl_sections[name.lower()]['args']
                    except KeyError:
                        pass

        return cell, resources


class RemoveMetatab(Preprocessor):
    """NBConvert preprocessor to remove the %metatab block"""

    def preprocess(self, nb, resources):
        import re

        out_cells = []

        mt_doc_name = 'mt_pkg'

        for cell in nb.cells:

            source = cell['source']

            if source.startswith('%%metatab'):

                lines = source.splitlines() # resplit to remove leading blank lines
github stellargraph / stellargraph / scripts / format_notebooks.py View on Github external
print(
                        f"Removing Tensorflow warning in code cell {cell.execution_count}"
                    )
                    output["text"] = self.sub_warn.sub("", output.get("text", ""))

                # Clear std errors
                if self.filter_all_stderr and output.get("name") == "stderr":
                    print(f"Removing stderr in code cell {cell.execution_count}")
                    continue

                pro_outputs.append(output)
            cell.outputs = pro_outputs
        return cell, resources


class RenumberCodeCellPreprocessor(preprocessors.Preprocessor):
    def preprocess(self, nb, resources):
        self.code_index = 0
        return super().preprocess(nb, resources)

    def preprocess_cell(self, cell, resources, cell_index):
        if cell.cell_type == "code":
            self.code_index += 1
            cell.execution_count = self.code_index
        return cell, resources


class SetKernelSpecPreprocessor(preprocessors.Preprocessor):
    def preprocess(self, nb, resources):
        # Set the default kernel:
        if (
            "kernelspec" in nb.metadata
github pyviz-dev / nbsite / nbsite / gallery / thumbnailer.py View on Github external
def strip_specific_magics(source, magic):
    """
    Given the source of a cell, filter out specific cell and line magics.
    """
    filtered=[]
    for line in source.splitlines():
        if line.startswith(f'%{magic}'):
            filtered.append(line.lstrip(f'%{magic}').strip(' '))
        if line.startswith(f'%%{magic}'):
            filtered.append(line.lstrip(f'%%{magic}').strip(' '))
        else:
            filtered.append(line)
    return '\n'.join(filtered)


class StripTimeMagicsProcessor(Preprocessor):
    """
    Preprocessor to convert notebooks to Python source strips out just time
    magics while keeping the rest of the cell.
    """

    def preprocess_cell(self, cell, resources, index):
        if cell['cell_type'] == 'code':
            cell['source'] = strip_specific_magics(cell['source'], 'time')
        return cell, resources

    def __call__(self, nb, resources): return self.preprocess(nb,resources)


def strip_trailing_semicolons(source, function):
    """
    Give the source of a cell, filter out lines that contain a specified
github Metatab / metatab / metatab / jupyter / preprocessors.py View on Github external
source = cell['source']

                tags = cell['metadata'].get('tags',[])

                if source.startswith('%mt_showinput') or 'show' in tags:
                    cell['source'] = re.sub(r'\%mt_showinput','',source)
                else:
                    cell['metadata']['hide_input'] = True

            out_cells.append(cell)

        nb.cells = out_cells

        return nb, resources

class ExtractMetatabTerms(Preprocessor):
    """Look for tagged markdown cells and use the value to set some metatab doc terms"""

    terms = None

    def preprocess_cell(self, cell, resources, index):

        if not self.terms:
            self.terms = []

        if cell['cell_type'] == 'markdown':

            tags = cell['metadata'].get('tags', [])

            if 'Title' in tags:
                self.terms.append(('Root.Title', cell.source.strip().replace('#', '')))
github pyviz-dev / nbsite / nbsite / nbbuild.py View on Github external
self._kc=v
        if self._ipython_startup is not None:
            msg_id = self._kc.execute( # noqa: a mess
                self._ipython_startup,silent=False,store_history=False,allow_stdin=False,stop_on_error=True)


    def handle_comm_msg(self, outs, msg, cell_index):
        """
        Comm messages are not handled correctly in some cases so we
        just ignore them during export.
        """
        pass


            
class SkipOutput(Preprocessor):
    """A transformer to skip the output for cells containing a certain string"""

    def __init__(self, substring=None, **kwargs):
        self.substring = substring
        super(SkipOutput, self).__init__(**kwargs)

    def preprocess_cell(self, cell, resources, index):
        if cell['cell_type'] == 'code':
            if self.substring in cell['source']:
                cell['outputs'] = []
        return cell, resources


    def __call__(self, nb, resources): return self.preprocess(nb,resources)
github pyviz-dev / nbsite / nbsite / gallery / thumbnailer.py View on Github external
def __call__(self, nb, resources): return self.preprocess(nb,resources)


def thumbnail(obj, basename):
    import os
    if isinstance(obj, Dimensioned) and not os.path.isfile(basename+'.png'):
        Store.renderers[Store.current_backend].save(obj, basename, fmt='png')
    elif 'panel' in sys.modules:
        from panel.viewable import Viewable
        if isinstance(obj, Viewable) and not os.path.isfile(basename+'.png'):
            obj.save(basename+'.png')
    return obj


class ThumbnailProcessor(Preprocessor):

    def __init__(self, basename, **kwargs):
        self.basename = basename
        super(ThumbnailProcessor, self).__init__(**kwargs)

    def preprocess_cell(self, cell, resources, index):
        if cell['cell_type'] == 'code':
            template = 'from nbsite.gallery.thumbnailer import thumbnail;thumbnail({{expr}}, {basename!r})'
            cell['source'] = wrap_cell_expression(cell['source'],
                                                  template.format(
                                                      basename=self.basename))
        return cell, resources

    def __call__(self, nb, resources): return self.preprocess(nb,resources)
github ipython-contrib / jupyter_contrib_nbextensions / src / jupyter_contrib_nbextensions / nbconvert_support / nbTranslate.py View on Github external
'te': 'Telugu',
    'th': 'Thai',
    'tr': 'Turkish',
    'uk': 'Ukrainian',
    'ur': 'Urdu',
    'uz': 'Uzbek',
    'vi': 'Vietnamese',
    'cy': 'Welsh',
    'xh': 'Xhosa',
    'yi': 'Yiddish',
    'yo': 'Yoruba',
    'zu': 'Zulu'
}


class nbTranslatePreprocessor(Preprocessor):

    def __init__(self, lang='en', **kw):
        self.language = lang

    def __call__(self, nb, resources, lang='en'):
        if self.enabled:
            self.log.debug("Applying preprocessor: %s",
                           self.__class__.__name__)
            return self.preprocess(nb, resources)
        else:
            return nb, resources

    def preprocess(self, nb, resources):
        """
        Preprocessing to apply on each notebook.
github jbandlow / nb_hugo_exporter / nbhugoexporter / hugopreprocessor.py View on Github external
This module exports a single class.

    HugoPreprocessor: An `nbconvert` `Preprocessor` for exporting
    notebooks to a Markdown format compatible with
    [Hugo](https://gohugo.io)

"""
import datetime
import os.path
import re

from nbconvert.preprocessors import Preprocessor


class HugoPreprocessor(Preprocessor):
    r"""Preprocessor class for Hugo.

    This class overrides the `preprocess` and `preprocess_cell` methods
    of the `nbcovert` `Preprocessor` class, to accomplish the following
    tasks:

    1.  Properly quote underscores in math mode. See
    https://gohugo.io/content-management/formats/#issues-with-markdown
    for more context on the problem. This resolves the issue with the
    "tedious" solution of quoting all underscores.

    2.  Set default values for metadata (date, title, and draft).
    """

    def _insert_newline_before_lists(self, text):
        r"""Ensure that there is a blank line before all lists."""
github pyviz-dev / nbsite / nbsite / nbbuild.py View on Github external
def __init__(self, substring=None, **kwargs):
        self.substring = substring
        super(SkipOutput, self).__init__(**kwargs)

    def preprocess_cell(self, cell, resources, index):
        if cell['cell_type'] == 'code':
            if self.substring in cell['source']:
                cell['outputs'] = []
        return cell, resources


    def __call__(self, nb, resources): return self.preprocess(nb,resources)


class NotebookSlice(Preprocessor):
    """A transformer to select a slice of the cells of a notebook"""


    def __init__(self, substring=None, end=None, offset=0, **kwargs):
        self.substring = substring
        self.end = end
        self.offset = offset
        super(NotebookSlice, self).__init__(**kwargs)

    def _find_slice(self, nbc, substring, endstr):
        start = 0 if substring is None else None
        end = None
        try:
            end = int(endstr)
            count = 0
        except:
github Metatab / metatab / metatab / jupyter / preprocessors.py View on Github external
"""Remove line magic lines, or entire cell magic cells"""

    def preprocess(self, nb, resources):
        import re

        for i, cell in enumerate(nb.cells):


            if re.match(r'^\%\%', cell.source):
                cell.source = ''
            else:
                cell.source = re.sub(r'\%[^\n]+\n?', '', cell.source)

        return nb, resources

class NoShowInput(Preprocessor):
    """NBConvert preprocessor to add hide_input metatab to cells, except to cells that have either
     an %mt_showinput magic, or a 'show' tag """

    def preprocess(self, nb, resources):
        import re

        out_cells = []

        for cell in nb.cells:

            #  Code cells aren't displayed at all, unless it starts with
            # a '%mt_showinput' magic, which is removed

            if cell['cell_type'] == 'code':

                source = cell['source']