Skip to main content

Python code visualization tool that generates interactive Mermaid diagrams from static analysis

Project description

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. 🎯

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

d4_diag-0.1.4.tar.gz (17.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

d4_diag-0.1.4-py3-none-any.whl (17.7 kB view details)

Uploaded Python 3

File details

Details for the file d4_diag-0.1.4.tar.gz.

File metadata

  • Download URL: d4_diag-0.1.4.tar.gz
  • Upload date:
  • Size: 17.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for d4_diag-0.1.4.tar.gz
Algorithm Hash digest
SHA256 3c2358bddb25826e58f94ba12c19669ee0989af735b8add59f0cd563b61c63ad
MD5 727f53097c0d2c626ea9b52eecaf2d7d
BLAKE2b-256 2d32756ba5a880f76a8c787e7276d654fc1385861c899914ab06728317433356

See more details on using hashes here.

File details

Details for the file d4_diag-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: d4_diag-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 17.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for d4_diag-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2928489db330bd620dbb513d020bdaf75870697c0423f4132341809c06a38410
MD5 0915a29bc00d264c0e72107ec563cc86
BLAKE2b-256 c8240ea364d16ceb384b997fce1851e5678226e348766669452f87f74759410c

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