• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python nbformat.from_dict函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中nbformat.from_dict函数的典型用法代码示例。如果您正苦于以下问题:Python from_dict函数的具体用法?Python from_dict怎么用?Python from_dict使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了from_dict函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_save

    def test_save(self):
        resp = self.api.read('foo/a.ipynb')
        nbcontent = json.loads(resp.text)['content']
        nb = from_dict(nbcontent)
        nb.cells.append(new_markdown_cell(u'Created by test ³'))

        nbmodel = {'content': nb, 'type': 'notebook'}
        resp = self.api.save('foo/a.ipynb', body=json.dumps(nbmodel))

        nbcontent = self.api.read('foo/a.ipynb').json()['content']
        newnb = from_dict(nbcontent)
        self.assertEqual(newnb.cells[0].source,
                         u'Created by test ³')
开发者ID:drastorguev,项目名称:pythondojo,代码行数:13,代码来源:test_contents_api.py


示例2: _runTest

    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)
开发者ID:heistermann,项目名称:wradlib,代码行数:32,代码来源:testrunner.py


示例3: save

    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(nb,
                              source='notebook',
                              target=lang,
                              resources=None,  # TODO
                              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
开发者ID:willingc,项目名称:podoc,代码行数:60,代码来源:manager.py


示例4: post

    def post(self, format):
        exporter = get_exporter(format, config=self.config)

        model = self.get_json_body()
        name = model.get("name", "notebook.ipynb")
        nbnode = from_dict(model["content"])

        try:
            output, resources = exporter.from_notebook_node(
                nbnode,
                resources={
                    "metadata": {"name": name[: name.rfind(".")]},
                    "config_dir": self.application.settings["config_dir"],
                },
            )
        except Exception as e:
            raise web.HTTPError(500, "nbconvert failed: %s" % e)

        if respond_zip(self, name, output, resources):
            return

        # MIME type
        if exporter.output_mimetype:
            self.set_header("Content-Type", "%s; charset=utf-8" % exporter.output_mimetype)

        self.finish(output)
开发者ID:Fitz3012,项目名称:notebook,代码行数:26,代码来源:handlers.py


示例5: sanitize_outputs

    def sanitize_outputs(self, outputs, skip_sanitize=('metadata',
                                                       'traceback',
                                                       'text/latex',
                                                       'prompt_number',
                                                       'output_type',
                                                       'name',
                                                       'execution_count'
                                                       )):
        sanitized_outputs = []
        for output in outputs:
            sanitized = {}
            for key in output.keys():
                if key in skip_sanitize:
                    sanitized[key] = output[key]
                else:
                    if key == 'data':
                        sanitized[key] = {}
                        for data_key in output[key].keys():
                            # Filter the keys in the SUB-dictionary again
                            if data_key in skip_sanitize:
                                sanitized[key][data_key] = output[key][data_key]
                            else:
                                sanitized[key][data_key] = self.sanitize(output[key][data_key])

                    # Otherwise, just create a normal dictionary entry from
                    # one of the keys of the dictionary
                    else:
                        # Create the dictionary entries on the fly, from the
                        # existing ones to be compared
                        sanitized[key] = self.sanitize(output[key])
            sanitized_outputs.append(nbformat.from_dict(sanitized))
        return sanitized_outputs
开发者ID:SiddharthTiwari,项目名称:fastai,代码行数:32,代码来源:plugin.py


示例6: write_notebook

    def write_notebook(self, include_html=True):
        suffix = "_responses_with_names" if self.include_usernames else "_responses"
        nb_name = self.nb_name_stem + suffix
        output_file = os.path.join(PROCESSED_NOTEBOOK_DIR, nb_name + '.ipynb')
        html_output = os.path.join(PROCESSED_NOTEBOOK_DIR, nb_name + '.html')

        remove_duplicate_answers = not self.include_usernames

        filtered_cells = []
        for prompt in self.question_prompts:
            filtered_cells += prompt.cells
            answers = prompt.answers_without_duplicates if remove_duplicate_answers else prompt.answers
            for gh_username, response_cells in answers.items():
                if self.include_usernames:
                    filtered_cells.append(
                        NotebookUtils.markdown_heading_cell(self.gh_username_to_fullname(gh_username), 4))
                filtered_cells.extend(response_cells)

        answer_book = deepcopy(self.template)
        answer_book['cells'] = filtered_cells
        nb = nbformat.from_dict(answer_book)

        print "Writing", output_file
        with io.open(output_file, 'wt') as fp:
            nbformat.write(nb, fp, version=4)

        if include_html:
            # TODO why is the following necessary?
            nb = nbformat.reads(nbformat.writes(nb, version=4), as_version=4)
            html_content, _ = nbconvert.export_html(nb)
            print "Writing", html_output
            with io.open(html_output, 'w') as fp:
                fp.write(html_content)
开发者ID:paulruvolo,项目名称:SoftDesSp16Prep,代码行数:33,代码来源:extract_answers_template.py


示例7: main

def main():
    arguments = docopt(__doc__, version='nbgen 2.0')

    cmd = subprocess.run([arguments["<path>"]] + arguments["<arguments>"], stdout=subprocess.PIPE)
    cmd.check_returncode()
    cells = json.loads(cmd.stdout.decode("utf-8"))

    nb_dict = {
      "metadata": {},
      "nbformat": 4,
      "nbformat_minor": 0,
      "cells": cells,
    }
    notebook = nbformat.from_dict(nb_dict)

    ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
    ep.preprocess(notebook, {'metadata': {}})

    if arguments["nb"]:
        nbformat.write(notebook, "{}.ipynb".format(arguments["<name>"]))

    elif arguments["slides"]:
        config = Config()
        reveal_cdn = "https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.3.0/"
        config.SlidesExporter.reveal_url_prefix = (arguments["--reveal"] or reveal_cdn)

        slides, __ = export_slides(nb=notebook, config=config)
        with open("{}.html".format(arguments["<name>"]), "w") as html_file:
            html_file.write(slides)
开发者ID:ewjoachim,项目名称:nbgen,代码行数:29,代码来源:__init__.py


示例8: setUp

    def setUp(self):
        if self.metadata is None:
            metadata = self.default_metadata
        else:
            metadata = self.metadata
        self.orig = nbformat.from_dict({
            "nbformat": NBFORMAT_VERSION,
            "nbformat_minor": 0,
            "metadata": metadata,
            "cells": self.cells
        })

        with tempfile.TemporaryDirectory() as d:
            ipynb0_name = d + "/0"
            rmd_name = d + "/1"
            ipynb1_name = d + "/2"

            with open(ipynb0_name, "w") as f:
                nbformat.write(self.orig, f)

            if self.use_rmd:
                ipyrmd.ipynb_to_rmd(ipynb0_name, rmd_name)
                ipyrmd.rmd_to_ipynb(rmd_name, ipynb1_name)
            else:
                ipyrmd.ipynb_to_spin(ipynb0_name, rmd_name)
                ipyrmd.spin_to_ipynb(rmd_name, ipynb1_name)

            with open(rmd_name) as f:
                self.rmd = f.read()

            with open(ipynb1_name) as f:
                self.roundtrip = nbformat.read(f, NBFORMAT_VERSION)
开发者ID:akzaidi,项目名称:ipyrmd,代码行数:32,代码来源:__init__.py


示例9: test_checkpoints_follow_file

    def test_checkpoints_follow_file(self):

        # Read initial file state
        orig = self.api.read('foo/a.ipynb')

        # Create a checkpoint of initial state
        r = self.api.new_checkpoint('foo/a.ipynb')
        cp1 = r.json()

        # Modify file and save
        nbcontent = json.loads(orig.text)['content']
        nb = from_dict(nbcontent)
        hcell = new_markdown_cell('Created by test')
        nb.cells.append(hcell)
        nbmodel = {'content': nb, 'type': 'notebook'}
        self.api.save('foo/a.ipynb', body=json.dumps(nbmodel))

        # Rename the file.
        self.api.rename('foo/a.ipynb', 'foo/z.ipynb')

        # Looking for checkpoints in the old location should yield no results.
        self.assertEqual(self.api.get_checkpoints('foo/a.ipynb').json(), [])

        # Looking for checkpoints in the new location should work.
        cps = self.api.get_checkpoints('foo/z.ipynb').json()
        self.assertEqual(cps, [cp1])

        # Delete the file.  The checkpoint should be deleted as well.
        self.api.delete('foo/z.ipynb')
        cps = self.api.get_checkpoints('foo/z.ipynb').json()
        self.assertEqual(cps, [])
开发者ID:drastorguev,项目名称:pythondojo,代码行数:31,代码来源:test_contents_api.py


示例10: add_codecell

 def add_codecell(self, code):
     self.nb.cells.append(nbformat.from_dict({
         "cell_type": "code",
         "execution_count": None,
         "metadata": {},
         "source": code.strip(),
         "outputs": []
     }))
开发者ID:Yukee,项目名称:pybinding,代码行数:8,代码来源:nbexport.py


示例11: merge_notebooks

def merge_notebooks(base, local, remote):
    """Merge changes introduced by notebooks local and remote from a shared ancestor base.

    Return new (partially) merged notebook and unapplied diffs from the local and remote side.
    """
    merged, local_conflict_diffs, remote_conflict_diffs = merge(base, local, remote)
    merged = nbformat.from_dict(merged)
    return merged, local_conflict_diffs, remote_conflict_diffs
开发者ID:ijstokes,项目名称:nbdime,代码行数:8,代码来源:notebooks.py


示例12: save

    def save(self, model, path=''):
        """Save the file model and return the model with no content."""

        import json

        path = path.strip('/')

        with self.library_context as l:
            b, f = self._file_from_path(l, path)

            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.run_pre_save_hook(model=model, path=f.record.id)

            if not f.record.size:
                f.record.update_contents(f.default, 'application/json')
            else:
                f.record.update_contents(json.dumps(model['content']), 'application/json')

            try:
                if model['type'] == 'notebook':

                    nb = nbformat.from_dict(model['content'])
                    self.check_and_sign(nb, path)

                    # One checkpoint should always exist for notebooks.

                    if not self.checkpoints.list_checkpoints(path):
                        self.create_checkpoint(path)

                elif model['type'] == 'file':
                    pass
                elif model['type'] == 'directory':
                    pass
                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



        return model
开发者ID:CivicKnowledge,项目名称:ambry-ui,代码行数:58,代码来源:jupyter.py


示例13: post

    def post(self, *args, **kwargs):
        # Given a list of nb cells, save to ipynb format v4
        filepath = self.get_argument('filepath')
        if filepath[-6:] != '.ipynb':
            filepath = '{}.ipynb'.format(filepath)
        cells = json.loads(self.request.body)['cells']

        nb_cells = []
        for cell in cells:
            cinput = cell.get('input', '')
            coutput = cell.get('output', '')
            ctype = cell.get('type')
            tmp_cell = {
                "cell_type": ctype,
                "metadata": {
                    "collapsed" : False, # whether the output of the cell is collapsed
                    "autoscroll": "auto", # any of true, false or "auto"
                },
                "source": cinput,
            }
            if ctype=='code':
                tmp_cell.update({
                    "execution_count": None,
                    "outputs": [{
                        "output_type" : "stream",
                        "name" : "stdout",
                        "text" : coutput,
                    }]
                })
            nb_cells.append(tmp_cell)

        base_nb = {
            'metadata': {
                'kernelspec': {
                    'name': 'bash',
                    "display_name": "Bash",
                    "language": "bash"
                },
                "language_info": {
                    "codemirror_mode": "shell",
                    "file_extension": ".sh",
                    "mimetype": "text/x-sh",
                    "name": "bash"
                }
            },
            'nbformat': 4,
            'nbformat_minor': 0,
            'cells': nb_cells
        }

        try:
            nbformat.validate(base_nb,version=4)
            nb = nbformat.from_dict(base_nb)
            nbformat.write(nb,filepath)
            self.write({'res':'File saved to {}'.format(filepath)})
        except nbformat.ValidationError:
            self.set_status(400)
            return
开发者ID:SandstoneHPC,项目名称:sandstone-nb-term,代码行数:58,代码来源:handlers.py


示例14: _save_notebook

 def _save_notebook(self, model, key):
     nbcontents = from_dict(model['content'])
     self.check_and_sign(nbcontents, key)
     
     content = self._nb_encode_b64(nbcontents)
     s3file = S3File(key, None, 'application/octet-stream')
     self.write_content(content, s3file)
     self.validate_notebook_model(model)
     return model.get('message')
开发者ID:petrosrizos,项目名称:s3drive,代码行数:9,代码来源:s3drive.py


示例15: write_markdown

    def write_markdown(self, text):
        if self.nb.cells[-1].cell_type != "markdown":
            self.nb.cells.append(nbformat.from_dict({
                "cell_type": "markdown",
                "metadata": {},
                "source": []
            }))

        self.nb.cells[-1].source.append(
            text.replace("\n", "\n" + " " * self.indent)
        )
开发者ID:Yukee,项目名称:pybinding,代码行数:11,代码来源:nbexport.py


示例16: _save_notebook

      def _save_notebook(self,  model, path):
		"""
		Save a notebook.
		Returns a validation message.
		"""
		with fileManager.session_scope() as session:
			nb_contents = nbformat.from_dict(model['content'])
			self.check_and_sign(nb_contents, path)
			self.fm.save_file(path, writes_base64(nb_contents),session)
			# It's awkward that this writes to the model instead of returning.
			self.validate_notebook_model(model)
			return model.get('message')
开发者ID:albanatita,项目名称:GilgameshServer,代码行数:12,代码来源:SharedContentsManager.py


示例17: compute_imports

 def compute_imports(self):
     if self.lookup is None:
         notebook = nbformat.from_dict(self.contents['notebook'])
         code = ""
         for cell in notebook.cells:
             if cell.cell_type == "code":
                 code += "\n" + cell.source                
         self.lookup = ImportsLookup()
         self.lookup.visit(ast.parse(self.ast_parse(code)))
         self.contents['notebook']['metadata']['pixiedust'].update({
             "imports": {p[0]:{"version":p[1],"install":p[2]} for p in self.lookup.imports}
         })
开发者ID:ibm-cds-labs,项目名称:pixiedust,代码行数:12,代码来源:publish.py


示例18: test_checkpoints

    def test_checkpoints(self):
        resp = self.api.read('foo/a.ipynb')
        r = self.api.new_checkpoint('foo/a.ipynb')
        self.assertEqual(r.status_code, 201)
        cp1 = r.json()
        self.assertEqual(set(cp1), {'id', 'last_modified'})
        self.assertEqual(r.headers['Location'].split('/')[-1], cp1['id'])

        # Modify it
        nbcontent = json.loads(resp.text)['content']
        nb = from_dict(nbcontent)
        hcell = new_markdown_cell('Created by test')
        nb.cells.append(hcell)
        # Save
        nbmodel= {'content': nb, 'type': 'notebook'}
        resp = self.api.save('foo/a.ipynb', body=json.dumps(nbmodel))

        # List checkpoints
        cps = self.api.get_checkpoints('foo/a.ipynb').json()
        self.assertEqual(cps, [cp1])

        nbcontent = self.api.read('foo/a.ipynb').json()['content']
        nb = from_dict(nbcontent)
        self.assertEqual(nb.cells[0].source, 'Created by test')

        # Restore cp1
        r = self.api.restore_checkpoint('foo/a.ipynb', cp1['id'])
        self.assertEqual(r.status_code, 204)
        nbcontent = self.api.read('foo/a.ipynb').json()['content']
        nb = from_dict(nbcontent)
        self.assertEqual(nb.cells, [])

        # Delete cp1
        r = self.api.delete_checkpoint('foo/a.ipynb', cp1['id'])
        self.assertEqual(r.status_code, 204)
        cps = self.api.get_checkpoints('foo/a.ipynb').json()
        self.assertEqual(cps, [])
开发者ID:drastorguev,项目名称:pythondojo,代码行数:37,代码来源:test_contents_api.py


示例19: save_notebook

    def save_notebook(self, model, name='', path=''):
        """Save the notebook model and return the model with no content."""
        path = path.strip('/')

        if 'content' not in model:
            raise web.HTTPError(400, u'No notebook JSON data provided')

        if not path:
            raise web.HTTPError(400, u'We require path for saving.')

        nb = nbformat.from_dict(model['content'])

        gist = self._get_gist(name, path)
        if gist is None:
            tags = parse_tags(name)
            if path:
                tags.append(path)
            content = nbformat.writes(nb, version=nbformat.NO_CONVERT)
            gist = self.gisthub.create_gist(name, tags, content)

        # One checkpoint should always exist
        #if self.notebook_exists(name, path) and not self.list_checkpoints(name, path):
        #    self.create_checkpoint(name, path)

        new_path = model.get('path', path).strip('/')
        new_name = model.get('name', name)

        if path != new_path:
            raise web.HTTPError(400, u'Gist backend does not support path change')

        # remove [gist_id] if we're being sent old key_name
        gist.name = gist.strip_gist_id(new_name)
        gist.notebook_content = nb

        self.check_and_sign(nb, self.fullpath(new_path, new_name))

        if 'name' in nb['metadata']:
            nb['metadata']['name'] = u''
        try:
            self.log.debug("Autosaving notebook %s %s", path, name)
            self.gisthub.save(gist)
        except Exception as e:
            raise web.HTTPError(400, u'Unexpected error while autosaving notebook: %s %s %s' % (path, name, e))

        # NOTE: since gist.name might not have [gist_id] suffix on rename
        # we use gist.key_name
        model = self.get_notebook(gist.key_name, new_path, content=False)
        return model
开发者ID:dalejung,项目名称:nbx,代码行数:48,代码来源:gistnbmanager.py


示例20: save

    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')

        os_path = self._get_os_path(path)
        self.log.debug("Saving %s", os_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(os_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(os_path, model['content'], model.get('format'))
            elif model['type'] == 'directory':
                self._save_directory(os_path, model, path)
            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=os_path)

        return model
开发者ID:willingc,项目名称:notebook,代码行数:47,代码来源:filemanager.py



注:本文中的nbformat.from_dict函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python nbformat.read函数代码示例发布时间:2022-05-27
下一篇:
Python nbdime.merge_notebooks函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap