How to use the nbsphinx.Exporter function in nbsphinx

To help you get started, we’ve selected a few nbsphinx 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 has2k1 / plotnine / doc / sphinxext / examples_and_gallery.py View on Github external
metadata_path = os.path.dirname(rstfilename)
    unique_key = nbfilename.rstrip('.ipynb')

    resources = {
        'metadata': {'path': metadata_path},
        'output_files_dir': output_files_dir,
        # Prefix for the output image filenames
        'unique_key': unique_key
    }

    # Read notebook
    with open(nbfilepath, 'r') as f:
        nb = nbformat.read(f, as_version=4)

    # Export
    exporter = nbsphinx.Exporter(execute='never', allow_errors=True)
    (body, resources) = exporter.from_notebook_node(nb, resources)

    # Correct path for the resources
    for filename in list(resources['outputs'].keys()):
        tmp = os.path.join(RST_PATH, filename)
        resources['outputs'][tmp] = resources['outputs'].pop(filename)

    fw = FilesWriter()
    fw.build_directory = RST_PATH
    # Prevent "not in doctree" complains
    resources['output_extension'] = ''
    body = 'Examples\n--------\n' + body
    fw.write(body, resources, notebook_name=rstfilename)
github has2k1 / plotnine / doc / sphinxext / include_example.py View on Github external
The extension of the file is '.txt' since it will
        note be part of the `toctree`.
    """
    path = os.path.dirname(rst_filename)
    basename = os.path.splitext(os.path.basename(rst_filename))[0]
    resources_d = {
        'metadata': {'path': path},
        'output_files_dir': basename
    }

    # Read notebook
    with open(notebook_filename, 'r') as f:
        nb = nbformat.read(f, as_version=4)

    # Export
    rst_exporter = nbsphinx.Exporter(execute='never', allow_errors=True)
    (body, resources) = rst_exporter.from_notebook_node(
        nb, resources_d)

    # Correct path for the resources
    for filename in list(resources['outputs'].keys()):
        tmp = '{}/{}'.format(path, filename)
        resources['outputs'][tmp] = resources['outputs'].pop(filename)

    fw = FilesWriter()
    fw.build_directory = path
    # Prevent "not in doctree" complains
    resources['output_extension'] = '.txt'
    body = 'Examples\n--------\n' + body
    return fw.write(body, resources, notebook_name=basename)

nbsphinx

Jupyter Notebook Tools for Sphinx

MIT
Latest version published 4 months ago

Package Health Score

76 / 100
Full package analysis