本文整理汇总了Python中nbformat.writes方法的典型用法代码示例。如果您正苦于以下问题:Python nbformat.writes方法的具体用法?Python nbformat.writes怎么用?Python nbformat.writes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nbformat
的用法示例。
在下文中一共展示了nbformat.writes方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: merge_notebooks
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def merge_notebooks(filenames):
merged = None
for fname in filenames:
with io.open(fname, 'r', encoding='utf-8') as f:
nb = nbformat.read(f, as_version=4)
if merged is None:
merged = nb
else:
# TODO: add an optional marker between joined notebooks
# like an horizontal rule, for example, or some other arbitrary
# (user specified) markdown cell)
merged.cells.extend(nb.cells)
if not hasattr(merged.metadata, 'name'):
merged.metadata.name = ''
merged.metadata.name += "_merged"
print(nbformat.writes(merged))
示例2: update_ipynb_toc
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def update_ipynb_toc(root):
"""Change the toc code block into a list of clickable links"""
notebooks = find_files('**/*.ipynb', root)
for fn in notebooks:
nb = notebook.read(fn)
if not nb:
continue
for cell in nb.cells:
if (cell.cell_type == 'markdown' and '```toc' in cell.source):
md_cells = markdown.split_markdown(cell.source)
for c in md_cells:
if c['type'] == 'code' and c['class'] == 'toc':
toc = []
for l in c['source'].split('\n'):
if l and not l.startswith(':'):
toc.append(' - [%s](%s.ipynb)'%(l,l))
c['source'] = '\n'.join(toc)
c['type'] = 'markdown'
cell.source = markdown.join_markdown_cells(md_cells)
with open(fn, 'w') as f:
f.write(nbformat.writes(nb))
示例3: process_and_eval_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def process_and_eval_notebook(input_fn, output_fn, run_cells, timeout=20*60,
lang='python', tab=None, default_tab=None):
with open(input_fn, 'r') as f:
md = f.read()
nb = notebook.read_markdown(md)
if tab:
# get the tab
nb = notebook.split_markdown_cell(nb)
nb = notebook.get_tab_notebook(nb, tab, default_tab)
if not nb:
logging.info(f"Skip to eval tab {tab} for {input_fn}")
# write an emtpy file to track the dependencies
open(output_fn, 'w')
return
# evaluate
if run_cells:
# change to the notebook directory to resolve the relpaths properly
cwd = os.getcwd()
os.chdir(os.path.join(cwd, os.path.dirname(output_fn)))
notedown.run(nb, timeout)
os.chdir(cwd)
# write
nb['metadata'].update({'language_info':{'name':lang}})
with open(output_fn, 'w') as f:
f.write(nbformat.writes(nb))
示例4: sample_perf
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def sample_perf(nb, n=30):
samples = pd.DataFrame(
pd.np.NaN,
index=pd.MultiIndex.from_product(
(range(n), ['nbformat'] + JUPYTEXT_FORMATS), names=['sample', 'implementation']),
columns=pd.Index(['size', 'read', 'write'], name='measure'))
for i, fmt in samples.index:
t0 = time.time()
if fmt == 'nbformat':
text = nbformat.writes(nb)
else:
text = jupytext.writes(nb, fmt)
t1 = time.time()
samples.loc[(i, fmt), 'write'] = t1 - t0
samples.loc[(i, fmt), 'size'] = len(text)
t0 = time.time()
if fmt == 'nbformat':
nbformat.reads(text, as_version=4)
else:
jupytext.reads(text, fmt)
t1 = time.time()
samples.loc[(i, fmt), 'read'] = t1 - t0
return samples
示例5: notebook_to_md
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def notebook_to_md(notebook):
"""Convert a notebook to its Markdown representation, using Pandoc"""
tmp_file = tempfile.NamedTemporaryFile(delete=False)
tmp_file.write(ipynb_writes(notebook).encode("utf-8"))
tmp_file.close()
pandoc(
u"--from ipynb --to markdown -s --atx-headers --wrap=preserve --preserve-tabs",
tmp_file.name,
tmp_file.name,
)
with open(tmp_file.name, encoding="utf-8") as opened_file:
text = opened_file.read()
os.unlink(tmp_file.name)
return "\n".join(text.splitlines())
示例6: _save_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def _save_notebook(self, path, nb):
"""
Uploads notebook to GCS.
:param path: blob path.
:param nb: :class:`nbformat.notebooknode.NotebookNode` instance.
:return: created :class:`google.cloud.storage.Blob`.
"""
bucket_name, bucket_path = self._parse_path(path)
bucket = self._get_bucket(bucket_name, throw=True)
data = nbformat.writes(nb, version=nbformat.NO_CONVERT)
blob = bucket.blob(bucket_path)
blob.upload_from_string(data, "application/x-ipynb+json")
return blob
示例7: test_jupyter_nbconvert_cli
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def test_jupyter_nbconvert_cli(tmpdir, download_notebook):
ipynb_filename = os.path.join(tmpdir, 'download_notebook.ipynb')
with open(ipynb_filename, 'w', encoding='utf8') as file:
file.write(nbformat.writes(download_notebook))
app = nbconvertapp.NbConvertApp()
app.initialize(argv=[ipynb_filename, '--to', 'jupyter_docx_bundler.DocxExporter'])
app.convert_notebooks()
示例8: _save_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def _save_notebook(self, path, nb):
"""Save a notebook to an os_path."""
s = nbformat.writes(nb, version=nbformat.NO_CONVERT)
self._pyfilesystem_instance.writetext(path, s)
示例9: write_notebook_output
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def write_notebook_output(notebook, output_dir, notebook_name, location=None):
"""Extract output from notebook cells and write to files in output_dir.
This also modifies 'notebook' in-place, adding metadata to each cell that
maps output mime-types to the filenames the output was saved under.
"""
resources = dict(unique_key=os.path.join(output_dir, notebook_name), outputs={})
# Modifies 'resources' in-place
ExtractOutputPreprocessor().preprocess(notebook, resources)
# Write the cell outputs to files where we can (images and PDFs),
# as well as the notebook file.
FilesWriter(build_directory=output_dir).write(
nbformat.writes(notebook),
resources,
os.path.join(output_dir, notebook_name + ".ipynb"),
)
# Write a script too. Note that utf-8 is the de facto
# standard encoding for notebooks.
ext = notebook.metadata.get("language_info", {}).get("file_extension", None)
if ext is None:
ext = ".txt"
js.logger.warning(
"Notebook code has no file extension metadata, " "defaulting to `.txt`",
location=location,
)
contents = "\n\n".join(cell.source for cell in notebook.cells)
with open(os.path.join(output_dir, notebook_name + ext), "w",
encoding = "utf8") as f:
f.write(contents)
示例10: get_notebook_scaffolding
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def get_notebook_scaffolding(kernelspec):
check.dict_param(kernelspec, 'kernelspec', key_type=str, value_type=str)
notebook = nbformat.v4.new_notebook()
notebook.cells = [get_import_cell(), get_parameters_cell()]
metadata = {'celltoolbar': 'Tags', 'kernelspec': kernelspec}
notebook.metadata = metadata
return nbformat.writes(notebook)
示例11: path_to_intermediate
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def path_to_intermediate(path):
"""Name of the intermediate file used in atomic writes.
The .~ prefix will make Dropbox ignore the temporary file."""
dirname, basename = os.path.split(path)
return os.path.join(dirname, '.~' + basename)
示例12: atomic_writing
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def atomic_writing(self, hdfs_path):
"""wrapper around atomic_writing that turns permission errors to 403.
Depending on flag 'use_atomic_writing', the wrapper perform an actual atomic writing or
simply writes the file (whatever an old exists or not)"""
with self.perm_to_403(hdfs_path):
if self.use_atomic_writing:
with atomic_writing(self.hdfs, hdfs_path) as f:
yield f
else:
with _simple_writing(self.hdfs, hdfs_path) as f:
yield f
示例13: _save_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def _save_notebook(self, hdfs_path, nb):
"""Save a notebook to an os_path."""
# Convert the notebook to unicode string
notebook_json = nbformat.writes(nb, version=nbformat.NO_CONVERT)
with self.atomic_writing(hdfs_path) as f:
# Write the notebook on hdfs
f.write(notebook_json.encode('utf-8'))
示例14: dumps
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def dumps(self, nb):
return nbformat.writes(nb, _NBFORMAT_VERSION)
示例15: from_file
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import writes [as 别名]
def from_file(self, filename):
import nbformat
from nbconvert import MarkdownExporter
from jinja2 import DictLoader
from traitlets.config import Config
c = Config()
# c.ExtractOutputPreprocessor.extract_output_types = set()
c.ExtractOutputPreprocessor.output_filename_template = 'images/{unique_key}_{cell_index}_{index}{extension}'
c.NbConvertBase.display_data_priority = ['application/javascript', 'text/html', 'text/markdown',
'image/svg+xml', 'text/latex', 'image/png', 'image/jpeg',
'text/plain']
nb = nbformat.read(filename, as_version=4)
dl = DictLoader({'full.tpl': TEMPLATE})
md_exporter = MarkdownExporter(config=c, extra_loaders=[
dl], template_file='full.tpl')
(body, resources) = md_exporter.from_notebook_node(nb)
self.kp_write(body, images={name.split(
'images/')[1]: data for name, data in resources.get('outputs', {}).items()})
# Add cleaned ipynb file
for cell in nb['cells']:
if cell['cell_type'] == 'code':
cell['outputs'] = [] # remove output data
cell['execution_count'] = None # reset to not executed
self.kp.write_src(os.path.basename(filename), nbformat.writes(nb))