本文整理汇总了Python中IPython.nbformat.current.read方法的典型用法代码示例。如果您正苦于以下问题:Python current.read方法的具体用法?Python current.read怎么用?Python current.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IPython.nbformat.current
的用法示例。
在下文中一共展示了current.read方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: evaluate_notebook
# 需要导入模块: from IPython.nbformat import current [as 别名]
# 或者: from IPython.nbformat.current import read [as 别名]
def evaluate_notebook(nb_path, dest_path=None, skip_exceptions=False):
# Create evaluated version and save it to the dest path.
# Always use --pylab so figures appear inline
# perhaps this is questionable?
notebook = read(open(nb_path), 'json')
nb_runner = NotebookRunner(notebook, pylab=True, mpl_inline=True)
try:
nb_runner.run_notebook(skip_exceptions=skip_exceptions)
except NotebookError as e:
print('\n', '-'*80)
print(e)
print('-'*80)
raise
# Return the traceback, filtering out ANSI color codes.
# http://stackoverflow.com/questions/13506033/filtering-out-ansi-escape-sequences
# return 'Notebook conversion failed with the following traceback: \n%s' % \
# re.sub(r'\\033[\[\]]([0-9]{1,2}([;@][0-9]{0,2})*)*[mKP]?', '', str(e))
if dest_path is None:
dest_path = 'temp_evaluated.ipynb'
write(nb_runner.nb, open(dest_path, 'w'), 'json')
ret = nb_to_html(dest_path)
if dest_path is 'temp_evaluated.ipynb':
os.remove(dest_path)
return ret
示例2: from_notebook_node
# 需要导入模块: from IPython.nbformat import current [as 别名]
# 或者: from IPython.nbformat.current import read [as 别名]
def from_notebook_node(self, nb, resources=None, **kw):
"""
Convert a notebook from a notebook node instance.
Parameters
----------
nb : Notebook node
resources : dict (**kw)
of additional resources that can be accessed read/write by
transformers and filters.
"""
nb_copy = copy.deepcopy(nb)
resources = self._init_resources(resources)
# Preprocess
nb_copy, resources = self._transform(nb_copy, resources)
self._load_template()
if self.template is not None:
output = self.template.render(nb=nb_copy, resources=resources)
else:
raise IOError('template file "%s" could not be found' % self.template_file)
return output, resources
示例3: from_filename
# 需要导入模块: from IPython.nbformat import current [as 别名]
# 或者: from IPython.nbformat.current import read [as 别名]
def from_filename(self, filename, resources=None, **kw):
"""
Convert a notebook from a notebook file.
Parameters
----------
filename : str
Full filename of the notebook file to open and convert.
"""
#Pull the metadata from the filesystem.
if resources is None:
resources = ResourcesDict()
if not 'metadata' in resources or resources['metadata'] == '':
resources['metadata'] = ResourcesDict()
basename = os.path.basename(filename)
notebook_name = basename[:basename.rfind('.')]
resources['metadata']['name'] = notebook_name
modified_date = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
resources['metadata']['modified_date'] = modified_date.strftime(text.date_format)
with io.open(filename) as f:
return self.from_notebook_node(nbformat.read(f, 'json'), resources=resources,**kw)
示例4: _transform
# 需要导入模块: from IPython.nbformat import current [as 别名]
# 或者: from IPython.nbformat.current import read [as 别名]
def _transform(self, nb, resources):
"""
Preprocess the notebook before passing it into the Jinja engine.
To preprocess the notebook is to apply all of the
Parameters
----------
nb : notebook node
notebook that is being exported.
resources : a dict of additional resources that
can be accessed read/write by transformers
and filters.
"""
# Do a copy.deepcopy first,
# we are never safe enough with what the transformers could do.
nbc = copy.deepcopy(nb)
resc = copy.deepcopy(resources)
#Run each transformer on the notebook. Carry the output along
#to each transformer
for transformer in self._transformers:
nbc, resc = transformer(nbc, resc)
return nbc, resc
示例5: from_file
# 需要导入模块: from IPython.nbformat import current [as 别名]
# 或者: from IPython.nbformat.current import read [as 别名]
def from_file(self, file_stream, resources=None, **kw):
"""
Convert a notebook from a notebook file.
Parameters
----------
file_stream : file-like object
Notebook file-like object to convert.
"""
return self.from_notebook_node(nbformat.read(file_stream, 'json'), resources=resources, **kw)
示例6: test_export_nbnode
# 需要导入模块: from IPython.nbformat import current [as 别名]
# 或者: from IPython.nbformat.current import read [as 别名]
def test_export_nbnode(self):
"""
Can a notebook be exported by a notebook node handle?
"""
with open(self._get_notebook(), 'r') as f:
notebook = nbformat.read(f, 'json')
(output, resources) = export_by_name('python', notebook)
assert len(output) > 0
示例7: get_notebook
# 需要导入模块: from IPython.nbformat import current [as 别名]
# 或者: from IPython.nbformat.current import read [as 别名]
def get_notebook(self, name, path='', content=True):
self.log.debug('get_notebook: {}'.format(locals()))
k = self._notebook_s3_key(path, name)
model = self._s3_key_notebook_to_model(k, timeformat=S3_TIMEFORMAT_GET_KEY)
if content:
try:
with tempfile.NamedTemporaryFile() as f:
k.get_file(f)
f.seek(0)
nb = current.read(f, u'json')
except Exception as e:
raise web.HTTPError(400, u"Unreadable Notebook: %s %s" % (os_path, e))
self.mark_trusted_cells(nb, name, path)
model['content'] = nb
return model
示例8: test_notebook
# 需要导入模块: from IPython.nbformat import current [as 别名]
# 或者: from IPython.nbformat.current import read [as 别名]
def test_notebook(filename):
nb = read(open(filename), 'json')
expected = deepcopy(nb['worksheets'][0]['cells'])
# TODO: figure out how to set the log-level for IPython here
r = NotebookRunner(nb)
r.run_notebook()
actual = r.nb['worksheets'][0]['cells']
failed = []
for exp, act in zip(expected, actual):
if exp['cell_type'] == 'code':
#print("comparing outputs for ", exp['input'])
for eo,ao in zip(exp['outputs'], act['outputs']):
#print("\t", eo['text'], ao['text'], eo['text']==ao['text'])
eotext = ['\n'.join(l for l in eo['text'].split('\n') if _filter_exceptions(l))]
aotext = ['\n'.join(l for l in ao['text'].split('\n') if _filter_exceptions(l))]
if eotext != aotext:
#print("\tFAILED")
failed.append({'prompt_number': exp['prompt_number'], 'input': exp['input'], 'expected_output': eotext, 'actual_output': aotext})
return len(failed)==0, failed, r.nb