本文整理汇总了Python中nbformat.writes函数的典型用法代码示例。如果您正苦于以下问题:Python writes函数的具体用法?Python writes怎么用?Python writes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了writes函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: 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
示例2: from_file
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))
开发者ID:WangLaoK,项目名称:knowledge-repo,代码行数:29,代码来源:ipynb.py
示例3: fix_notebook
def fix_notebook(filename, grade_id, source):
with io.open(filename, "r", encoding="utf-8") as f:
nb = nbformat.read(f, as_version=4)
for i, cell in enumerate(nb.cells):
if "nbgrader" in cell["metadata"] and cell["metadata"]["nbgrader"]["grade_id"] == grade_id:
nb.cells[i]["source"] = source
return nbformat.writes(nb)
开发者ID:EdwardJKim,项目名称:jupyterhub-info490-scripts,代码行数:7,代码来源:nbfix.py
示例4: bundle
def bundle(handler, model):
"""Create a compressed tarball containing the notebook document.
Parameters
----------
handler : tornado.web.RequestHandler
Handler that serviced the bundle request
model : dict
Notebook model from the configured ContentManager
"""
notebook_filename = model['name']
notebook_content = nbformat.writes(model['content']).encode('utf-8')
notebook_name = os.path.splitext(notebook_filename)[0]
tar_filename = '{}.tar.gz'.format(notebook_name)
info = tarfile.TarInfo(notebook_filename)
info.size = len(notebook_content)
with io.BytesIO() as tar_buffer:
with tarfile.open(tar_filename, "w:gz", fileobj=tar_buffer) as tar:
tar.addfile(info, io.BytesIO(notebook_content))
handler.set_attachment_header(tar_filename)
handler.set_header('Content-Type', 'application/gzip')
# Return the buffer value as the response
handler.finish(tar_buffer.getvalue())
开发者ID:BarnetteME1,项目名称:DnD-stuff,代码行数:27,代码来源:tarball_bundler.py
示例5: notebook_node_to_string_list
def notebook_node_to_string_list(notebook_node):
"""
Writes a NotebookNode to a list of strings.
:param notebook_node: The notebook as NotebookNode to write.
:return: The notebook as list of strings (linewise).
"""
return nbformat.writes(notebook_node, nbformat.NO_CONVERT).splitlines(True)
开发者ID:AbdealiJK,项目名称:coala-bears,代码行数:8,代码来源:PEP8NotebookBear.py
示例6: from_notebook_node
def from_notebook_node(self, nb, resources=None, **kw):
nb_copy, resources = super(NotebookExporter, self).from_notebook_node(nb, resources, **kw)
if self.nbformat_version != nb_copy.nbformat:
resources['output_suffix'] = '.v%i' % self.nbformat_version
else:
resources['output_suffix'] = '.nbconvert'
output = nbformat.writes(nb_copy, version=self.nbformat_version)
return output, resources
开发者ID:CaptainAL,项目名称:Spyder,代码行数:8,代码来源:notebook.py
示例7: convert_md
def convert_md():
"""Find all markdown files, convert into jupyter notebooks
"""
converted_files = []
reader = notedown.MarkdownReader(match='strict')
files = glob.glob('*/*.md')
# evaluate the newest file first, so we can catchup error ealier
files.sort(key=os.path.getmtime, reverse=True)
do_eval = int(os.environ.get('DO_EVAL', True))
if do_eval:
do_eval = int(os.environ.get('EVAL', True))
if not do_eval:
print('=== Will skip evaluating notebooks')
for fname in files:
new_fname = _get_new_fname(fname)
# parse if each markdown file is actually a jupyter notebook
with open(fname, 'r') as fp:
valid = '```{.python .input' in fp.read()
if not valid:
if new_fname != fname:
print('=== Rename %s -> %s' % (fname, new_fname))
shutil.copyfile(fname, new_fname)
converted_files.append((fname, new_fname))
continue
# read
with open(fname, 'r') as f:
notebook = reader.read(f)
if do_eval and not (_has_output(notebook) or
any([i in fname for i in ignore_execution])):
print('=== Evaluate %s with timeout %d sec'%(fname, timeout))
tic = time.time()
# update from ../data to data
for c in notebook.cells:
if c.get('cell_type', None) == 'code':
c['source'] = c['source'].replace(
'"../data', '"data').replace("'../data", "'data")
notedown.run(notebook, timeout)
print('=== Finished in %f sec'%(time.time()-tic))
# even that we will check it later, but do it ealier so we can see the
# error message before evaluating all notebooks
_check_notebook(notebook)
# write
# need to add language info to for syntax highlight
notebook['metadata'].update({'language_info':{'name':'python'}})
new_fname = _replace_ext(new_fname, 'ipynb')
print('=== Convert %s -> %s' % (fname, new_fname))
with open(new_fname, 'w') as f:
f.write(nbformat.writes(notebook))
converted_files.append((fname, new_fname))
return converted_files
开发者ID:antiBoson,项目名称:gluon-tutorials-zh,代码行数:58,代码来源:sphinx_plugin.py
示例8: test_write_downgrade_2
def test_write_downgrade_2(self):
"""dowgrade a v3 notebook to v2"""
# Open a version 3 notebook.
with self.fopen(u'test3.ipynb', 'r') as f:
nb = read(f, as_version=3)
jsons = writes(nb, version=2)
nb2 = json.loads(jsons)
(major, minor) = get_version(nb2)
self.assertEqual(major, 2)
开发者ID:BarnetteME1,项目名称:DnD-stuff,代码行数:10,代码来源:test_api.py
示例9: save
def save(self, keep_alt=False):
if keep_alt:
# xxx store in alt filename
outfilename = "{}.alt.ipynb".format(self.name)
else:
outfilename = self.filename
# xxx don't specify output version for now
new_contents = nbformat.writes(self.notebook)
if replace_file_with_string(outfilename, new_contents):
print("{} saved into {}".format(self.name, outfilename))
开发者ID:parmentelat,项目名称:flotpython,代码行数:10,代码来源:nbcf.py
示例10: _save_notebook
def _save_notebook(self, os_path, nb):
"""Save a notebook to an os_path."""
with self.atomic_writing(os_path, encoding='utf-8') as f:
if ftdetect(os_path) == 'notebook':
nbformat.write(nb, f, version=nbformat.NO_CONVERT)
elif ftdetect(os_path) == 'markdown':
nbjson = nbformat.writes(nb, version=nbformat.NO_CONVERT)
markdown = convert(nbjson, informat='notebook',
outformat='markdown')
f.write(markdown)
开发者ID:izahn,项目名称:notedown,代码行数:10,代码来源:contentsmanager.py
示例11: _save_notebook
def _save_notebook(self, path, nb):
self.log.debug('_save_notebook: %s', locals())
k = boto.s3.key.Key(self.bucket)
k.key = self._path_to_s3_key(path)
try:
notebook_json = nbformat.writes(nb, version=nbformat.NO_CONVERT)
k.set_contents_from_string(notebook_json)
except Exception as e:
raise web.HTTPError(400, u"Unexpected Error Writing Notebook: %s %s" % (path, e))
开发者ID:vertica,项目名称:s3nb,代码行数:11,代码来源:jupyter4.py
示例12: notebook_content
def notebook_content(self, content):
if isinstance(content, compat.string_types):
self._notebook_content = content
return
try:
# maybe this is a notebook
content = nbformat.writes(content, version=nbformat.NO_CONVERT)
self._notebook_content = content
except:
raise
开发者ID:dalejung,项目名称:nbx,代码行数:11,代码来源:notebook_gisthub.py
示例13: write
def write(self, filepath, notebookNode, version=4):
"""
Write a notebook to Storage
:param filepath: The path to the notebook to write on the Storage
:param notebookNode: notebookNode object to write
:param version: Version of the notebook
:return boolean
"""
self.log.debug("Write the notebook '%s' to storage" % filepath)
content = nbformat.writes(notebookNode, version);
return self.do_write(filepath, content);
开发者ID:Valdimus,项目名称:nbshared,代码行数:12,代码来源:storage.py
示例14: translate
def translate(self):
visitor = NBTranslator(self.document, self.app, self.docpath)
self.document.walkabout(visitor)
nb = _finilize_markdown_cells(visitor.nb)
if self.app.config.nbexport_execute:
ep = ExecutePreprocessor(allow_errors=True)
try:
ep.preprocess(nb, {'metadata': {}})
except CellExecutionError as e:
self.app.warn(str(e))
self.output = nbformat.writes(nb)
开发者ID:Yukee,项目名称:pybinding,代码行数:13,代码来源:nbexport.py
示例15: test_run_nb
def test_run_nb(self):
"""Test %run notebook.ipynb"""
from nbformat import v4, writes
nb = v4.new_notebook(
cells=[
v4.new_markdown_cell("The Ultimate Question of Everything"),
v4.new_code_cell("answer=42")
]
)
src = writes(nb, version=4)
self.mktmp(src, ext='.ipynb')
_ip.magic("run %s" % self.fname)
nt.assert_equal(_ip.user_ns['answer'], 42)
开发者ID:marcosptf,项目名称:fedora,代码行数:15,代码来源:test_run.py
示例16: 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
示例17: merge_notebooks
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))
开发者ID:zwsong,项目名称:nnabla,代码行数:16,代码来源:nbmerge.py
示例18: fetch
def fetch(self, filepath, dest):
"""
Fetch a notebook from Storage to home directory
:param filepath: Path to the notebook on the storage
:param dest: Path to the notebook fetched in the home directory of the user
:return the path to the notebook in the home directory of the user
"""
self.log.debug("Fetch notebook '%s' to '%s'" % (filepath, dest));
nb = self.read(filepath);
if not dest.endswith('.ipynb'):
dest += '.ipynb'
# Write the notebook on local storage
self.local.write(dest, nbformat.writes(nb));
return os.path.join(self.local.sharedNotebook, dest);
开发者ID:Valdimus,项目名称:nbshared,代码行数:17,代码来源:storage.py
示例19: test_directory_empty_mainipynb
def test_directory_empty_mainipynb(self):
import nbformat
doc = Document()
source = nbformat.v4.new_notebook()
result = {}
def load(filename):
handler = bahd.DirectoryHandler(filename=filename)
handler.modify_document(doc)
result['handler'] = handler
result['filename'] = filename
if handler.failed:
raise RuntimeError(handler.error)
with_directory_contents({
'main.ipynb': nbformat.writes(source)
}, load)
assert not doc.roots
开发者ID:digitalsatori,项目名称:Bokeh,代码行数:19,代码来源:test_directory.py
示例20: merge_notebooks
def merge_notebooks(outfile, filenames):
merged = None
added_appendix = False
for fname in filenames:
with io.open(fname, 'r', encoding='utf-8') as f:
nb = nbformat.read(f, nbformat.NO_CONVERT)
remove_formatting(nb)
if not added_appendix and fname[0:8] == 'Appendix':
remove_links_add_appendix(nb)
added_appendix = True
else:
remove_links(nb)
if merged is None:
merged = nb
else:
merged.cells.extend(nb.cells)
#merged.metadata.name += "_merged"
outfile.write(nbformat.writes(merged, nbformat.NO_CONVERT))
开发者ID:garlinplus,项目名称:Kalman-and-Bayesian-Filters-in-Python,代码行数:19,代码来源:merge_book.py
注:本文中的nbformat.writes函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论