Ejemplo n.º 1
0
    def preprocess(self, nb, resources):
        """Concatenates the cells from the header and footer notebooks to the
        given cells.

        """
        new_cells = []

        # header
        if self.header:
            with open(self.header, 'r') as fh:
                header_nb = read_nb(fh, as_version=current_nbformat)
            new_cells.extend(header_nb.cells)

        # body
        new_cells.extend(nb.cells)

        # footer
        if self.footer:
            with open(self.footer, 'r') as fh:
                footer_nb = read_nb(fh, as_version=current_nbformat)
            new_cells.extend(footer_nb.cells)

        nb.cells = new_cells
        super(IncludeHeaderFooter, self).preprocess(nb, resources)

        return nb, resources
Ejemplo n.º 2
0
    def preprocess(self, nb, resources):
        """Concatenates the cells from the header and footer notebooks to the
        given cells.

        """
        new_cells = []

        # header
        if self.header:
            with open(self.header, 'r') as fh:
                header_nb = read_nb(fh, as_version=current_nbformat)
            new_cells.extend(header_nb.cells)

        # body
        new_cells.extend(nb.cells)

        # footer
        if self.footer:
            with open(self.footer, 'r') as fh:
                footer_nb = read_nb(fh, as_version=current_nbformat)
            new_cells.extend(footer_nb.cells)

        nb.cells = new_cells
        super(IncludeHeaderFooter, self).preprocess(nb, resources)

        return nb, resources
Ejemplo n.º 3
0
def transform(nb_path, *fs):
    with open(nb_path, "r") as fp:
        nb = read_nb(fp, as_version=4)

    for f in fs:
        f(nb)

    with open(nb_path, "w") as fp:
        write_nb(nb, nb_path)
Ejemplo n.º 4
0
    def validate(
            self, filename: str
    ) -> typing.Dict[str, typing.List[typing.Dict[str, str]]]:
        self.log.info("Validating '{}'".format(os.path.abspath(filename)))
        basename = os.path.basename(filename)
        dirname = os.path.dirname(filename)
        with utils.chdir(dirname):
            nb = read_nb(basename, as_version=current_nbformat)

        type_changed = self._get_type_changed_cells(nb)
        if len(type_changed) > 0:
            results = {}
            results['type_changed'] = [{
                "source":
                cell.source.strip(),
                "old_type":
                cell.cell_type,
                "new_type":
                cell.metadata.nbgrader.cell_type
            } for cell in type_changed]
            return results

        with utils.chdir(dirname):
            nb = self._preprocess(nb)
        changed = self._get_changed_cells(nb)
        passed = self._get_passed_cells(nb)
        failed = self._get_failed_cells(nb)

        results = {}

        if not self.ignore_checksums and len(changed) > 0:
            results['changed'] = [{
                "source": cell.source.strip()
            } for cell in changed]

        elif self.invert:
            if len(passed) > 0:
                results['passed'] = [{
                    "source": cell.source.strip()
                } for cell in passed]

        else:
            if len(failed) > 0:
                results['failed'] = [{
                    "source":
                    cell.source.strip(),
                    "error":
                    ansi2html(self._extract_error(cell)),
                    "raw_error":
                    self._extract_error(cell)
                } for cell in failed]

        return results
Ejemplo n.º 5
0
 def verify_notebook(self, nbfile):
     """
     verify_notebook: Runs a notebook and ensures that all cells execute without errors.
     """
     from nbformat import read as read_nb, NO_CONVERT
     try:
         pass
         # First newline avoids the confusing "F"/"." output of unittest
         #            print("\nTesting " + nbfile)
         nb = read_nb(nbfile, NO_CONVERT)
         self.preprocessor.preprocess(nb, {})
     except Exception as err:
         self.fail(err)
