Skip to main content

A tool that validates a NotebookNode object against the Jupyter Notebook schema using nbformat, ensuring structural correctness.

Project description

Swamauri Logo

PyPI - Downloads GitHub Hits PyPI - Python Version PyPI - License PyPI - swarmauri_tool_jupytervalidatenotebook


Swarmauri Tool Jupyter Validate Notebook

Overview

This package provides a tool that validates a Jupyter notebook (NotebookNode) against its JSON schema using nbformat. It is useful for ensuring that your notebooks follow the correct structural and metadata standards required for processing or distribution. The tool can easily be integrated into automated workflows for CI/CD or general code validation processes.

Installation

To install this package using pip:

pip install swarmauri_tool_jupytervalidatenotebook

If you are using Poetry, you may add the following line to your pyproject.toml under [tool.poetry.dependencies]:

swarmauri_tool_jupytervalidatenotebook = "*"

Then run:

poetry install

Make sure that you have a supported version of Python (3.10+), together with the required dependencies as defined in the pyproject.toml (including nbformat, pydantic, typing_extensions, etc.).

Usage

Below is a basic example of how to use the JupyterValidateNotebookTool to validate a notebook:


import logging import nbformat from swarmauri_tool_jupytervalidatenotebook import JupyterValidateNotebookTool

def main(): # Configure logging to see validation messages: logging.basicConfig(level=logging.INFO)

# Create an instance of the validation tool
validator = JupyterValidateNotebookTool()

# Load a notebook for validation. Make sure the notebook is in the correct format (v4 typically).
notebook = nbformat.read("my_notebook.ipynb", as_version=4)

# Invoke the validator by calling the tool with the notebook object
validation_result = validator(notebook)

# Check the outcome
if validation_result["valid"] == "True":
    print("Success:", validation_result["report"])
else:
    print("Failure:", validation_result["report"])

if name == "main": main()


In this example: • We import nbformat to read the notebook file into a NotebookNode object.
• We instantiate JupyterValidateNotebookTool.
• We pass our notebook to the tool, which will return a dictionary with "valid" and "report" keys.
• We then inspect those keys to display the results of the validation procedure.

Advanced Usage

You can further customize log handling or implement additional processing of the validation results to suit your workflow. For instance, you might collect statistics, filter notebooks based on validation success, or integrate the tool into multi-step pipelines.

Logging is handled by the Python logging library. For more production-focused scenarios, configure logging as needed to capture validation details, such as warnings or errors in your notebooks.

Example with expanded logging:


import logging import nbformat from swarmauri_tool_jupytervalidatenotebook import JupyterValidateNotebookTool

def validate_notebooks(notebook_paths): logger = logging.getLogger(name) logging.basicConfig(level=logging.INFO) validator = JupyterValidateNotebookTool()

for path in notebook_paths:
    try:
        notebook = nbformat.read(path, as_version=4)
        result = validator(notebook)
        if result["valid"] == "True":
            logger.info(f"{path} passed validation. Details: {result['report']}")
        else:
            logger.warning(f"{path} failed validation. Error: {result['report']}")
    except FileNotFoundError:
        logger.error(f"Notebook file not found: {path}")

if name == "main": notebooks_to_check = ["notebook1.ipynb", "notebook2.ipynb"] validate_notebooks(notebooks_to_check)


The above approach allows you to queue multiple notebooks for validation, with clear logs about success/failure.

Dependencies

Key libraries and versions: • Python >= 3.10,<3.13
• nbformat
• pydantic
• typing_extensions

For development, additional libraries such as pytest, flake8, and others may be included for testing and linting.

Versioning

The underlying version of this tool is managed by its own distribution metadata. You can retrieve the tool's version by referencing the version attribute in the package (if installed from PyPI) or by checking the version field in the pyproject.toml file.


For any issues, please consult the nbformat documentation to ensure your notebooks are well-formed. This tool primarily serves to confirm schema compliance, which is an essential first step in verifying proper notebook functionality in the broader Jupyter ecosystem.

Happy validating!

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file swarmauri_tool_jupytervalidatenotebook-0.7.3.dev2.tar.gz.

File metadata

File hashes

Hashes for swarmauri_tool_jupytervalidatenotebook-0.7.3.dev2.tar.gz
Algorithm Hash digest
SHA256 79c25e242c0f11c5fe69458ea970e4de5f582f1fb407346acf37352ca552c3ea
MD5 4e3ff823bd04b0f9570218f467b004a6
BLAKE2b-256 2b2c0c759ee02b96e4687a9ab8620f532554fd0ec6d647c335d806c7c6316931

See more details on using hashes here.

File details

Details for the file swarmauri_tool_jupytervalidatenotebook-0.7.3.dev2-py3-none-any.whl.

File metadata

File hashes

Hashes for swarmauri_tool_jupytervalidatenotebook-0.7.3.dev2-py3-none-any.whl
Algorithm Hash digest
SHA256 f90821c91a14480359a84736863a2369559b6388095d8c073a8a7f17141dd09a
MD5 ffa3efd6364f3d245728622198417a08
BLAKE2b-256 6e1d2dbab28319e7f9657427ec71cf7a93dec9731ba19441569ed383497479a0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page