本文整理汇总了Python中nbformat.from_dict方法的典型用法代码示例。如果您正苦于以下问题:Python nbformat.from_dict方法的具体用法?Python nbformat.from_dict怎么用?Python nbformat.from_dict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nbformat
的用法示例。
在下文中一共展示了nbformat.from_dict方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_image_mimetype_uri
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import from_dict [as 别名]
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]
示例2: export_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import from_dict [as 别名]
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)
示例3: _run
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import from_dict [as 别名]
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
示例4: _runTest
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import from_dict [as 别名]
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)
示例5: save
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import from_dict [as 别名]
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
示例6: save
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import from_dict [as 别名]
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
示例7: save
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import from_dict [as 别名]
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
示例8: save
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import from_dict [as 别名]
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