Convert Pydantic models to Mermaid class diagrams for easy visualization
Project description
Pydantic Visualizer
Convert your Pydantic models into beautiful Mermaid class diagrams with ease! 🎨
Pydantic Visualizer automatically generates visual representations of your Pydantic data models, making it easier to understand complex data structures, document your APIs, and communicate your data architecture.
🚀 Turn your pydantic model in mermaid diagram
✨ Features
- 🔄 Automatic Conversion: Transform Pydantic models to Mermaid diagrams instantly
- 🎯 Relationship Detection: Automatically identifies and visualizes model relationships
- 📊 Enum Support: Special handling and visualization for Enum types
- 🎨 Customizable Colors: Configure colors for different element types
- 📝 Multiple Export Formats: Save as Markdown or HTML
- 🌐 Browser Preview: Open diagrams directly in your browser
- 🔗 Nested Models: Handles complex nested model structures
- ⚡ Type-Safe: Fully typed with comprehensive type hints
📦 Installation
Using pip
pip install pydantic-visualizer
Using uv (recommended)
uv pip install pydantic-visualizer
uv add pydantic-visualizer
Exemple :
from pydantic_visualizer import PydanticVisualizer
# Define models
class Priority(str, Enum):
"""Task priority levels."""
LOW = "low"
MEDIUM = "medium"
HIGH = "high"
URGENT = "urgent"
class Comment(BaseModel):
"""Comment on a task."""
author: str = Field(description="Comment author")
text: str = Field(description="Comment text")
timestamp: str = Field(description="When the comment was made")
class Task(BaseModel):
"""Task model with priority and comments."""
title: str = Field(description="Task title")
description: str = Field(description="Detailed task description")
priority: Priority = Field(description="Task priority level")
completed: bool = Field(default=False, description="Whether task is completed")
comments: List[Comment] = Field(default_factory=list, description="Task comments")
# Create a visualizer instance
visualizer = PydanticVisualizer()
# Set the data model to visualize
visualizer.set_datamodel(Task)
# Add the second model (Nothing change here as Task is already set)
visualizer.add_model(Task)
# Print the Mermaid diagram code
print(visualizer.mermaid)
# Print the markdown enum table
print(visualizer.enum_markdown_tables)
# Returns a markdown description with tables for each class and subclass.
print(visualizer.description)
# Open the diagram in your browser
visualizer.show()
# Saving complete markdown (diagram + enums + descriptions)
visualizer.save_markdown(
output_folder="./examples/output",
include_diagram=True,
include_enums=True,
include_description=True,
)
# Saving complete HTML (diagram + enums + descriptions)
visualizer.save_html(
output_folder="./examples/output",
include_diagram=True,
include_enums=True,
include_description=True,
)
📚 More Examples: Visit the examples folder for 8+ complete, runnable examples covering:
- Basic usage and nested models
- Enum handling and custom colors
- Complex relationships and self-referencing models
- Saving to Markdown/HTML and browser preview
- Adding multiple models to one diagram
See the Examples README for detailed descriptions and usage instructions.
🎨 Diagram Features
Relationship Types
- Solid arrows (
-->) for required relationships - Dashed arrows (
..>) for optional relationships - Star notation (
*) for list/collection relationships
Visual Indicators
- Different colors for objects, lists, and enums
- Dashed borders for optional nested models
- Aligned field names for better readability
Development Setup
# Clone the repository
git clone https://github.com/Maxlo24/pydantic-visualizer.git
cd pydantic-visualizer
# Install dependencies with uv
uv pip install -e ".[dev]"
# Run tests
pytest
# Run linting
ruff check .
# Run type checking
mypy pydantic_visualizer
📝 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Built with Pydantic - Data validation using Python type hints
- Diagrams rendered with Mermaid - Generation of diagrams from text
📮 Contact & Support
- Repo: GitHub Repo
- Issues: GitHub Issues
- Discussions: GitHub Discussions
🗺️ Roadmap
- Support for Pydantic v1 models
- CLI tool for quick visualization
- Integration with FastAPI for automatic API documentation
- Export to additional formats (PNG, SVG, PDF)
Made with ❤️ by Maxime Gillot
Project details
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_visualizer-0.4.0.tar.gz.
File metadata
- Download URL: pydantic_visualizer-0.4.0.tar.gz
- Upload date:
- Size: 230.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7a0c39adc8ab8d60ef13afcbc428c6009d7a3c9d772cc7430dd403808522eeb
|
|
| MD5 |
b940b0b549331df290e80d2578258d7d
|
|
| BLAKE2b-256 |
f13fb4ba8a52ce53c4431bc056e2ca5fc27ff9c71d8353342bd2c779bbae972d
|
File details
Details for the file pydantic_visualizer-0.4.0-py3-none-any.whl.
File metadata
- Download URL: pydantic_visualizer-0.4.0-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.22
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c8e803ae990ad5a4717ac74c1db4337602f7d9f5c913dafe0f54dd48689b726f
|
|
| MD5 |
1cd59f07469ce3b11a9c7f59b6ce2320
|
|
| BLAKE2b-256 |
455da21f0b1136a1a1a81c276be00538ca22182c3363f5c50f740aec885fa086
|