本文整理汇总了Python中nbformat.read方法的典型用法代码示例。如果您正苦于以下问题:Python nbformat.read方法的具体用法?Python nbformat.read怎么用?Python nbformat.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nbformat
的用法示例。
在下文中一共展示了nbformat.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_timeseries_controls
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def test_timeseries_controls(self):
nb_path = Path(_NB_FOLDER).joinpath(_NB_NAME)
abs_path = Path(_NB_FOLDER).absolute()
with open(nb_path) as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600, kernel_name="python3")
try:
ep.preprocess(nb, {"metadata": {"path": abs_path}})
except CellExecutionError:
nb_err = str(nb_path).replace(".ipynb", "-err.ipynb")
msg = f"Error executing the notebook '{nb_path}'.\n"
msg += f"See notebook '{nb_err}' for the traceback."
print(msg)
with open(nb_err, mode="w", encoding="utf-8") as f:
nbformat.write(nb, f)
raise
示例2: get_notebook_checkpoint
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def get_notebook_checkpoint(self, checkpoint_id, path):
"""Get the content of a checkpoint for a notebook.
Returns a dict of the form:
{
"type": "notebook",
"content": <output of nbformat.read>,
}
"""
self.log.info("restoring %s from checkpoint %s", path, checkpoint_id)
cp = self._get_checkpoint_path(checkpoint_id, path)
exists, blob = self.parent._fetch(cp)
if not exists:
raise web.HTTPError(404, u"No such checkpoint: %s for %s" % (
checkpoint_id, path))
nb = self.parent._read_notebook(blob)
return {
"type": "notebook",
"content": nb
}
示例3: test_folium_map_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def test_folium_map_notebook(self):
nb_path = Path(_NB_FOLDER).joinpath(_NB_NAME)
abs_path = Path(_NB_FOLDER).absolute()
with open(nb_path) as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600, kernel_name="python3")
try:
ep.preprocess(nb, {"metadata": {"path": abs_path}})
except CellExecutionError:
nb_err = str(nb_path).replace(".ipynb", "-err.ipynb")
msg = f"Error executing the notebook '{nb_path}'.\n"
msg += f"See notebook '{nb_err}' for the traceback."
print(msg)
with open(nb_err, mode="w", encoding="utf-8") as f:
nbformat.write(nb, f)
raise
示例4: run_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def run_notebook(notebook_path):
nb_name, _ = os.path.splitext(os.path.basename(notebook_path))
with open(notebook_path) as f:
nb = nbformat.read(f, as_version=4)
proc = ExecutePreprocessor(timeout=600, kernel_name='python3')
proc.allow_errors = True
proc.preprocess(nb)
for num, cell in enumerate(nb.cells):
if 'outputs' in cell:
for output in cell['outputs']:
if output.output_type == 'error':
return cell.execution_count, output.traceback
return None
# 7-bit C1 ANSI sequences
示例5: run_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def run_notebook(path, workdir=None, timeout=_DEFAULT_TIMEOUT):
resources = {
'metadata': {
'path': workdir
}
} if workdir is not None else None
with open(path, 'r') as f:
nb = nbformat.read(f, as_version=_DEFAULT_IPYNB_VERSION)
ep = ExecutePreprocessor(timeout=timeout)
# Some notebooks may generate and automatically open reports in a web browser.
# This is inconvenient in an automated test suite, so let's disable it.
# Overwriting $BROWSER with a dummy command will keep the notebook
# kernel from being able to open a web browser on Linux.
# TODO find platform-neutral solution to suppress webbrowser.open
browser = os.environ.get('BROWSER', None)
os.environ['BROWSER'] = 'echo %s'
ep.preprocess(nb, resources=resources)
os.environ['BROWSER'] = browser
示例6: _read_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def _read_notebook(self, os_path, as_version=4):
"""Read a notebook from an os path."""
with self.open(os_path, 'r', encoding='utf-8') as f:
try:
# NEW
file_ext = _file_extension(os_path)
if file_ext == '.ipynb':
return nbformat.read(f, as_version=as_version)
else:
return convert(os_path, from_=self.format, to='notebook')
except Exception as e:
raise HTTPError(
400,
u"Unreadable Notebook: %s %r" % (os_path, e),
)
示例7: test_clustering_nbdisplay_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def test_clustering_nbdisplay_notebook(self):
nb_path = Path(_NB_FOLDER).joinpath(_NB_NAME)
abs_path = Path(_NB_FOLDER).absolute()
with open(nb_path) as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600, kernel_name="python3")
try:
ep.preprocess(nb, {"metadata": {"path": abs_path}})
except CellExecutionError:
nb_err = str(nb_path).replace(".ipynb", "-err.ipynb")
msg = f"Error executing the notebook '{nb_path}'.\n"
msg += f"See notebook '{nb_err}' for the traceback."
print(msg)
with open(nb_err, mode="w", encoding="utf-8") as f:
nbformat.write(nb, f)
raise
示例8: test_process_tree_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def test_process_tree_notebook():
nb_path = Path(_NB_FOLDER).joinpath(_NB_NAME)
abs_path = Path(_NB_FOLDER).absolute()
with open(nb_path) as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600, kernel_name="python3")
try:
ep.preprocess(nb, {"metadata": {"path": abs_path}})
except CellExecutionError:
nb_err = str(nb_path).replace(".ipynb", "-err.ipynb")
msg = f"Error executing the notebook '{nb_path}'.\n"
msg += f"See notebook '{nb_err}' for the traceback."
print(msg)
with open(nb_err, mode="w", encoding="utf-8") as f:
nbformat.write(nb, f)
raise
示例9: test_timeline_controls
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def test_timeline_controls(self):
nb_path = Path(_NB_FOLDER).joinpath(_NB_NAME)
abs_path = Path(_NB_FOLDER).absolute()
with open(nb_path) as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600, kernel_name="python3")
try:
ep.preprocess(nb, {"metadata": {"path": abs_path}})
except CellExecutionError:
nb_err = str(nb_path).replace(".ipynb", "-err.ipynb")
msg = f"Error executing the notebook '{nb_path}'.\n"
msg += f"See notebook '{nb_err}' for the traceback."
print(msg)
with open(nb_err, mode="w", encoding="utf-8") as f:
nbformat.write(nb, f)
raise
示例10: test_main_with_options
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def test_main_with_options(tmpdir):
# Given
code = dedent("""
from tex2ipy.tex2cells import Tex2Cells
class Converter(Tex2Cells):
def _handle_frame(self, node):
super(Converter, self)._handle_frame(node)
self.current['source'].append('## Overloaded\\n')
""")
convert = tmpdir.join('extra.py')
convert.write(code)
src = tmpdir.join('test.tex')
src.write(DOCUMENT)
dest = tmpdir.join('test.ipynb')
# When
main(args=[str(src), str(dest), '-c', str(convert)])
# Then
assert dest.check(file=1)
nb = nbformat.read(str(dest), 4)
src = nb.cells[0].source.splitlines()
assert src[0] == '## Overloaded'
assert src[1] == '## Foo'
示例11: test_can_run_examples_jupyter_notebooks
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def test_can_run_examples_jupyter_notebooks(self):
print("Examples folder ", self.examples_folder)
for filename in os.listdir(self.examples_folder):
if not filename.endswith('.ipynb'):
continue
path = os.path.join(self.examples_folder, filename)
notebook = nbformat.read(path, nbformat.NO_CONVERT)
state = {}
for cell in notebook.cells:
if cell.cell_type == 'code' and not is_matplotlib_cell(cell):
try:
exec(strip_magics_and_shows(cell.source), state)
# coverage: ignore
except:
print('Failed to run {}.'.format(path))
raise
示例12: run_notebook
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def run_notebook(filename):
run_path = os.path.split(filename)[0]
with open(filename) as f:
nb = nbformat.read(f, as_version=4)
try:
ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
ep.preprocess(nb, {'metadata': {'path': run_path}})
# FIXME: use tempfile and mv to avoid interruption
# better split the source code of the notebook and the compiled targets.
with open(filename, 'wt') as f:
nbformat.write(nb, f)
except Exception as e:
print('processing', filename, e)
示例13: convert
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def convert(nbfile):
basename, _ = os.path.splitext(nbfile)
meta = {'metadata': {'path': '.'}}
with open(nbfile, 'r', encoding='utf-8') as nbf:
nbdata = nbformat.read(nbf, as_version=4, encoding='utf-8')
runner = ExecutePreprocessor(timeout=600, kernel_name='probscale')
runner.preprocess(nbdata, meta)
img_folder = basename + '_files'
body_raw, images = RSTExporter().from_notebook_node(nbdata)
body_final = body_raw.replace('.. image:: ', '.. image:: {}/'.format(img_folder))
with open(basename + '.rst', 'w', encoding='utf-8') as rst_out:
rst_out.write(body_final)
for img_name, img_data in images['outputs'].items():
img_path = os.path.join(img_folder, img_name)
with open(img_path, 'wb') as img:
img.write(img_data)
示例14: get_pipeline_parameters
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def get_pipeline_parameters(request, source_notebook_path):
"""Get the pipeline parameters tagged in the notebook."""
# read notebook
log = request.log if hasattr(request, "log") else logger
try:
notebook = nbformat.read(source_notebook_path,
as_version=nbformat.NO_CONVERT)
params_source = parser.get_pipeline_parameters_source(notebook)
if params_source == '':
raise ValueError("No pipeline parameters found. Please tag a cell"
" of the notebook with the `pipeline-parameters`"
" tag.")
# get a dict from the 'pipeline parameters' cell source code
params_dict = ast.parse_assignments_expressions(params_source)
except ValueError as e:
log.exception("Value Error during parsing of pipeline parameters")
raise RPCInternalError(details=str(e), trans_id=request.trans_id)
# convert dict in list so its easier to parse in js
params = [[k, *v] for k, v in params_dict.items()]
log.info("Pipeline parameters:")
for ln in tabulate(params, headers=["name", "type", "value"]).split("\n"):
log.info(ln)
return params
示例15: get_pipeline_metrics
# 需要导入模块: import nbformat [as 别名]
# 或者: from nbformat import read [as 别名]
def get_pipeline_metrics(request, source_notebook_path):
"""Get the pipeline metrics tagged in the notebook."""
# read notebook
log = request.log if hasattr(request, "log") else logger
try:
notebook = nbformat.read(source_notebook_path,
as_version=nbformat.NO_CONVERT)
metrics_source = parser.get_pipeline_metrics_source(notebook)
if metrics_source == '':
raise ValueError("No pipeline metrics found. Please tag a cell"
" of the notebook with the `pipeline-metrics`"
" tag.")
# get a dict from the 'pipeline parameters' cell source code
metrics = ast.parse_metrics_print_statements(metrics_source)
except ValueError as e:
log.exception("Failed to parse pipeline metrics")
raise RPCInternalError(details=str(e), trans_id=request.trans_id)
log.info("Pipeline metrics: {}".format(metrics))
return metrics