How to use nbformat - 10 common examples

To help you get started, we’ve selected a few nbformat examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github legion-platform / legion / test-integration / View on Github external
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 =, 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}})
github holoviz / holoviews / tests / ipython / View on Github external
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.NO_CONVERT)
        exporter = nbconvert.PythonExporter()
        for preprocessor in preprocessors:
        source, meta = exporter.from_notebook_node(nb)
    return source
github kubeflow / tf-operator / examples / gke / View on Github external
def run_test(project, zone, cluster, new_values):  # pylint: disable=too-many-locals
    # TODO( Need to configure the notebook and test to build
    # using GCB.
    dirname = os.path.dirname(__file__)
    if not dirname:"__file__ doesn't apper to be absolute path.")
      dirname = os.getcwd()
    notebook_path = os.path.join(dirname, "TF on GKE.ipynb")"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.NO_CONVERT)
    exporter = nbconvert.PythonExporter()
    raw, _ = nbconvert.export(exporter, node)

    credentials = GoogleCredentials.get_application_default()
    gke ="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="", prefix="tmpGke",
                                     mode="w", delete=False) as hf:
github IAMconsortium / pyam / tests / View on Github external
:returns (parsed nb object, execution errors)
    major_version = sys.version_info[0]
    kernel = kernel or 'python{}'.format(major_version)
    dirname, __ = os.path.split(path)
    fname = os.path.join(here, 'test.ipynb')
    args = [
        "jupyter", "nbconvert", "--to", "notebook", "--execute",
        "--output", fname, path]

    nb =, encoding='utf-8'),

    errors = [
        output for cell in nb.cells if "outputs" in cell
        for output in cell["outputs"] if output.output_type == "error"


    return nb, errors
github IBM / AIF360 / tests / View on Github external
dirname, __ = os.path.split(path)

    kername = "python3"

    with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
        args = ["jupyter", "nbconvert", "--to", "notebook", "--execute",
                "--output",, path]

        nb =, 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
github networkit / networkit / notebooks / View on Github external
def run_notebook(path):
    nb_name, _ = os.path.splitext(os.path.basename(path))
    dirname = os.path.dirname(path)

    with open(path) as f:
        nb =, as_version=4)

    print("Start ", path)

    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':
    if errors == []:
        print(" " + path + " test successfully completed.")
github mwouts / jupytext / tests / View on Github external
def test_simple_hook_with_explicit_format(tmpdir):
    nb_file = str(tmpdir.join('notebook.ipynb'))
    py_file = str(tmpdir.join(''))
    nbformat.write(new_notebook(cells=[new_markdown_cell('Some text')]), nb_file)

    nb =
    jupytext.write(nb, py_file, fmt='py:percent')

    with open(py_file) as fp:
        text =

    assert '# %% [markdown]' in text.splitlines()
    assert '# Some text' in text.splitlines()
github microsoft / msticpy / tests / View on Github external
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 =, as_version=4)
        ep = ExecutePreprocessor(timeout=600, kernel_name="python3")

            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."
            with open(nb_err, mode="w", encoding="utf-8") as f:
                nbformat.write(nb, f)
github uw-cmg / MAST-ML / tests / View on Github external
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')

        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')
github m-rossi / jupyter-docx-bundler / tests / View on Github external
def matplotlib_notebook(tmpdir, request):
    nb = nbformat.v4.new_notebook()

        ['import numpy as np',
         'import matplotlib.pyplot as plt',
         '% matplotlib inline'])))

    for _ in range(request.param):
             'plt.plot(np.linspace(0, 1), np.power(np.linspace(0, 1), 2))',

    ep = ExecutePreprocessor()
    ep.preprocess(nb, {'metadata': {'path': tmpdir}})

    return nb