Skip to content

Diagrams Rendering Support¤

Diagram rendering support with svg mode.

Requirements¤

pip install diagrams

Example¤

Set up your defaults, typically in a silent block:

# graphviz setup
 gv = {
     "fontsize": "17",
     "fontcolor": "gray",
     "labelfontcolor": "white",
     "bgcolor": "transparent",
 }
 
 def diagr(**kw):
     '''factory creating same style Diagram context mgr objects'''
     from diagrams import Diagram
     setup = dict(show=False, outformat='svg', filename='tmpf',
                  graph_attr=gv,
                  node_attr=gv,
                  edge_attr=gv)
     setup.update(kw)
     return Diagram(**setup)
 
 def T(typ, *a, **kw):
     kw['graph_attr'] = gv
     return typ(*a, **kw)
  • Note the outformat is svg
  • No limits regarding sophistication - the factory approach is just an example

Then use the library like normal, added our show based renderer at the end:

LP Source:

 ```python lp:python addsrc eval=always session=gvdiag
 from diagrams import Cluster, Diagram
 from diagrams.aws.compute import ECS
 from diagrams.aws.database import ElastiCache, RDS
 from diagrams.aws.network import ELB
 from diagrams.aws.network import Route53
 
 with diagr(name="Clustered Web Services") as d:
     dns = Route53("dns")
     lb = ELB("lb")
 
     with Cluster("Services"):
         svc_group = [ECS("web1"),
                      ECS("web2"),
                      ECS("web3")]
 
     with Cluster("DB Cluster"):
         db_main = RDS("userdb")
         db_main - [RDS("userdb ro")]
 
     memcached = ElastiCache("memcached")
 
     dns >> lb >> svc_group
     svc_group >> db_main
     svc_group >> memcached
 show(d)
 ```

Result:

Clustered Web ServicesClustered Web Servicescluster_ServicesServicescluster_DB ClusterDB Cluster5ad92c7896d0492b927941a46ec0188bdns60bbcd9a5452478ca0caf7490f4f117blb5ad92c7896d0492b927941a46ec0188b->60bbcd9a5452478ca0caf7490f4f117b8b25a1b49ee74d2b86c75c7d6e8591b9web160bbcd9a5452478ca0caf7490f4f117b->8b25a1b49ee74d2b86c75c7d6e8591b9e504fd22ee3f4ba19eae3e3c302792d5web260bbcd9a5452478ca0caf7490f4f117b->e504fd22ee3f4ba19eae3e3c302792d57d7ca99aed3d4d0a89d9d94ea5e28b9cweb360bbcd9a5452478ca0caf7490f4f117b->7d7ca99aed3d4d0a89d9d94ea5e28b9c4eac1d52bd14474da8624e8945ac45fbuserdb8b25a1b49ee74d2b86c75c7d6e8591b9->4eac1d52bd14474da8624e8945ac45fb6791ea50a9514875b73ffc399dbadb35memcached8b25a1b49ee74d2b86c75c7d6e8591b9->6791ea50a9514875b73ffc399dbadb35e504fd22ee3f4ba19eae3e3c302792d5->4eac1d52bd14474da8624e8945ac45fbe504fd22ee3f4ba19eae3e3c302792d5->6791ea50a9514875b73ffc399dbadb357d7ca99aed3d4d0a89d9d94ea5e28b9c->4eac1d52bd14474da8624e8945ac45fb7d7ca99aed3d4d0a89d9d94ea5e28b9c->6791ea50a9514875b73ffc399dbadb35a3be245b510842d7a0638affe1c08230userdb ro4eac1d52bd14474da8624e8945ac45fb->a3be245b510842d7a0638affe1c08230

Inline Colors¤

