Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def run_notebook(self, notebook_filename):
"""Copy to a sandbox"""
nb_dir, nb_name = os.path.split(notebook_filename)
sandboxed_nb = os.path.join(self.sandboxdir, nb_name)
shutil.copy2(notebook_filename, sandboxed_nb)
with open(notebook_filename) as f:
nb = nbformat.read(f, as_version=4)
ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
ep.extra_arguments = ['--Application.log_level=0']
print("Executing notebook %s in %s" % (notebook_filename, self.sandboxdir))
ep.preprocess(nb, {'metadata': {'path': self.sandboxdir}})
def apply_preprocessors(preprocessors, nbname):
notebooks_path = os.path.join(os.path.split(__file__)[0], 'notebooks')
with open(os.path.join(notebooks_path, nbname)) as f:
nb = nbformat.read(f, nbformat.NO_CONVERT)
exporter = nbconvert.PythonExporter()
for preprocessor in preprocessors:
exporter.register_preprocessor(preprocessor)
source, meta = exporter.from_notebook_node(nb)
return source
def run_test(project, zone, cluster, new_values): # pylint: disable=too-many-locals
# TODO(jeremy@lewi.us): Need to configure the notebook and test to build
# using GCB.
dirname = os.path.dirname(__file__)
if not dirname:
logging.info("__file__ doesn't apper to be absolute path.")
dirname = os.getcwd()
notebook_path = os.path.join(dirname, "TF on GKE.ipynb")
logging.info("Reading notebook %s", notebook_path)
if not os.path.exists(notebook_path):
raise ValueError("%s does not exist" % notebook_path)
with open(notebook_path) as hf:
node = nbformat.read(hf, nbformat.NO_CONVERT)
exporter = nbconvert.PythonExporter()
raw, _ = nbconvert.export(exporter, node)
credentials = GoogleCredentials.get_application_default()
gke = discovery.build("container", "v1", credentials=credentials)
lines = raw.splitlines()
modified = replace_vars(lines, new_values)
modified = strip_appendix(modified)
modified = strip_unexecutable(modified)
with tempfile.NamedTemporaryFile(suffix="notebook.py", prefix="tmpGke",
mode="w", delete=False) as hf:
:returns (parsed nb object, execution errors)
"""
major_version = sys.version_info[0]
kernel = kernel or 'python{}'.format(major_version)
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),
"--ExecutePreprocessor.kernel_name={}".format(kernel),
"--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
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
def run_notebook(path):
nb_name, _ = os.path.splitext(os.path.basename(path))
dirname = os.path.dirname(path)
with open(path) as f:
nb = nbformat.read(f, as_version=4)
print("Start ", path)
sys.stdout.flush()
proc = ExecutePreprocessor(timeout=600, kernel_name='python3')
proc.allow_errors = True
proc.preprocess(nb, {'metadata': {'path': dirname}})
errors = []
for cell in nb.cells:
if 'outputs' in cell:
for output in cell['outputs']:
if output.output_type == 'error':
errors.append(output)
if errors == []:
print(" " + path + " test successfully completed.")
def test_simple_hook_with_explicit_format(tmpdir):
nb_file = str(tmpdir.join('notebook.ipynb'))
py_file = str(tmpdir.join('notebook.py'))
nbformat.write(new_notebook(cells=[new_markdown_cell('Some text')]), nb_file)
nb = jupytext.read(nb_file)
jupytext.write(nb, py_file, fmt='py:percent')
with open(py_file) as fp:
text = fp.read()
assert '# %% [markdown]' in text.splitlines()
assert '# Some text' in text.splitlines()
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
main = textwrap.dedent(f"""\
import pandas as pd
from IPython.core.display import Image as image
df = pd.read_csv('{csv_file}')
y_true = df['Enorm DFT (eV)'].values
y_pred = df['Enorm Predicted (eV)'].values
savepath = './foobar.png'
stats = {stats}
{plot_func.__name__}(y_true, y_pred, savepath, stats, title='some plot of some data')
image(filename='foobar.png')
""")
nb = nbformat.v4.new_notebook()
text_cells = [header, func_strings, plot_func_string, main]
cells = [nbformat.v4.new_code_cell(cell_text)
for cell_text in text_cells]
nb['cells'] = cells
nbformat.write(nb, 'test.ipynb')
def matplotlib_notebook(tmpdir, request):
nb = nbformat.v4.new_notebook()
nb.cells.append(nbformat.v4.new_code_cell('\n'.join(
['import numpy as np',
'import matplotlib.pyplot as plt',
'% matplotlib inline'])))
for _ in range(request.param):
nb.cells.append(nbformat.v4.new_code_cell('\n'.join(
['plt.figure(dpi=100)',
'plt.plot(np.linspace(0, 1), np.power(np.linspace(0, 1), 2))',
'plt.show()'])))
ep = ExecutePreprocessor()
ep.preprocess(nb, {'metadata': {'path': tmpdir}})
return nb