nbconvert Documentation

repository·main·Indexed 23 days ago

https://github.com/jupyter/nbconvert

A tool for converting Jupyter Notebooks (.ipynb files) into various static formats including HTML, PDF, Markdown, LaTeX, Reveal JS, and reStructured Text using Jinja templates. The documentation covers CLI usage, programmatic conversion via NbConvertApp, and the extensibility framework including Preprocessors, PostProcessors, and specialized Exporter classes.

Tokens
13.6K
Snippets
40
Records
90
Agent score
83%

What's inside nbconvert

  1. Use Filters for custom content transformations

    main

    Filters are Python callables that take an input (typically text) and produce a text output. They are used within Jinja templates to perform specific transformations on cell content or metadata.

    Example of a filter usage in an HTML template:

    {{- output.text | ansi2html -}}

    In this example, ansi2html is a filter transforming text output on stdout into HTML.

  2. Configure external exporters using traitlets

    main

    External exporters can expose custom options using the traitlets configurable API. You can configure these options using standard Jupyter configuration methods:

    1. Configuration files: Use the syntax c.MyExporter.config_option=value.
    2. Command line flags: Use the syntax --MyExporter.config_option=value.

    Note that the specific configuration options available depend on the implementation of the exporter library itself.

  3. How the nbconvert pipeline works

    main

    nbconvert operates on a notebook (a JSON object) through a structured pipeline to produce output in different formats. The process follows this sequence:

    1. Loading: An Exporter loads the notebook (usually via nbformat).
    2. Preprocessing: A Preprocessor transforms the notebook content (e.g., re-executing cells, stripping output, or removing bundled files). The result of a preprocessor is always a notebook.
    3. Conversion: The notebook is converted into the destination format. Most exporters use TemplateExporter, which utilizes the jinja templating engine to map notebook structure (cells, inputs, outputs) to the output format.
    4. Filtering: During templating, filters (Python callables) transform specific content, such as converting ANSI text to HTML.
    5. Writing: A Writer takes the resulting data and writes it to either stdout or the filesystem.
    6. Postprocessing: A Postprocessor runs after the file has been written (e.g., starting a webserver to serve a slideshow).
  4. Manage Jupyter Widget state during execution

    main

    When executing notebooks that contain Jupyter Widgets, their state can be stored in the notebook metadata to allow for proper rendering in tools like nbviewer or HTML exports.

    • To disable storing widget state, set store_widget_state=False (via CLI or Python).
    • Note that widget rendering is not performed in a browser during execution; only default states or states modified by user code are captured.
    • If widget results are not visible after execution, you may need to 'Trust' the notebook in the Jupyter UI.
  5. Create custom Pygments syntax highlighting styles

    main
    To use a custom syntax highlighting style in nbconvert, you must first create it by subclassing pygments.styles.Style and then register it with the Pygments plugin system. Detailed instructions for this process are available in the official Pygments documentation.
  6. Use filters with TemplateExporter

    main
    Filters are transformation functions used with the nbconvert.exporters.templateexporter.TemplateExporter exporter. They allow you to transform notebook content into specific formats based on the target template. For example, you can use ansi2html() to convert ANSI color codes (often found in terminal tracebacks) into HTML-compatible colors during an HTML conversion.
  7. Understand the documentation structure

    main

    The nbconvert documentation is built using Sphinx. The following files and directories manage the build process and content:

    • conf.py: The Sphinx build configuration file.
    • source/: The directory containing the documentation source files.
    • source/api/: The directory containing source files for the automatically generated API documentation.
    • autogen_config.py: A script used to generate .rst files from .ipynb source files.
    • index.rst: The main landing page for the Sphinx documentation.
  8. Convert Jupyter notebooks using the CLI

    main

    Use the jupyter nbconvert command to convert .ipynb notebook files into various static formats using Jinja templates.

    Supported output formats include:

    • HTML
    • LaTeX
    • PDF
    • Reveal JS
    • Markdown (md)
    • ReStructured Text (rst)
    • executable script

    The basic command structure is: jupyter nbconvert --to <output format> <input notebook>

    $ jupyter nbconvert --to <output format> <input notebook>
  9. Remove cells, inputs, or outputs using cell tags

    main

    You can use the TagRemovePreprocessor to selectively remove entire cells, cell inputs, or cell outputs during conversion based on metadata tags assigned to the cells. The original notebook remains unchanged; only the exported output is modified.

    To use this, assign specific tags to your cells in the notebook metadata. You can then configure the preprocessor to look for these specific tag strings.

    Key configuration keys for TagRemovePreprocessor:

    • remove_cell_tags: A tuple of strings. Cells containing any of these tags will be removed entirely.
    • remove_input_tags: A tuple of strings. The input code of cells containing these tags will be removed.
    • remove_all_outputs_tags: A tuple of strings. The outputs of cells containing these tags will be removed.
    • enabled: Boolean to enable/disable the preprocessor.
    from traitlets.config import Config
    import nbformat as nbf
    from nbconvert.exporters import HTMLExporter
    from nbconvert.preprocessors import TagRemovePreprocessor
    
    # Setup config
    c = Config()
    
    # Configure tag removal
    c.TagRemovePreprocessor.remove_cell_tags = ("remove_cell",)
    c.TagRemovePreprocessor.remove_all_outputs_tags = ("remove_output",)
    c.TagRemovePreprocessor.remove_input_tags = ("remove_input",)
    c.TagRemovePreprocessor.enabled = True
    
    # Configure and run exporter
    c.HTMLExporter.preprocessors = ["nbconvert.preprocessors.TagRemovePreprocessor"]
    
    exporter = HTMLExporter(config=c)
    exporter.register_preprocessor(TagRemovePreprocessor(config=c), True)
    
    # Run exporter - returns a tuple (html_content, notebook_metadata)
    output = HTMLExporter(config=c).from_filename("your-notebook-file-path.ipynb")
    
    # Write to output html file
    with open("your-output-file-name.html", "w") as f:
        f.write(output[0])
  10. Install nbconvert for development

    main

    To install nbconvert in editable mode for development, you must first ensure pandoc is installed on your system.

    Prerequisite: Install Pandoc

    • Ubuntu/Debian: sudo apt-get install pandoc
    • macOS (Homebrew): brew install pandoc

    Development Installation Steps

    1. Clone the repository.
    2. Install the package in editable mode using pip install -e ..

    Running Tests After a development install, you can run the test suite by installing the test dependencies and using pytest:

    1. pip install nbconvert[test]
    2. py.test --pyargs nbconvert
    git clone https://github.com/jupyter/nbconvert.git
    cd nbconvert
    pip install -e .
    
    # To run tests:
    pip install nbconvert[test]
    py.test --pyargs nbconvert
  11. Register a custom exporter as a named entry point

    main

    To allow users to call your custom exporter by a short name instead of a fully qualified Python path, register it as a named entry point in your package's setup.py using the nbconvert.exporters group.

    Example setup.py configuration:

    setup(
        # ...
        entry_points={
            "nbconvert.exporters": [
                "simple = mymodule:SimpleExporter",
                "detail = mymodule:DetailExporter",
            ],
        }
    )

    Once installed, the exporter can be invoked via:

    jupyter nbconvert --to detail mynotebook.ipynb