Skip to main content

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

Project description

Swarmauri Logo

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


swarmauri_tool_jupytervalidatenotebook

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.0.dev6.tar.gz.

File metadata

File hashes

Hashes for swarmauri_tool_jupytervalidatenotebook-0.7.0.dev6.tar.gz
Algorithm Hash digest
SHA256 73d89f12e045fb57e0a08c8811762b74cb4087b3a0ce97206600ee0ed0f037b0
MD5 f03ead5e3d92386982b58e0e432c18c1
BLAKE2b-256 5dc1b5bb5be078a9d3b654670223a975ccc719f2afcbd8e9a025044fa7ff9bc4

See more details on using hashes here.

File details

Details for the file swarmauri_tool_jupytervalidatenotebook-0.7.0.dev6-py3-none-any.whl.

File metadata

File hashes

Hashes for swarmauri_tool_jupytervalidatenotebook-0.7.0.dev6-py3-none-any.whl
Algorithm Hash digest
SHA256 be0929491a2ef5179c4c09eeead712a7536e825171e92aff55a014a851518ff1
MD5 db237ca47cb1d02ee98b76c9c4e0954e
BLAKE2b-256 2f47a09233ef0899ba93f6828809b908abdaa1eef0f3f04ff59eec7192314e02

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