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.dev5.tar.gz.

File metadata

File hashes

Hashes for swarmauri_tool_jupytervalidatenotebook-0.7.0.dev5.tar.gz
Algorithm Hash digest
SHA256 a350c00734ddae955eaec053a08b7e98c7341fe9bab52b46317a850060dfcbf3
MD5 91398f797b3835577cdbf54a82afd07a
BLAKE2b-256 acfca1dc4c6e6df1ecc221233f0ce1f7e3c6c3850bdd8d1696e6fd36db263338

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for swarmauri_tool_jupytervalidatenotebook-0.7.0.dev5-py3-none-any.whl
Algorithm Hash digest
SHA256 137aef07572c17d1396bc650f9cd5a6140006a5330e6f7782bb9abd56807009d
MD5 4a3e959fcd3e4066647d3f7ef702dbc5
BLAKE2b-256 7b4551431ff3bec029a72319ad1cdcdbb1ecd3e2d4d02f175a35628969a418db

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