Skip to main content

Visualize your Python code execution flow as Mermaid Sequence Diagrams.

Project description

MermaidTrace: The Python Logger That Draws Diagrams

PyPI version Python Versions License CI Status Codecov

Stop reading logs. Start watching them.

MermaidTrace is a specialized logging tool that automatically generates Mermaid JS sequence diagrams from your code execution. It's perfect for visualizing complex business logic, microservice interactions, or asynchronous flows.

✨ Features

  • Decorator-Driven: Just add @trace or @trace_interaction to your functions.
  • Auto-Diagramming: Generates .mmd files that can be viewed in VS Code, GitHub, or Mermaid Live Editor.
  • Async Support: Works seamlessly with asyncio coroutines.
  • Context Inference: Automatically tracks nested calls and infers source participants using contextvars.
  • FastAPI Integration: Includes middleware for zero-config HTTP request tracing.
  • CLI Tool: Built-in viewer to preview diagrams in your browser.

🚀 Quick Start

Installation

pip install mermaid-trace

Basic Usage

from mermaid_trace import trace, configure_flow
import time

# 1. Configure output
configure_flow("my_flow.mmd")

# 2. Add decorators
@trace(source="Client", target="PaymentService", action="Process Payment")
def process_payment(amount):
    if check_balance(amount):
        return "Success"
    return "Failed"

@trace(source="PaymentService", target="Database", action="Check Balance")
def check_balance(amount):
    return True

# 3. Run your code
process_payment(100)

Nested Calls (Context Inference)

You don't need to specify source every time. MermaidTrace infers it from the current context.

@trace(source="Client", target="API")
def main():
    # Inside here, current participant is "API"
    service_call()

@trace(target="Service") # source inferred as "API"
def service_call():
    pass

FastAPI Integration

from fastapi import FastAPI
from mermaid_trace.integrations.fastapi import MermaidTraceMiddleware

app = FastAPI()
app.add_middleware(MermaidTraceMiddleware, app_name="MyAPI")

@app.get("/")
async def root():
    return {"message": "Hello World"}

CLI Viewer

Visualize your generated .mmd files instantly:

mermaid-trace serve my_flow.mmd

📂 Documentation

🤝 Contributing

We welcome contributions! Please see CONTRIBUTING.md for details.

📄 License

MIT

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

mermaid_trace-0.3.1.tar.gz (36.7 kB view details)

Uploaded Source

Built Distribution

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

mermaid_trace-0.3.1-py3-none-any.whl (20.7 kB view details)

Uploaded Python 3

File details

Details for the file mermaid_trace-0.3.1.tar.gz.

File metadata

  • Download URL: mermaid_trace-0.3.1.tar.gz
  • Upload date:
  • Size: 36.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mermaid_trace-0.3.1.tar.gz
Algorithm Hash digest
SHA256 7e9f894a906f5c3419c8653ef75dae83d5295103b84e1f0ec735bcc5264e3719
MD5 fe7a3255495c8a9aba82c463ecf6008a
BLAKE2b-256 401128d36103af6b8b4a60087cb5b8f6d887b0ce680dec5b94a28a8646c370df

See more details on using hashes here.

Provenance

The following attestation bundles were made for mermaid_trace-0.3.1.tar.gz:

Publisher: release.yml on xt765/mermaid-trace

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mermaid_trace-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: mermaid_trace-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 20.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mermaid_trace-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d80a993be4a6341ea4e36feb99215eeda0200719556c5689e149f778d10a0270
MD5 30f596eab8307efca293f60cac9e7dad
BLAKE2b-256 e2cf548bfe7a503f88533a117136470dd834a42a69cdf49676e256982864747f

See more details on using hashes here.

Provenance

The following attestation bundles were made for mermaid_trace-0.3.1-py3-none-any.whl:

Publisher: release.yml on xt765/mermaid-trace

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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