本文整理汇总了Python中nbformat.current_nbformat方法的典型用法代码示例。如果您正苦于以下问题:Python nbformat.current_nbformat方法的具体用法?Python nbformat.current_nbformat怎么用?Python nbformat.current_nbformat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nbformat
的用法示例。
在下文中一共展示了nbformat.current_nbformat方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _notebook_run
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def _notebook_run(path):
"""Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
dirname = os.path.dirname(str(path))
os.chdir(dirname)
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
"--ExecutePreprocessor.timeout=360",
"--output", fout.name, str(path)]
subprocess.check_call(args)
fout.seek(0)
nb = nbformat.read(fout, nbformat.current_nbformat)
errors = [
output for cell in nb.cells if "outputs" in cell
for output in cell["outputs"]
if output.output_type == "error"
]
return nb, errors
示例2: _notebook_run
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def _notebook_run(path, kernel=None, timeout=60, capsys=None):
"""Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
major_version = sys.version_info[0]
dirname, __ = os.path.split(path)
os.chdir(dirname)
fname = os.path.join(here, 'test.ipynb')
args = [
"jupyter", "nbconvert", "--to", "notebook", "--execute",
"--ExecutePreprocessor.timeout={}".format(timeout),
"--output", fname, path]
subprocess.check_call(args)
nb = nbformat.read(io.open(fname, encoding='utf-8'),
nbformat.current_nbformat)
errors = [
output for cell in nb.cells if "outputs" in cell
for output in cell["outputs"] if output.output_type == "error"
]
os.remove(fname)
return nb, errors
示例3: _run_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def _run_notebook(self, path):
"""Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
"--ExecutePreprocessor.timeout=60",
"--output", fout.name, path]
try:
subprocess.check_output(args, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
# Note that CalledProcessError will have stdout/stderr in py3
# instead of output attribute
self.fail('jupyter nbconvert fails with:\n'
'STDOUT: %s\n' % (e.output))
fout.seek(0)
nb = nbformat.read(fout, nbformat.current_nbformat)
# gather all error messages in all cells in the notebook
errors = [output for cell in nb.cells if "outputs" in cell
for output in cell["outputs"]
if output.output_type == "error"]
return nb, errors
示例4: _notebook_read
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def _notebook_read(path):
"""
Parameters
----------
path: str
path to ipython notebook
Returns
-------
nb: notebook object
errors: list of Exceptions
"""
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
args = [
"jupyter-nbconvert", "--to", "notebook", "--execute",
"--ExecutePreprocessor.timeout=600", "--output", fout.name, path
]
subprocess.check_call(args)
fout.seek(0)
nb = nbformat.read(fout, nbformat.current_nbformat)
errors = [output for cell in nb.cells if "outputs" in cell
for output in cell["outputs"] \
if output.output_type == "error"]
return nb, errors
示例5: test_demo
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def test_demo(self):
"""Execute a notebook via nbconvert and collect output."""
# Determine if python 2 or 3 is being used.
major_version, minor_version = sys.version_info[:2]
if major_version == 2 or minor_version == 6:
version = str(major_version)
# Run ipython notebook.
with tempfile.NamedTemporaryFile(suffix='.ipynb') as output_file:
args = ['jupyter',
'nbconvert',
'--to',
'notebook',
'--execute',
'--ExecutePreprocessor.timeout=600',
'--ExecutePreprocessor.kernel_name=python{}'.format(
version),
'--output',
output_file.name,
self.path]
subprocess.check_call(args)
output_file.seek(0)
nb = nbformat.read(output_file, nbformat.current_nbformat)
# Parse output and make sure there are no errors.
errors = [output for cell in nb.cells if "outputs" in cell for
output in cell["outputs"] if
output.output_type == "error"]
else:
errors = []
self.assertEqual(errors, [])
示例6: _exec_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def _exec_notebook(notebook_filename, nbpath):
pythonkernel = 'python' + str(sys.version_info[0])
ep = ExecutePreprocessor(timeout=600, kernel_name=pythonkernel, interrupt_on_timeout=True)
with open(notebook_filename) as f:
nb = nbformat.read(f, as_version=nbformat.current_nbformat)
try:
ep.preprocess(nb, {'metadata': {'path': nbpath}})
except CellExecutionError:
print('-' * 60)
traceback.print_exc(file=sys.stdout)
print('-' * 60)
assert False, 'Error executing the notebook %s. See above for error.' % notebook_filename
示例7: _notebook_run
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def _notebook_run(notebook):
"""
Execute a notebook via nbconvert and collect output.
:return: (parsed nb object, execution errors)
"""
if os.path.isfile(temp_notebook):
os.remove(temp_notebook)
with open(temp_notebook, 'w') as fout:
# with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
args = ["jupyter", "nbconvert", "--to", "notebook", "--execute", "--allow-errors",
"--ExecutePreprocessor.timeout=-1",
"--output", fout.name, notebook]
try:
subprocess.check_call(args)
except subprocess.CalledProcessError as e:
if e.returncode == 1:
# print the message and ignore error with code 1 as this indicates there were errors in the notebook
print(e.output)
pass
else:
# all other codes indicate some other problem, rethrow
raise
with open(temp_notebook, 'r') as fout:
nb = nbformat.read(fout, nbformat.current_nbformat)
errors = [output for cell in nb.cells if "outputs" in cell for output in cell["outputs"]
if output.output_type == "error"]
os.remove(temp_notebook)
return nb, errors
示例8: notebook_run
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def notebook_run(path):
"""Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
dirname, __ = os.path.split(path)
os.chdir(dirname)
kername = "python3"
with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
"--ExecutePreprocessor.timeout=600",
"--ExecutePreprocessor.allow_errors=True",
"--ExecutePreprocessor.kernel_name={}".format(kername),
"--output", fout.name, path]
subprocess.check_call(args)
fout.seek(0)
nb = nbformat.read(fout, nbformat.current_nbformat)
errors = [output for cell in nb.cells if "outputs" in cell
for output in cell["outputs"]
if output.output_type == "error"]
return nb, errors
示例9: _notebook_run
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def _notebook_run(path):
"""Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
import nbformat
_, notebook = os.path.split(path)
base, ext = os.path.splitext(notebook)
with tempfile.NamedTemporaryFile("w", suffix=".ipynb") as fp:
args = [
"jupyter",
"nbconvert",
"--to",
"notebook",
"--execute",
"--ExecutePreprocessor.kernel_name=python",
"--ExecutePreprocessor.timeout=None",
"--output",
fp.name,
"--output-dir=.",
path,
]
subprocess.check_call(args)
nb = nbformat.read(fp.name, nbformat.current_nbformat, encoding="UTF-8")
errors = [
output
for cell in nb.cells
if "outputs" in cell
for output in cell["outputs"]
if output.output_type == "error"
]
return nb, errors
示例10: test_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def test_notebook(self):
test_path = os.path.abspath(__file__)
test_dir = os.path.dirname(test_path)
original_notebook = os.path.join(test_dir, 'test_notebook.ipynb')
with open(original_notebook) as f:
original_nb = nbformat.read(f, nbformat.current_nbformat)
expected_output = self._flatten_output_text(original_nb)
got_nb, errors = self._run_notebook(original_notebook)
self.assertFalse(errors)
got = self._flatten_output_text(got_nb)
self._assert_output(expected_output, got)
示例11: preprocess
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def preprocess(self, nb: NotebookNode, resources: ResourcesDict) -> Tuple[NotebookNode, ResourcesDict]:
"""Concatenates the cells from the header and footer notebooks to the
given cells.
"""
new_cells = []
# header
if self.header:
with io.open(self.header, encoding='utf-8') as fh:
header_nb = read_nb(fh, as_version=current_nbformat)
new_cells.extend(header_nb.cells)
# body
new_cells.extend(nb.cells)
# footer
if self.footer:
with io.open(self.footer, encoding='utf-8') as fh:
footer_nb = read_nb(fh, as_version=current_nbformat)
new_cells.extend(footer_nb.cells)
nb.cells = new_cells
super(IncludeHeaderFooter, self).preprocess(nb, resources)
return nb, resources
示例12: test_read
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def test_read():
currdir = os.path.split(__file__)[0]
path = os.path.join(currdir, "..", "apps", "files", "test-v2.ipynb")
read_v2(path, current_nbformat)
示例13: test_reads
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def test_reads():
currdir = os.path.split(__file__)[0]
path = os.path.join(currdir, "..", "apps", "files", "test-v2.ipynb")
contents = open(path, "r").read()
reads_v2(contents, current_nbformat)
示例14: test_write
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def test_write():
currdir = os.path.split(__file__)[0]
path = os.path.join(currdir, "..", "apps", "files", "test-v2.ipynb")
nb = read_v2(path, current_nbformat)
with tempfile.TemporaryFile(mode="w") as fh:
write_v2(nb, fh)
示例15: test_too_old
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import current_nbformat [as 别名]
def test_too_old():
currdir = os.path.split(__file__)[0]
path = os.path.join(currdir, "..", "apps", "files", "test-v0.ipynb")
with pytest.raises(SchemaMismatchError):
read_v2(path, current_nbformat)