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 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.4.dev20.tar.gz.

File metadata

File hashes

Hashes for swarmauri_tool_jupytervalidatenotebook-0.7.4.dev20.tar.gz
Algorithm Hash digest
SHA256 f095fbc258ffafafda9c2adc26f46e2c19a10c2a73c6a5e2f3bea65bec120e42
MD5 ba59e29738ec613787ecc74a2aca6cc3
BLAKE2b-256 3560ab24f97d8b9b64e1a9aa388ba1eb4d340d469ff9befbcbe3f6c212071bc2

See more details on using hashes here.

File details

Details for the file swarmauri_tool_jupytervalidatenotebook-0.7.4.dev20-py3-none-any.whl.

File metadata

File hashes

Hashes for swarmauri_tool_jupytervalidatenotebook-0.7.4.dev20-py3-none-any.whl
Algorithm Hash digest
SHA256 4b041e8a4cca63a7203e0a204c9ff95752b104c2f517af86da939ffda65fb04d
MD5 49ef527a7a1b299b4e6c2e1261c33b50
BLAKE2b-256 236e27d8f3d30638a2ec9340a51c1b08aa3b0b1055eabdf64173faf322dbb0a7

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