Python nbformat.from_dict() Examples

The following are 9 code examples of nbformat.from_dict(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module nbformat , or try the search function .
Example #1
Source File: views.py    From MPContribs with MIT License 6 votes vote down vote up
def export_notebook(nb, cid):
    nb = nbformat.from_dict(nb)
    html_exporter = HTMLExporter()
    html_exporter.template_file = "basic"
    body = html_exporter.from_notebook_node(nb)[0]
    soup = BeautifulSoup(body, "html.parser")
    # mark cells with special name for toggling, and
    # TODO make element id's unique by appending cid (for ingester)
    for div in soup.find_all("div", "output_wrapper"):
        script = div.find("script")
        if script:
            script = script.contents[0]
            if script.startswith("render_json"):
                div["name"] = "HData"
            elif script.startswith("render_table"):
                div["name"] = "Tables"
            elif script.startswith("render_plot"):
                div["name"] = "Graphs"
        else:
            pre = div.find("pre")
            if pre and pre.contents[0].startswith("Structure"):
                div["name"] = "Structures"
    # name divs for toggling code_cells
    for div in soup.find_all("div", "input"):
        div["name"] = "Code"
    # separate script
    script = []
    for s in soup.find_all("script"):
        script.append(s.string)
        s.extract()  # remove javascript
    return soup.prettify(), "\n".join(script) 
Example #2
Source File: test_execute.py    From jupyter-sphinx with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_image_mimetype_uri(doctree):
    # tests the image uri paths on conversion to docutils image nodes
    priority =  ['image/png', 'image/jpeg', 'text/latex', 'text/plain']
    output_dir = '/_build/jupyter_execute'
    img_locs = ['/_build/jupyter_execute/docs/image_1.png','/_build/jupyter_execute/image_2.png']

    cells = [
        {'outputs':
            [{'data': {'image/png': 'Vxb6L1wAAAABJRU5ErkJggg==\n', 'text/plain': '<Figure size 432x288 with 1 Axes>'}, 'metadata': {'filenames': {'image/png': img_locs[0]}}, 'output_type': 'display_data'}]
        },
        {'outputs':
            [{'data': {'image/png': 'iVBOJggg==\n', 'text/plain': '<Figure size 432x288 with 1 Axes>'}, 'metadata': {'filenames': {'image/png': img_locs[1]}}, 'output_type': 'display_data'}]
        }]

    for index, cell in enumerate(cells):
        cell = from_dict(cell)
        output_node = cell_output_to_nodes(cell["outputs"], priority, True, output_dir, None)
        assert output_node[0].attributes['uri'] == img_locs[index] 
Example #3
Source File: extension.py    From nbcelltests with Apache License 2.0 5 votes vote down vote up
def _run(self, body, path, name):
        with TemporaryDirectory() as tempdir:
            path = os.path.abspath(os.path.join(tempdir, name))
            node = nbformat.from_dict(body.get('model'))
            nbformat.write(node, path)
            ret = runTest(path, executable=self.executable, rules=self.rules)
            return ret 
Example #4
Source File: extension.py    From nbcelltests with Apache License 2.0 5 votes vote down vote up
def _run(self, body, path, name):
        with TemporaryDirectory() as tempdir:
            path = os.path.abspath(os.path.join(tempdir, name))
            node = nbformat.from_dict(body.get('model'))
            nbformat.write(node, path)
            ret, status = runLint(path, executable=self.executable, rules=self.rules)
            return ret, status
            self.finish({'status': status, 'lint': ret}) 
Example #5
Source File: testrunner.py    From wradlib with MIT License 5 votes vote down vote up
def _runTest(self):
        kernel = "python%d" % sys.version_info[0]
        cur_dir = os.path.dirname(self.nbfile)

        with open(self.nbfile) as f:
            nb = nbformat.read(f, as_version=4)
            if self.cov:
                covdict = {
                    "cell_type": "code",
                    "execution_count": 1,
                    "metadata": {"collapsed": True},
                    "outputs": [],
                    "nbsphinx": "hidden",
                    "source": "import coverage\n"
                    "coverage.process_startup()\n"
                    "import sys\n"
                    'sys.path.append("{0}")\n'.format(cur_dir),
                }
                nb["cells"].insert(0, nbformat.from_dict(covdict))

            exproc = ExecutePreprocessor(kernel_name=kernel, timeout=600)

            try:
                run_dir = os.getenv("WRADLIB_BUILD_DIR", cur_dir)
                exproc.preprocess(nb, {"metadata": {"path": run_dir}})
            except CellExecutionError as e:
                raise e

        if self.cov:
            nb["cells"].pop(0)

        with io.open(self.nbfile, "wt") as f:
            nbformat.write(nb, f)

        self.assertTrue(True) 
Example #6
Source File: __init__.py    From jgscm with MIT License 4 votes vote down vote up
def save(self, model, path):
        if path.startswith("/"):
            path = path[1:]
        if "type" not in model:
            raise web.HTTPError(400, u"No file type provided")
        if "content" not in model and model["type"] != "directory":
            raise web.HTTPError(400, u"No file content provided")
        if "/" not in path and self.default_path:
            path = "%s/%s" % (self.default_path, path)
        bucket_name, bucket_path = self._parse_path(path)
        if bucket_path == "" and model["type"] != "directory":
            raise web.HTTPError(403, u"You may only create directories "
                                     u"(buckets) at the root level.")
        if bucket_path != "" and model["type"] == "directory" and \
                bucket_path[-1] != "/":
            path += "/"
        self.log.debug("Saving %s", path)

        self.run_pre_save_hook(model=model, path=path)

        try:
            if model["type"] == "notebook":
                nb = nbformat.from_dict(model["content"])
                self.check_and_sign(nb, path)
                self._save_notebook(path, nb)
                # One checkpoint should always exist for notebooks.
                if not self.checkpoints.list_checkpoints(path):
                    self.create_checkpoint(path)
            elif model["type"] == "file":
                # Missing format will be handled internally by _save_file.
                self._save_file(path, model["content"], model.get("format"))
            elif model["type"] == "directory":
                self._save_directory(path, model)
            else:
                raise web.HTTPError(
                    00, u"Unhandled contents type: %s" % model["type"])
        except web.HTTPError:
            raise
        except Exception as e:
            self.log.error(u"Error while saving file: %s %s", path, e,
                           exc_info=True)
            raise web.HTTPError(
                500, u"Unexpected error while saving file: %s %s" % (path, e))

        validation_message = None
        if model["type"] == "notebook":
            self.validate_notebook_model(model)
            validation_message = model.get("message", None)

        model = self.get(path, content=False)
        if validation_message:
            model["message"] = validation_message

        self.run_post_save_hook(model=model, os_path=path)

        return model 
Example #7
Source File: fsmanager.py    From jupyter-fs with Apache License 2.0 4 votes vote down vote up
def save(self, model, path=''):
        """Save the file model and return the model with no content."""
        path = path.strip('/')

        if 'type' not in model:
            raise web.HTTPError(400, u'No file type provided')
        if 'content' not in model and model['type'] != 'directory':
            raise web.HTTPError(400, u'No file content provided')

        self.log.debug("Saving %s", path)
        self.run_pre_save_hook(model=model, path=path)

        try:
            if model['type'] == 'notebook':
                nb = nbformat.from_dict(model['content'])
                self.check_and_sign(nb, path)
                self._save_notebook(path, nb)
                # TODO: decide how to handle checkpoints for non-local fs.
                # For now, checkpoint pathing seems to be borked.
                # One checkpoint should always exist for notebooks.
                # if not self.checkpoints.list_checkpoints(path):
                #     self.create_checkpoint(path)
            elif model['type'] == 'file':
                # Missing format will be handled internally by _save_file.
                self._save_file(path, model['content'], model.get('format'))
            elif model['type'] == 'directory':
                self._save_directory(path, model)
            else:
                raise web.HTTPError(400, "Unhandled contents type: %s" % model['type'])
        except web.HTTPError:
            raise
        except Exception as e:
            self.log.error(u'Error while saving file: %s %s', path, e, exc_info=True)
            raise web.HTTPError(500, u'Unexpected error while saving file: %s %s' % (path, e))

        validation_message = None
        if model['type'] == 'notebook':
            self.validate_notebook_model(model)
            validation_message = model.get('message', None)

        model = self.get(path, content=False)
        if validation_message:
            model['message'] = validation_message

        self.run_post_save_hook(model=model, os_path=path)

        return model 
Example #8
Source File: hdfsmanager.py    From hdfscontents with Apache License 2.0 4 votes vote down vote up
def save(self, model, path=''):
            """
                    Save a file or directory model to path.
                    Should return the saved model with no content.  Save implementations
                    should call self.run_pre_save_hook(model=model, path=path) prior to
                    writing any data.
                    """
            path = path.strip('/')

            if 'type' not in model:
                raise web.HTTPError(400, u'No file type provided')
            if 'content' not in model and model['type'] != 'directory':
                raise web.HTTPError(400, u'No file content provided')

            path = path.strip('/')
            hdfs_path = to_os_path(path, self.root_dir)
            self.log.debug("Saving %s", hdfs_path)

            self.run_pre_save_hook(model=model, path=path)

            try:
                if model['type'] == 'notebook':
                    nb = nbformat.from_dict(model['content'])
                    self.check_and_sign(nb, path)
                    self._save_notebook(hdfs_path, nb)
                    # One checkpoint should always exist for notebooks.
                    if not self.checkpoints.list_checkpoints(path):
                        self.create_checkpoint(path)
                elif model['type'] == 'file':
                    # Missing format will be handled internally by _save_file.
                    self._save_file(hdfs_path, model['content'], model.get('format'))
                elif model['type'] == 'directory':
                    self._save_directory(hdfs_path, model, path)
                else:
                    raise web.HTTPError(400, "Unhandled hdfscontents type: %s" % model['type'])
            except web.HTTPError:
                raise
            except Exception as e:
                self.log.error(u'Error while saving file: %s %s', path, e, exc_info=True)
                raise web.HTTPError(500, u'Unexpected error while saving file: %s %s' % (path, e))

            validation_message = None
            if model['type'] == 'notebook':
                self.validate_notebook_model(model)
                validation_message = model.get('message', None)

            model = self.get(path, content=False)
            if validation_message:
                model['message'] = validation_message

            #self.run_post_save_hook(model=model, os_path=hdfs_path)

            return model 
Example #9
Source File: manager.py    From podoc with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def save(self, model, path=''):
        """Save the file model and return the model with no content."""
        path = path.strip('/')

        if 'type' not in model:  # pragma: no cover
            raise web.HTTPError(400, u'No file type provided')
        if ('content' not in model and
                model['type'] != 'directory'):  # pragma: no cover
            raise web.HTTPError(400, u'No file content provided')

        self.run_pre_save_hook(model=model, path=path)

        os_path = self._get_os_path(path)
        self.log.debug("Saving %s", os_path)
        try:
            if model['type'] == 'notebook':

                file_ext = _file_extension(os_path)
                nb = nbformat.from_dict(model['content'])
                if file_ext == '.ipynb':
                    self.check_and_sign(nb, path)
                    self._save_notebook(os_path, nb)
                else:
                    p = self._podoc
                    lang = p.get_lang_for_file_ext(file_ext)
                    p.convert_text(nb,
                                   source='notebook',
                                   target=lang,
                                   output=os_path,
                                   )

                # One checkpoint should always exist for notebooks.
                if not self.checkpoints.list_checkpoints(path):
                    self.create_checkpoint(path)
            elif model['type'] == 'file':
                # Missing format will be handled internally by _save_file.
                self._save_file(os_path, model['content'], model.get('format'))
            elif model['type'] == 'directory':
                self._save_directory(os_path, model, path)
            else:  # pragma: no cover
                raise web.HTTPError(400, "Unhandled contents type: %s" % model['type'])  # noqa
        except web.HTTPError:  # pragma: no cover
            raise
        except Exception as e:  # pragma: no cover
            self.log.error(u'Error while saving file: %s %s', path, e, exc_info=True)  # noqa
            raise web.HTTPError(500, u'Unexpected error while saving file: %s %s' % (path, e))  # noqa

        validation_message = None
        if model['type'] == 'notebook':
            self.validate_notebook_model(model)
            validation_message = model.get('message', None)

        model = self.get(path, content=False)
        if validation_message:  # pragma: no cover
            model['message'] = validation_message

        self.run_post_save_hook(model=model, os_path=os_path)

        return model