Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
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
def test_widgets_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
def test_notebook(fpath):
"""Execute each notebook."""
# Information on running notebooks is found here
# https://nbconvert.readthedocs.io/en/latest/execute_api.html
with fpath.open() as fp:
nb = nbformat.read(fp, as_version=4)
ep = ExecutePreprocessor(timeout=600)
try:
ep.preprocess(nb, {'metadata': {'path': fpath.parent}})
except CellExecutionError:
msg = 'Error executing the notebook "%s".\n\n' % fpath
msg += 'See notebook "%s" for the traceback.' % fpath
print(msg)
raise
finally:
with fpath.open(mode='w', encoding='utf-8') as fp:
nbformat.write(nb, fp)
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
elif b_errors and not progress_bar:
log.error(notebook_filename + " execution failed.")
if args.get('parallel'):
futures = []
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
for notebook_run_cmd in notebook_run_cmds:
run_notebook_name, notebook_save_name, nb_params = utils.parse_run_str(notebook_run_cmd)
futures.append(executor.submit(execute_notebook, run_notebook_name, notebook_save_name, nb_params))
# Handle other notebook runs if one or more fails intermittently
for future in concurrent.futures.as_completed(futures):
try:
future.result()
except CellExecutionError:
raise
else:
for notebook_run_cmd in notebook_run_cmds:
run_notebook_name, notebook_save_name, nb_params = utils.parse_run_str(notebook_run_cmd)
execute_notebook(run_notebook_name, notebook_save_name, nb_params)
path = os.path.join(
os.path.abspath("theano_cache"), "p{0}".format(os.getpid())
)
os.makedirs(path, exist_ok=True)
os.environ["THEANO_FLAGS"] = "base_compiledir={0}".format(path)
with open(filename) as f:
notebook = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=-1, kernel_name=kernel_name)
ep.log.setLevel(logging.DEBUG)
ep.log.addHandler(logging.StreamHandler())
try:
ep.preprocess(notebook, {"metadata": {"path": "examples/"}})
except CellExecutionError as e:
msg = "error while running: {0}\n\n".format(filename)
msg += e.traceback
print(msg)
finally:
with open(os.path.join(filename), mode="w") as f:
nbformat.write(notebook, f)
errors = []
for filename in glob.glob(pattern):
if filename.endswith("_exec.ipynb"):
continue
with open(filename) as f:
notebook = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=-1)
print("running: {0}".format(filename))
try:
ep.preprocess(notebook, {"metadata": {"path": "."}})
except CellExecutionError as e:
msg = "error while running: {0}\n\n".format(filename)
msg += e.traceback
print(msg)
errors.append(msg)
finally:
with open(os.path.splitext(filename)[0] + "_exec.ipynb",
mode="wt") as f:
nbformat.write(notebook, f)
txt = "\n\n".join(errors)
ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]')
txt = ansi_escape.sub("", txt)
with open("notebook_errors.log", "wb") as f:
f.write(txt.encode("utf-8"))
def run_tests(self):
LOG.info("Triggered test for %s", self.file_path)
try:
self.is_successful, console_output = execute_notebook(self.file_path)
result = self.result_string()
if not self.is_successful or _is_verbose():
result += console_output
LOG.info(result)
except CellExecutionError as cell_error:
LOG.error(self.error_string(cell_error.traceback))
except ConversionException:
LOG.exception("Execution of notebook '%s' failed", self.file_path)
nb = nbformat.read(nb, as_version=notebook_version)
ep = ExecutePreprocessor(
timeout=self.timeout,
kernel_name=self.kernel_name
)
ep.preprocess(nb, {
'metadata': {
'path': os.path.dirname(self.notebook_path)
}
})
logger.info('===== Done! =====')
except CellExecutionError:
logger.info('===== Done with errors! =====')
raise
finally:
# clean up (remove temporary JSON file)
if self.json_action == 'delete':
os.remove(tmp_file_path)