本文整理汇总了Python中nbconvert.preprocessors.ExecutePreprocessor.preprocess方法的典型用法代码示例。如果您正苦于以下问题:Python ExecutePreprocessor.preprocess方法的具体用法?Python ExecutePreprocessor.preprocess怎么用?Python ExecutePreprocessor.preprocess使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nbconvert.preprocessors.ExecutePreprocessor
的用法示例。
在下文中一共展示了ExecutePreprocessor.preprocess方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_tutorial_nb
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def test_tutorial_nb(file_path):
"""Run tutorial jupyter notebook to catch any execution error.
Parameters
----------
file_path : str
path of tutorial markdown file
"""
tutorial_name = os.path.basename(file_path)
notebook = nbformat.read(file_path + '.ipynb', as_version=4)
eprocessor = ExecutePreprocessor(timeout=1800)
try:
eprocessor.preprocess(notebook, {'metadata': {}})
except Exception as err:
err_msg = str(err)
fail_dict[tutorial_name] = err_msg
finally:
output_nb = open("output.txt", mode='w')
nbformat.write(notebook, output_nb)
output_nb.close()
output_nb = open("output.txt", mode='r')
for line in output_nb:
if "Warning:" in line:
fail_dict[tutorial_name] = "%s has warning." % (tutorial_name)
return
示例2: execute_nb
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def execute_nb(src, dst, allow_errors=False, timeout=1000, kernel_name=''):
"""
Execute notebook in `src` and write the output to `dst`
Parameters
----------
src, dst: str
path to notebook
allow_errors: bool
timeout: int
kernel_name: str
defualts to value set in notebook metadata
Returns
-------
dst: str
"""
import nbformat
from nbconvert.preprocessors import ExecutePreprocessor
with io.open(src, encoding='utf-8') as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(allow_errors=allow_errors,
timeout=timeout,
kernel_name=kernel_name)
ep.preprocess(nb, resources={})
with io.open(dst, 'wt', encoding='utf-8') as f:
nbformat.write(nb, f)
return dst
示例3: _notebook_run
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def _notebook_run(path):
"""Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
kernel_name = "python%d" % sys.version_info[0]
this_file_directory = os.path.dirname(__file__)
errors = []
with tempfile.NamedTemporaryFile(suffix=".ipynb", mode="wt") as fout:
with open(path) as f:
nb = nbformat.read(f, as_version=4)
nb.metadata.get("kernelspec", {})["name"] = kernel_name
ep = ExecutePreprocessor(kernel_name=kernel_name, timeout=10)
try:
ep.preprocess(nb, {"metadata": {"path": this_file_directory}})
except CellExecutionError as e:
if "SKIP" in e.traceback:
print(str(e.traceback).split("\n")[-2])
else:
raise e
except TimeoutError as e:
print(e)
finally:
nbformat.write(nb, fout)
# nb = nbformat.read(fout, nbformat.current_nbformat)
# errors = errors.extend(
# [output for cell in nb.cells if "outputs" in cell
# for output in cell["outputs"] if output.output_type == "error"])
return nb, errors
示例4: _runTest
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def _runTest(self):
kernel = 'python%d' % sys.version_info[0]
cur_dir = os.path.dirname(self.nbfile)
with open(self.nbfile) as f:
nb = nbformat.read(f, as_version=4)
if self.cov:
covdict = {'cell_type': 'code', 'execution_count': 1,
'metadata': {'collapsed': True}, 'outputs': [],
'nbsphinx': 'hidden',
'source': 'import coverage\n'
'coverage.process_startup()\n'
'import sys\n'
'sys.path.append("{0}")\n'.format(cur_dir)
}
nb['cells'].insert(0, nbformat.from_dict(covdict))
exproc = ExecutePreprocessor(kernel_name=kernel, timeout=600)
try:
run_dir = os.getenv('WRADLIB_BUILD_DIR', cur_dir)
exproc.preprocess(nb, {'metadata': {'path': run_dir}})
except CellExecutionError as e:
raise e
if self.cov:
nb['cells'].pop(0)
with io.open(self.nbfile, 'wt') as f:
nbformat.write(nb, f)
self.assertTrue(True)
示例5: execute_nb
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def execute_nb(src, dst, allow_errors=False, timeout=1000, kernel_name=None):
'''
Execute notebook in `src` and write the output to `dst`
Parameters
----------
src, dst: str
path to notebook
allow_errors: bool
timeout: int
kernel_name: str
defualts to value set in notebook metadata
Returns
-------
dst: str
'''
with io.open(src, encoding='utf-8') as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(allow_errors=False,
timeout=timeout,
kernel_name=kernel_name)
ep.preprocess(nb, {'metadata': {'path': SOURCE_DIR}})
with io.open(dst, 'wt', encoding='utf-8') as f:
nbformat.write(nb, f)
return dst
示例6: nb_to_html
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def nb_to_html(root, template='basic', version=4, timeout=600, kernel='python3'):
'''
This functions executes a Jupyter notebook and creates the related
HTML file.
Args:
root (str): name of the file without the .ipynb extension
template (str): name of the template (to be in the current folder as template.tpl)
version (int): version of the notebook
timeout (float): maximum time spent per cell
kernel (str)
Returns:
None
The function executes root.ipynb into root_exe.ipynb and creates the file root.html.
'''
with open(root + '.ipynb') as f:
nb = nbformat.read(f, as_version=version)
ep = ExecutePreprocessor(timeout=timeout, kernel_name=kernel)
ep.preprocess(nb, {'metadata': {'path': '.'}})
with open(root + '_exe.ipynb', 'wt') as f:
nbformat.write(nb, f)
html_exporter = HTMLExporter()
html_exporter.template_file = template
with open(root + '_exe.ipynb', mode='r') as f:
notebook = nbformat.reads(''.join(f.readlines()), as_version=version)
(body, _) = html_exporter.from_notebook_node(notebook)
codecs.open(root + '.html', 'w', encoding='utf-8').write(body)
示例7: run_notebook
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def run_notebook(notebook_name, nb_kwargs=None,
insert_pos=5, timeout=120, execute_kwargs=None):
"""Runs a notebook and displays the output in the master notebook.
Executes a notebook, optionally passing "arguments" in a way roughly
similar to passing arguments to a function.
Notebook arguments are passed in a dictionary (`nb_kwargs`) which is
converted to a string containing python code, then inserted in the notebook
as a code cell. The code contains only assignments of variables which
can be used to control the execution of a suitably written notebook. When
calling a notebook, you need to know which arguments (variables) to pass.
Differently from functions, no check on the input arguments is performed.
Arguments:
notebook_name (string): name of the notebook to be executed.
nb_kwargs (dict or None): If not None, this dict is converted to a
string of python assignments with keys representing variables
names and values variables content. This string is inserted as
code-cell in the notebook to be executed.
insert_pos (int): position of insertion of the code-cell containing
the input arguments. Default is 5 (i.e. sixth cell). With this
default, the input notebook can define, in the first cell, default
values of input arguments (used when the notebook is executed
with no arguments or through the Notebook GUI).
timeout (int): timeout in seconds after which the execution is aborted.
execute_kwargs (dict): additional arguments passed to
`ExecutePreprocessor`.
"""
if nb_kwargs is not None:
header = '# Cell inserted during automated execution.'
code = dict_to_code(nb_kwargs)
code_cell = '\n'.join((header, code))
if execute_kwargs is None:
execute_kwargs = {}
ep = ExecutePreprocessor(timeout=timeout, **execute_kwargs)
nb = nbformat.read(notebook_name, as_version=4)
if len(nb_kwargs) > 0:
nb['cells'].insert(insert_pos, nbformat.v4.new_code_cell(code_cell))
try:
# Execute the notebook
ep.preprocess(nb, {'metadata': {'path': './'}})
#*****************************************
# Title: analysis.ipynb
# Author: Benjamin RK
# Date: April 2013
# Availability: https://gist.github.com/minrk/5491090
ip = get_ipython()
for cell in nb.cells:
if cell.cell_type != 'code':
continue
ip.run_cell(cell.source)
#*****************************************
except:
# Execution failed, print a message then raise.
msg = 'Error executing the notebook "%s".\n\n' % notebook_name
print(msg)
raise
示例8: execute
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def execute(self):
print("Cleaning lowfat/reports/html ...")
old_reports = os.listdir("lowfat/reports/html")
for old_report in old_reports:
print("- Removing lowfat/reports/html/{}".format(old_report))
os.remove("lowfat/reports/html/{}".format(old_report))
print("Cleaning of lowfat/reports/html is complete.")
notebook_filenames = os.listdir("lowfat/reports")
for notebook_filename in notebook_filenames:
if not notebook_filename.endswith(".ipynb"):
continue
print("Processing lowfat/reports/{}".format(notebook_filename))
# Based on Executing notebooks, nbconvert Documentation by Jupyter Development Team.
# https://nbconvert.readthedocs.io/en/latest/execute_api.html
with open("lowfat/reports/{}".format(notebook_filename)) as file_:
notebook = nbformat.read(file_, as_version=4)
# Kernel is provided by https://github.com/django-extensions/django-extensions/
execute_preprocessor = ExecutePreprocessor(timeout=600, kernel_name='django_extensions')
execute_preprocessor.preprocess(notebook, {'metadata': {'path': '.'}})
html_exporter = HTMLExporter()
html_exporter.template_file = 'basic'
(body, dummy_resources) = html_exporter.from_notebook_node(notebook)
with open('lowfat/reports/html/{}.html'.format(notebook_filename), 'wt') as file_:
file_.write(body)
示例9: execute_nb
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def execute_nb(src, dst, allow_errors=False, timeout=1000, kernel_name=None):
"""
Execute notebook in `src` and write the output to `dst`
Parameters
----------
src, dst: str
path to notebook
allow_errors: bool
timeout: int
kernel_name: str
defualts to value set in notebook metadata
Returns
-------
dst: str
"""
with io.open(src, encoding="utf-8") as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(allow_errors=allow_errors, timeout=timeout, kernel_name=kernel_name)
ep.preprocess(nb, {"metadta": {"path": "notebooks/"}})
with io.open(dst, "wt", encoding="utf-8") as f:
nbformat.write(nb, f)
return dst
示例10: main
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def main():
arguments = docopt(__doc__, version='nbgen 2.0')
cmd = subprocess.run([arguments["<path>"]] + arguments["<arguments>"], stdout=subprocess.PIPE)
cmd.check_returncode()
cells = json.loads(cmd.stdout.decode("utf-8"))
nb_dict = {
"metadata": {},
"nbformat": 4,
"nbformat_minor": 0,
"cells": cells,
}
notebook = nbformat.from_dict(nb_dict)
ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
ep.preprocess(notebook, {'metadata': {}})
if arguments["nb"]:
nbformat.write(notebook, "{}.ipynb".format(arguments["<name>"]))
elif arguments["slides"]:
config = Config()
reveal_cdn = "https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.3.0/"
config.SlidesExporter.reveal_url_prefix = (arguments["--reveal"] or reveal_cdn)
slides, __ = export_slides(nb=notebook, config=config)
with open("{}.html".format(arguments["<name>"]), "w") as html_file:
html_file.write(slides)
示例11: _notebook_run
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def _notebook_run(path):
"""Execute a notebook via nbconvert and collect output.
:returns (parsed nb object, execution errors)
"""
kernel_name = 'python%d' % sys.version_info[0]
this_file_directory = os.path.dirname(__file__)
errors = []
with tempfile.NamedTemporaryFile(suffix=".ipynb", mode='wt') as fout:
with open(path) as f:
nb = nbformat.read(f, as_version=4)
nb.metadata.get('kernelspec', {})['name'] = kernel_name
ep = ExecutePreprocessor(kernel_name=kernel_name, timeout=10)
try:
ep.preprocess(nb, {'metadata': {'path': this_file_directory}})
except CellExecutionError as e:
if "SKIP" in e.traceback:
print(str(e.traceback).split("\n")[-2])
else:
raise e
except RuntimeError as e:
print(e)
finally:
nbformat.write(nb, fout)
return nb, errors
示例12: test_notebook
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def test_notebook(notebook):
nb_name = os.path.split(notebook)[-1]
if nb_name in SLOW_NOTEBOOKS:
pytest.skip('Notebook is too slow to test')
nb = nbformat.read(notebook, as_version=4)
ep = ExecutePreprocessor(allow_errors=False,
timeout=240,
kernel_name=kernel_name)
ep.preprocess(nb, {'metadata': {'path': NOTEBOOK_DIR}})
示例13: run_ipynb
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def run_ipynb(fs):
"""
"""
with open(fs) as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor()
ep.preprocess(nb, {'metadata': {'path': os.path.dirname(fs)}})
with open(fs, 'wt') as f:
nbformat.write(nb, fs)
示例14: write_notebook
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def write_notebook(cells, outputfile, execute=True, kernel='python3'):
kernelspec = get_kernelspec(kernel)
notebook = new_notebook(cells=cells,
metadata={'language': 'python',
'kernelspec': kernelspec})
if execute:
ep = ExecutePreprocessor(timeout=600, kernelname='python3')
ep.preprocess(notebook,
{'metadata': {'path': os.path.dirname(outputfile)}})
nbformat.write(notebook, outputfile)
示例15: run_notebook
# 需要导入模块: from nbconvert.preprocessors import ExecutePreprocessor [as 别名]
# 或者: from nbconvert.preprocessors.ExecutePreprocessor import preprocess [as 别名]
def run_notebook(notebook):
# See http://nbconvert.readthedocs.io/en/latest/execute_api.html
# TODO: Specify 'django_extensions' kernel and make it work on the server.
# The kernel can be set as follows:
# ep = ExecutePreprocessor(timeout=120, kernel_name='django_extensions')
# This works locally, but on server, I wasn't able to create the kernel
# (list available kernels by `jupyter kernelspec list`).
# Default kernel currently works, given the `path` (directory from where to
# execute the notebook) is set to //backend. It may fail if some Django
# features are used in the notebook, but I haven't explored this.
ep = ExecutePreprocessor(timeout=120)
path = os.path.join(settings.REPO_DIR, 'backend')
ep.preprocess(notebook, {'metadata': {'path': path}})