Advanced Usage Example

The following script demonstrates complex directed acyclic graphs (DAGs), multiple export formats, and the use of the render method for high-level visualization orchestration.

This script (available as examples/advanced_usage.py) shows how to: - Build a more complex graph structure. - Export to multiple formats (Mermaid, Graphviz, D2, PlantUML). - Use high-level orchestration for visualization.

Command

uv run examples/advanced_usage.py --output-dir docs/_static/examples > docs/_static/examples/advanced_usage_output.txt

Script

examples/advanced_usage.py
  1from graphable.graph import Graph
  2from graphable.graphable import Graphable
  3from graphable.views.mermaid import MermaidStylingConfig, create_topology_mermaid_mmd
  4
  5
  6def build_complex_project():
  7    """
  8    Simulate a complex software project with multiple subsystems.
  9
 10    Structure:
 11    - Backend: Database -> API -> Worker
 12    - Frontend: API -> Web UI, Mobile App
 13    - Infrastructure: Monitoring, Logging (depend on all)
 14    """
 15    # 1. Core Services
 16    db = Graphable("Database Cluster")
 17    api = Graphable("API Gateway")
 18    worker = Graphable("Background Worker")
 19
 20    # 2. Frontend Applications
 21    web = Graphable("React Web App")
 22    mobile = Graphable("Flutter Mobile App")
 23
 24    # 3. Observability
 25    logging = Graphable("ELK Logging")
 26    monitoring = Graphable("Prometheus/Grafana")
 27
 28    # Build Graph
 29    g = Graph()
 30
 31    # Backend dependencies
 32    g.add_edge(db, api, label="provides data")
 33    g.add_edge(api, worker, label="enqueues tasks")
 34
 35    # Frontend dependencies
 36    g.add_edge(api, web, label="REST API")
 37    g.add_edge(api, mobile, label="gRPC")
 38
 39    # Observability (depends on everything)
 40    for node in [db, api, worker, web, mobile]:
 41        g.add_edge(node, logging, label="logs")
 42        g.add_edge(node, monitoring, label="metrics")
 43
 44    # Metadata for analysis
 45    db.duration = 15.0
 46    api.duration = 5.0
 47    worker.duration = 10.0
 48    web.duration = 3.0
 49    mobile.duration = 4.0
 50
 51    # Tags for visualization clustering
 52    for n in [db, api, worker]:
 53        n.add_tag("backend")
 54    for n in [web, mobile]:
 55        n.add_tag("frontend")
 56    for n in [logging, monitoring]:
 57        n.add_tag("ops")
 58
 59    return g, db, api, worker, web, mobile
 60
 61
 62def main():
 63    print("--- Graphable Advanced Usage Demo ---")
 64    g, db, api, worker, web, mobile = build_complex_project()
 65
 66    print(f"Total project nodes: {len(g)}")
 67    print(f"Sinks (Final Deliverables): {[n.reference for n in g.sinks]}")
 68
 69    # 1. Critical Path Analysis
 70    print("\n--- 1. Critical Path Analysis ---")
 71    cp = g.critical_path()
 72    print(f"Critical Path: {' -> '.join([n.reference for n in cp])}")
 73
 74    analysis = g.cpm_analysis()
 75    max_duration = max(v["EF"] for v in analysis.values())
 76    print(f"Estimated Project Duration: {max_duration} units")
 77
 78    # 2. Impact Analysis (Downstream)
 79    print("\n--- 2. Impact Analysis ---")
 80    print("What is impacted if the 'API Gateway' changes?")
 81    impacted = list(g.descendants(api))
 82    for n in impacted:
 83        print(f"  - {n.reference}")
 84
 85    # 3. Traceability (Upstream)
 86    print("\n--- 3. Traceability Analysis ---")
 87    print("What does the 'Mobile App' depend on?")
 88    deps = list(g.ancestors(mobile))
 89    for n in deps:
 90        print(f"  - {n.reference}")
 91
 92    # 4. Filtered Subgraphs
 93    print("\n--- 4. Backend Focus Subgraph ---")
 94    backend_g = g.subgraph_tagged("backend")
 95    print(f"Nodes in backend subgraph: {[n.reference for n in backend_g]}")
 96
 97    # 5. Visualizing the full picture
 98    print("\n--- 5. Mermaid Definition (Clustered) ---")
 99    config = MermaidStylingConfig(cluster_by_tag=True)
100    print(g.render(create_topology_mermaid_mmd, config=config))
101
102    # 6. Saving to different formats
103    print("\n--- 6. Exporting to multiple formats ---")
104    print("Export logic ready (write() calls commented out in demo).")
105
106
107if __name__ == "__main__":
108    main()

Output

Running the script produces the following text output: