How to use the nbconvert.export function in nbconvert

To help you get started, we’ve selected a few nbconvert 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 kubeflow / tf-operator / examples / gke / test_notebook.py View on Github external
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:
      code_path = hf.name
      hf.write("\n".join(modified))
github godatadriven / asekuro / asekuro / common.py View on Github external
After doing this it will take a python file that contains assert statements
    and will throw a status code of 2 if there are failures. Will also print
    the prefixed error message.
    :param ipynbfile: the notebook file
    :param pyfile: the python file
    :param verbose: turn on verbose mode, will print the code in both files
    :param prefix: optional prefix to add to the error message
    """
    if os.path.splitext(ipynbfile)[1] != '.ipynb':
        raise ValueError(".ipynb file needs to have .ipynb extension")
    if os.path.splitext(pyfile)[1] != '.py':
        raise ValueError(".py file needs to have .py extension")

    with open(ipynbfile, 'r') as readfile:
        notebook = nbformat.read(readfile, as_version=nbformat.NO_CONVERT)
    output, metadata = nbconvert.export(nbconvert.PythonExporter, notebook)
    if verbose:
        for i, cell in enumerate(output.split("#")):
            code = "#" + cell
            click.echo(click.style(f'showing block {i}', fg='blue'))
            for line in code.split("\n"):
                if line != "\n":
                    print("  " + line)
            try:
                exec(code)
            except:
                click.echo(click.style(f"Error in codeblock!", fg='red'))
                traceback.print_exc()
                click.echo(click.style(f"{prefix}notebook contains error, could not even pass it to py-file.", fg='red'))
                sys.exit(2)

    click.echo(click.style(f"Notebook {ipynbfile} has been parsed.", fg='blue'))
github pastas / pastas / doc / convert_nb.py View on Github external
""" This script is called by sphinxdoc to convert all Jupyter notebooks in the
Examples folder to ReStructured Text files and stores them in the documentation
folder.

Author: R.A. Collenteur 2016
"""

import nbconvert
from nbconvert import RSTExporter
import glob
import io
import os

notebooks = glob.iglob('../examples/notebooks/*.ipynb')
for nb in notebooks:
    (x,resources) = nbconvert.export(RSTExporter, nb)
    name = os.path.basename(nb)[:-6]
    pathname = '../doc/examples/{0}'.format(name)
    if not os.path.isdir(pathname):
        os.makedirs(pathname)
    fname = os.path.join(pathname,'{}.rst'.format(name))
    with io.open(fname, 'w', encoding='utf-8') as f:
        f.write(x[:])
    for output in resources['outputs'].keys():
        fname = os.path.join(pathname,output)
        with io.open(fname, 'wb') as f:
            f.write(resources['outputs'][output])

print("example notebooks successfully converted to .rst files")
github bdestombe / flopymetascript / flopymetascript / metafunctions.py View on Github external
assert os.path.isfile(nam_fp), f'{nam_fp} does not exist'
        mp = Model(nam_fp)
        nb = mp.script_model2nb(use_yapf=False)

        buff = io.BytesIO()
        zip_out = zipfile.ZipFile(buff, 'w')

        ipynb_buff = io.StringIO(nbformat.writes(nb))
        zip_out.writestr('test.ipynb', ipynb_buff.getvalue())

        for out_formats_item, out_formats_ext_item in zip(
                out_formats, out_formats_ext):
            ipynb_buff.seek(0)
            zip_out.writestr('test.' + out_formats_ext_item,
                             nbconvert.export(
                                 nbconvert.get_exporter(out_formats_item),
                                 ipynb_buff)[0])

        ipynb_buff.close()
        zip_out.close()
        print('\nContent of the zipfile:')
        zip_out.printdir()

    return buff
github bdestombe / flopymetascript / setup.py View on Github external
p_list_order = list(mp.packages.keys())
    toc = [
        '* [' + i + '](#' + i.replace('.', '') + ')\n' for i in p_list_order
    ]
    toc = ''.join(toc)

    with open('wiki_default_parameters.md', 'w') as f:
        f.write(toc)
        f.write(s)

    ipynb_buff.close()

    # without discription
    nb = mp.script_model2nb(print_descr=False, use_yapf=False)
    ipynb_buff = io.StringIO(nbformat.writes(nb))
    s = nbconvert.export(nbconvert.get_exporter('markdown'), ipynb_buff)[0]

    s = s.replace('\n\n```', '\n\n```python')

    p_list_order = list(mp.packages.keys())
    toc = [
        '* [' + i + '](#' + i.replace('.', '') + ')\n' for i in p_list_order
    ]
    toc = ''.join(toc)

    with open('wiki_default_parameters_without_description.md', 'w') as f:
        f.write(toc)
        f.write(s)

    ipynb_buff.close()

except OSError as e: