Skip to main content

d4-diag

A powerful Python code analysis tool that generates interactive Mermaid diagrams to visualize your project's structure, dependencies, and architecture.

🚀 Overview

d4-diag analyzes Python codebases and generates comprehensive diagrams showing module dependencies, class relationships, and code structure. Perfect for understanding complex projects, documenting architecture, and onboarding new developers.

✨ Key Features

  • 🔍 Comprehensive Analysis: Parses Python AST to understand functions, classes, imports, and relationships
  • 📁 Smart File Discovery: Automatically finds all Python files while excluding virtual environments and caches
  • 🎨 Multiple Diagram Types: Generate architecture, class, and module dependency diagrams
  • 🖥️ Modern CLI Interface: Clean, intuitive command-line interface with helpful error messages
  • 🌐 Interactive Viewer: Built-in HTML viewer for exploring diagrams with tabs and navigation
  • ⚡ High Performance: Handles large projects efficiently with size limits and error recovery
  • 🛡️ Robust Error Handling: Gracefully handles syntax errors, encoding issues, and file system problems
  • 🔄 Backward Compatible: Works with legacy command patterns while supporting modern CLI usage

📦 Installation

pip install d4-diag

Requires Python 3.8+. Verify the install:

d4-diag --version

🎯 Usage

# Analyze a project directory
d4-diag analyze ./src

# Analyze with verbose output
d4-diag analyze ./src --verbose

# Specify custom output directory
d4-diag analyze ./src --output-dir ./docs/diagrams

# Analyze multiple paths
d4-diag analyze ./src ./tests --verbose

# View generated diagrams
d4-diag viewer ./docs/diagrams

# View without opening browser
d4-diag viewer ./docs/diagrams --no-browser

Run as Python Module

python -m d4_diag analyze ./src
python -m d4_diag viewer ./docs/diagrams

Get Help

d4-diag --help
d4-diag analyze --help
d4-diag viewer --help

📊 Generated Diagrams

d4-diag generates three comprehensive diagrams in your project's docs/diagrams directory:

1. Architecture Diagram (architecture.mmd)

Shows the overall project structure with files, classes, and functions as nodes.

2. Class Diagram (class_diagram.mmd)

Displays class relationships, inheritance hierarchies, and method definitions.

3. Module Dependencies (module_deps.mmd)

Illustrates import relationships and module dependencies across your codebase.

🖥️ Interactive Viewer

The built-in viewer provides an elegant way to explore your diagrams:

# Launch interactive viewer
d4-diag viewer ./docs/diagrams

# Features:
# - Tabbed interface for multiple diagrams
# - Responsive design
# - Mermaid.js rendering
# - Easy navigation

📋 Requirements

  • Python: 3.8+ (tested on 3.8-3.14)
  • pip: For installation
  • Dependencies are installed automatically with pip install d4-diag

🎨 Examples

Basic Project Analysis

$ d4-diag analyze ./src --verbose
Project root: /path/to/project
Auto-detected project root: /path/to/project
Scanning directory: src
  Found 5 Python files
Analyzing 5 file(s)...
  Processing: src/main.py
  Processing: src/utils.py
  Processing: src/models/user.py
  Processing: src/services/auth.py
  Processing: src/generate_mermaid.py

=== Code Map Summary ===
Files analyzed:   5
Classes found:    3
Functions found:  12
Import links:     4

Generating diagrams in: /path/to/project/docs/diagrams
Done! View diagrams with: d4-diag viewer /path/to/project/docs/diagrams

Sample Generated Diagram

graph LR
    classDef classNode fill:#1565C0,stroke:#0D47A1,color:white;
    classDef funcStyle fill:#2E7D32,stroke:#1B5E20,color:white;

    subgraph src_main_py["main.py"]
        main[main]:::funcStyle
        analyze[analyze]:::funcStyle
    end

    subgraph src_utils_py["utils.py"]
        find_files[find_python_files]:::funcStyle
        sanitize[sanitize_id]:::funcStyle
    end

    main --> analyze
    analyze --> find_files

🛠️ Advanced Usage

Custom Project Root

# For monorepos or complex structures
d4-diag analyze ./packages/myapp --project-root ./packages/myapp

Output Directory Customization

# Custom output location
d4-diag analyze ./src --output-dir ./documentation/diagrams

Excluding Files and Directories

d4-diag automatically excludes:

  • Virtual environments (.venv, venv, .env)
  • Cache directories (__pycache__, .pytest_cache)
  • Version control (.git, .hg)
  • Build artifacts (dist, build, node_modules)
  • Package directories (site-packages, *.egg-info)

🔍 Troubleshooting

Common Issues

"No Python files found!"

  • Ensure you're pointing to a directory with .py files
  • Check that files aren't in excluded directories

"Permission denied" errors

  • Check file permissions on your source code
  • Ensure virtual environment has read access

Large project performance

  • d4-diag automatically skips files >10MB
  • Use --verbose to see processing progress

Getting Help

# Get help for commands
d4-diag --help
d4-diag analyze --help
d4-diag viewer --help

# Check version
d4-diag --version

📈 Performance

  • Memory Efficient: Processes files incrementally
  • Size Limits: Automatically skips files >10MB
  • Error Recovery: Continues analysis after individual file errors
  • Fast Scanning: Efficient directory traversal with exclusions

🤝 Contributing

We welcome contributions! Please see our Contributing Guide for details.

📄 License

MIT License - see LICENSE file for details.

🙏 Acknowledgments

  • Mermaid.js: For excellent diagram rendering
  • Click: For the beautiful CLI framework
  • pytest: For the robust testing framework

d4-diag: Understand your codebase at a glance. 🎯

Metadata

Release files for d4-diag 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for d4-diag 0.1.4
File Size Uploaded Provenance
d4_diag-0.1.4.tar.gz 17.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for d4-diag 0.1.4
File Interpreter ABI Platform
d4_diag-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 34.9 kB

Release files / d4_diag-0.1.4.tar.gz

Download URL d4_diag-0.1.4.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
3c2358bddb25826e58f94ba12c19669ee0989af735b8add59f0cd563b61c63ad
BLAKE2b-256 checksum
How to use checksums
2d32756ba5a880f76a8c787e7276d654fc1385861c899914ab06728317433356
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Release files / d4_diag-0.1.4-py3-none-any.whl

Download URL d4_diag-0.1.4-py3-none-any.whl
Size 17.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2928489db330bd620dbb513d020bdaf75870697c0423f4132341809c06a38410
BLAKE2b-256 checksum
How to use checksums
c8240ea364d16ceb384b997fce1851e5678226e348766669452f87f74759410c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.11

Security for d4-diag 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page