def _warn_if_invalid(nb, version): """Log validation errors, if there are any.""" from IPython.nbformat import validate, ValidationError try: validate(nb, version=version) except ValidationError as e: get_logger().error("Notebook JSON is not valid v%i: %s", version, e)
def convert_to_v4(path): nb = read(path, 3) nb_new = convert(nb, 4) nb_new["metadata"] = { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.2" } } validate(nb_new) write(nb_new, path)
def validate_notebook_at(path): nb = open_nb(path) (version, version_minor) = get_version(nb) try: validate(nb, version=version, version_minor=version_minor) except ValidationError as e: return e
def convert_to_v4(path): nb = read(path, 3) nb_new = convert(nb, 4) nb_new["metadata"] ={ "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.2" } } validate(nb_new) write(nb_new, path)
def validate_notebook_model(self, model): """Add failed-validation message to model""" try: validate(model['content']) except ValidationError as e: model['message'] = u'Notebook Validation failed: {}:\n{}'.format( e.message, json.dumps(e.instance, indent=1, default=lambda obj: '<UNKNOWN>'), ) return model
def validate_notebook_model(self, model): """Add failed-validation message to model""" try: validate(model["content"]) except ValidationError as e: model["message"] = u"Notebook Validation failed: {}:\n{}".format( e.message, json.dumps(e.instance, indent=1, default=lambda obj: "<UNKNOWN>") ) return model
def main(paths): failed = False for path in paths: with io.open(path, 'r', encoding='utf-8') as f: nb = nbformat.read(f, 4) try: nbformat.validate(nb) except ValidationError as e: print(path, 'failed') print(e) else: print(path) with io.open(path, 'w', encoding='utf-8') as f: nbformat.write(nb, f)
def test_compress(tmpdir, notebooks): "It should compress a notebook." for notebook in notebooks: outfile = tmpdir.join('out.ipynb') saved = compress(notebook.strpath, outfile.strpath, 800, 'jpeg') # it should not gain in size assert saved > 0 # output file should exists assert outfile.check() validate(read(outfile.strpath, NO_CONVERT)) outfile.remove()
def test_downgrade_3(self): exporter = self.exporter_class(nbformat_version=3) (output, resources) = exporter.from_filename(self._get_notebook()) nb = json.loads(output) validate(nb)
def test_downgrade_notebook(): nb04 = copy.deepcopy(nbexamples.nb0) validate(nb04) nb03 = convert.downgrade(nb04) validate(nb03)
def test_upgrade_notebook(): nb03 = copy.deepcopy(v3examples.nb0) validate(nb03) nb04 = convert.upgrade(nb03) validate(nb04)