本文整理汇总了Python中nbconvert.preprocessors.ExecutePreprocessor类的典型用法代码示例。如果您正苦于以下问题:Python ExecutePreprocessor类的具体用法?Python ExecutePreprocessor怎么用?Python ExecutePreprocessor使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ExecutePreprocessor类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_tutorial_nb
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
开发者ID:Piyush3dB,项目名称:mxnet,代码行数:25,代码来源:test_tutorial.py
示例2: _notebook_run
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
开发者ID:abs51295,项目名称:gensim,代码行数:27,代码来源:test_notebooks.py
示例3: execute_nb
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
开发者ID:RogerThomas,项目名称:pandas,代码行数:31,代码来源:make.py
示例4: main
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)
开发者ID:ewjoachim,项目名称:nbgen,代码行数:29,代码来源:__init__.py
示例5: _runTest
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)
开发者ID:heistermann,项目名称:wradlib,代码行数:32,代码来源:testrunner.py
示例6: execute_nb
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
开发者ID:ChadFulton,项目名称:statsmodels,代码行数:28,代码来源:nbgenerate.py
示例7: run_notebook
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
开发者ID:shibberu,项目名称:Big-Data-MVP,代码行数:60,代码来源:nbrun.py
示例8: nb_to_html
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)
开发者ID:PetitLepton,项目名称:design,代码行数:33,代码来源:nb_to_report.py
示例9: execute
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)
开发者ID:softwaresaved,项目名称:fat,代码行数:32,代码来源:report.py
示例10: execute_nb
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
开发者ID:statsmodels,项目名称:statsmodels,代码行数:26,代码来源:nbgenerate.py
示例11: _notebook_run
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
开发者ID:RaRe-Technologies,项目名称:gensim,代码行数:32,代码来源:test_notebooks.py
示例12: test_notebook
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}})
开发者ID:bashtage,项目名称:arch,代码行数:9,代码来源:test_examples.py
示例13: run_ipynb
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)
开发者ID:bartvle,项目名称:Synthacc,代码行数:10,代码来源:run_demos.py
示例14: write_notebook
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)
开发者ID:iliatimofeev,项目名称:altair_notebooks,代码行数:12,代码来源:create_example_notebooks.py
示例15: _test_tutorial_nb
def _test_tutorial_nb(tutorial):
"""Run tutorial jupyter notebook to catch any execution error.
Parameters
----------
tutorial : str
tutorial name in folder/tutorial format
"""
tutorial_dir = os.path.join(os.path.dirname(__file__), '..', '..', 'docs', '_build', 'html', 'tutorials')
tutorial_path = os.path.join(*([tutorial_dir] + tutorial.split('/')))
# see env variable docs in the doc string of the file
kernel = os.getenv('MXNET_TUTORIAL_TEST_KERNEL', None)
no_cache = os.getenv('MXNET_TUTORIAL_TEST_NO_CACHE', False)
working_dir = os.path.join(*([temp_dir] + tutorial.split('/')))
if no_cache == '1':
print("Cleaning and setting up temp directory '{}'".format(working_dir))
shutil.rmtree(temp_dir, ignore_errors=True)
errors = []
notebook = None
if not os.path.isdir(working_dir):
os.makedirs(working_dir)
try:
notebook = nbformat.read(tutorial_path + '.ipynb', as_version=IPYTHON_VERSION)
# Adding a small delay to allow time for sockets to be freed
# stop-gap measure to battle the 1000ms linger of socket hard coded
# in the kernel API code
time.sleep(1.1)
if kernel is not None:
eprocessor = ExecutePreprocessor(timeout=TIME_OUT, kernel_name=kernel)
else:
eprocessor = ExecutePreprocessor(timeout=TIME_OUT)
nb, _ = eprocessor.preprocess(notebook, {'metadata': {'path': working_dir}})
except Exception as err:
err_msg = str(err)
errors.append(err_msg)
finally:
if notebook is not None:
output_file = os.path.join(working_dir, "output.txt")
nbformat.write(notebook, output_file)
output_nb = open(output_file, mode='r')
for line in output_nb:
if "Warning:" in line:
errors.append("Warning:\n"+line)
if len(errors) > 0:
print('\n'.join(errors))
return False
return True
开发者ID:tuliang1996,项目名称:incubator-mxnet,代码行数:52,代码来源:test_tutorials.py
示例16: run_notebook
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}})
开发者ID:adaptive-learning,项目名称:robomission,代码行数:13,代码来源:export_monitoring_notebook.py
示例17: test_notebooks
def test_notebooks(tmpdir, filename):
""" Runs a given notebook in a tmpdir """
import pylada
from os.path import join, dirname
from nbformat import read
from nbconvert.preprocessors import ExecutePreprocessor
from sys import version_info
directory = join(dirname(pylada.__file__), 'notebooks')
with open(join(directory, filename + ".ipynb")) as notebook_file:
notebook = read(notebook_file, as_version=4)
preprocessor = ExecutePreprocessor(kernel_name='python%i' %
version_info.major)
preprocessor.preprocess(notebook, {'metadata': {'path': str(tmpdir)}})
开发者ID:pylada,项目名称:pylada-light,代码行数:13,代码来源:test_notebooks.py
示例18: 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
示例19: notebook_tester
def notebook_tester(fname):
raw_nb = Exporter().from_filename(fname)
raw_nb[0].metadata.setdefault('kernelspec', {})['name'] = 'python'
preproc = ExecutePreprocessor(timeout=-1)
try:
exec_nb = preproc.preprocess(*raw_nb)
except Exception as e:
return '[Failed]\n{}'.format(e)
out_nb = HTMLExporter().from_notebook_node(*exec_nb)
fout = fname.replace('.ipynb', '.html')
with open(fout, 'w') as f:
f.write(out_nb[0])
return '[Passed]'
开发者ID:ODM2,项目名称:conda-recipes-ODM2,代码行数:14,代码来源:test_notebooks.py
示例20: test_func
def test_func(self):
passing = True
print("\n--------------- Testing {0} ---------------".format(nbname))
print(" {0}".format(nbpath))
if nbname in py2Ignore and sys.version_info[0] == 2:
print(" Skipping {}".format(nbname))
return
ep = ClearOutputPreprocessor()
with open(nbpath) as f:
nb = nbformat.read(f, as_version=4)
ep.preprocess(nb, {})
ex = ExecutePreprocessor(
timeout=600,
kernel_name='python{}'.format(sys.version_info[0]),
allow_errors=True
)
out = ex.preprocess(nb, {})
for cell in out[0]['cells']:
if 'outputs' in cell.keys():
for output in cell['outputs']:
if output['output_type'] == 'error': #in output.keys():
passing = False
err_msg = []
for o in output['traceback']:
err_msg += ["{}".format(o)]
err_msg = "\n".join(err_msg)
msg = """
\n <<<<< {} FAILED >>>>> \n
{} in cell [{}] \n-----------\n{}\n-----------\n
----------------- >> begin Traceback << ----------------- \n
{}\n
\n----------------- >> end Traceback << -----------------\n
""".format(
nbname, output['ename'],
cell['execution_count'], cell['source'],
err_msg
)
assert passing, msg
print("\n ..... {0} Passed ..... \n".format(nbname))
开发者ID:angeleneleow,项目名称:em-apps,代码行数:50,代码来源:test_notebooks.py
注:本文中的nbconvert.preprocessors.ExecutePreprocessor类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论