Advanced Web Service with On-Premise (colored)Advanced Web Service with On-Premise (colored)cluster_Service ClusterService Clustercluster_Sessions HASessions HAcluster_Database HADatabase HAb20a012da94748b8815348e826ea2583ingressc550506766a240b98710300829e70c99grpc1b20a012da94748b8815348e826ea2583->c550506766a240b98710300829e70c998ebd1a84fd8849998355899a8bc0b543grpc2b20a012da94748b8815348e826ea2583->8ebd1a84fd8849998355899a8bc0b543efbb6c4666f74ab695357c205134ae0egrpc3b20a012da94748b8815348e826ea2583->efbb6c4666f74ab695357c205134ae0e8aec0e9415f044deaa4f26d77becf024metric4fd6cebd6fab42aa9dcf59d497ef6489monitoring8aec0e9415f044deaa4f26d77becf024->4fd6cebd6fab42aa9dcf59d497ef6489fefb52d8960a4783808252055dda77d8sessionc550506766a240b98710300829e70c99->fefb52d8960a4783808252055dda77d85d001ea5555f4647a57b978cdc5d31f9usersc550506766a240b98710300829e70c99->5d001ea5555f4647a57b978cdc5d31f96aa9e4a8f24340059526aefd7eb0a15dloggingc550506766a240b98710300829e70c99->6aa9e4a8f24340059526aefd7eb0a15d8ebd1a84fd8849998355899a8bc0b543->fefb52d8960a4783808252055dda77d88ebd1a84fd8849998355899a8bc0b543->5d001ea5555f4647a57b978cdc5d31f98ebd1a84fd8849998355899a8bc0b543->6aa9e4a8f24340059526aefd7eb0a15defbb6c4666f74ab695357c205134ae0e->fefb52d8960a4783808252055dda77d8efbb6c4666f74ab695357c205134ae0e->5d001ea5555f4647a57b978cdc5d31f9efbb6c4666f74ab695357c205134ae0e->6aa9e4a8f24340059526aefd7eb0a15da8f8cf5363ce43be9219c88d7b85ad68replicafefb52d8960a4783808252055dda77d8->a8f8cf5363ce43be9219c88d7b85ad68a8f8cf5363ce43be9219c88d7b85ad68->8aec0e9415f044deaa4f26d77becf024collect911bc009b4f846fe985f9ec37e43d375replica5d001ea5555f4647a57b978cdc5d31f9->911bc009b4f846fe985f9ec37e43d375911bc009b4f846fe985f9ec37e43d375->8aec0e9415f044deaa4f26d77becf024collect663cfbf7a1444b9f8ef4d84c4b6cbb6fstream6aa9e4a8f24340059526aefd7eb0a15d->663cfbf7a1444b9f8ef4d84c4b6cbb6fparse9615560f1e6944afb8acaa43f0fd3aacanalytics663cfbf7a1444b9f8ef4d84c4b6cbb6f->9615560f1e6944afb8acaa43f0fd3aac

 
 ```python lp:python addsrc="Cloud Computing Example" eval=always session=gvdiag
 from diagrams import Cluster, Edge
 from diagrams.onprem.analytics import Spark
 from diagrams.onprem.compute import Server
 from diagrams.onprem.database import PostgreSQL
 from diagrams.onprem.inmemory import Redis
 from diagrams.onprem.aggregator import Fluentd
 from diagrams.onprem.monitoring import Grafana, Prometheus
 from diagrams.onprem.network import Nginx
 from diagrams.onprem.queue import Kafka
 
 
 with diagr(name="Advanced Web Service with On-Premise (colored)") as diag:
     ingress = Nginx("ingress")
 
     metrics = Prometheus("metric")
     metrics << Edge(color="firebrick", style="dashed") << Grafana("monitoring")
 
     with T(Cluster, "Service Cluster"):
         grpcsvc = [
             Server("grpc1", fontcolor="black"),
             Server("grpc2"),
             Server("grpc3")]
 
     with T(Cluster,"Sessions HA"):
         main = Redis("session")
         main - Edge(color="brown", style="dashed") - Redis("replica") << Edge(label="collect") << metrics
         grpcsvc >> Edge(color="brown") >> main
 
     with T(Cluster, "Database HA"):
         main = PostgreSQL("users")
         main - Edge(color="brown", style="dotted") - PostgreSQL("replica") << Edge(label="collect") << metrics
         grpcsvc >> Edge(color="black") >> main
 
     aggregator = Fluentd("logging")
     aggregator >> Edge(label="parse") >> Kafka("stream") >> Edge(color="black", style="bold") >> Spark("analytics")
 
     ingress >> Edge(color="darkgreen") << grpcsvc >> Edge(color="darkorange") >> aggregator
 
 show(diag) # this is passed into the plugin rendering function
 ```

Tech¤

The challenge here is to get svgs displayable within your docs.

They normally contain xlinked png images, pointing to your diagram package in site-directory - i.e. not served by a static documentation server.

Solution:

Through a patched icon loader, we detect which png icons your diagram requires and copy them over to docsdir/icons_diagram. You can git-ignore that directory, so that images do not pile up there.

Back to top