Ejemplo n.º 6
0
 def verify_notebook(self, nbfile):
     """
     verify_notebook: Runs a notebook and ensures that all cells execute without errors.
     """
     from nbformat import read as read_nb, NO_CONVERT
     try:
         # First newline avoids the confusing "F"/"." output of unittest
         print("\nTesting " + nbfile)
         nb = read_nb(nbfile, NO_CONVERT)
         if self._in_pyspark():
             nb = self.edit_notebook(nb)
         self.preprocessor.preprocess(nb, {})
     except Exception as err:
         self.fail(err)
Ejemplo n.º 7
0
    def validate(self, filename):
        self.log.info("Validating '{}'".format(os.path.abspath(filename)))
        basename = os.path.basename(filename)
        dirname = os.path.dirname(filename)
        with utils.chdir(dirname):
            nb = read_nb(basename, as_version=current_nbformat)

        type_changed = self._get_type_changed_cells(nb)
        if len(type_changed) > 0:
            results = {}
            results['type_changed'] = [{
                "source": cell.source.strip(),
                "old_type": cell.cell_type,
                "new_type": cell.metadata.nbgrader.cell_type
            } for cell in type_changed]
            return results

        # Changing directories to enable validation to run in the same
        # environment as the original notebook.
        with utils.chdir(dirname):   
            nb = self._preprocess(nb)

        changed = self._get_changed_cells(nb)
        passed = self._get_passed_cells(nb)
        failed = self._get_failed_cells(nb)

        results = {}

        if not self.ignore_checksums and len(changed) > 0:
            results['changed'] = [{
                "source": cell.source.strip()
            } for cell in changed]

        elif self.invert:
            if len(passed) > 0:
                results['passed'] = [{
                    "source": cell.source.strip()
                } for cell in passed]

        else:
            if len(failed) > 0:
                results['failed'] = [{
                    "source": cell.source.strip(),
                    "error": ansi2html(self._extract_error(cell)),
                    "raw_error": self._extract_error(cell)
                } for cell in failed]

        return results
Ejemplo n.º 8
0
    def validate(self, filename):
        self.log.info("Validating '{}'".format(os.path.abspath(filename)))
        basename = os.path.basename(filename)
        dirname = os.path.dirname(filename)
        with utils.chdir(dirname):
            nb = read_nb(basename, as_version=current_nbformat)

        type_changed = self._get_type_changed_cells(nb)
        if len(type_changed) > 0:
            results = {}
            results['type_changed'] = [{
                "source": cell.source.strip(),
                "old_type": cell.cell_type,
                "new_type": cell.metadata.nbgrader.cell_type
            } for cell in type_changed]
            return results

        with utils.chdir(dirname):
            nb = self._preprocess(nb)
        changed = self._get_changed_cells(nb)
        passed = self._get_passed_cells(nb)
        failed = self._get_failed_cells(nb)

        results = {}

        if not self.ignore_checksums and len(changed) > 0:
            results['changed'] = [{
                "source": cell.source.strip()
            } for cell in changed]

        elif self.invert:
            if len(passed) > 0:
                results['passed'] = [{
                    "source": cell.source.strip()
                } for cell in passed]

        else:
            if len(failed) > 0:
                results['failed'] = [{
                    "source": cell.source.strip(),
                    "error": ansi2html(self._extract_error(cell)),
                    "raw_error": self._extract_error(cell)
                } for cell in failed]

        return results
Ejemplo n.º 9
0
def load_example(example):
    with open(example + ".ipynb", "r") as f:
        nb = read_nb(f, 4)
    return nb
Ejemplo n.º 10
0
 def _read_nb(self, filename):
     fullpath = os.path.join(os.path.dirname(__file__), filename)
     with open(fullpath, "r") as fh:
         nb = read_nb(fh, as_version=current_nbformat)
     return nb
Ejemplo n.º 11
0
 def _read_nb(self, filename):
     fullpath = os.path.join(os.path.dirname(__file__), filename)
     with open(fullpath, "r") as fh:
         nb = read_nb(fh, as_version=current_nbformat)
     return nb