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 Cluster999ccba032b64baeb1bbaa8af2634022dnsb5f85636ac2f42cea139f5403ae30cd2lb999ccba032b64baeb1bbaa8af2634022->b5f85636ac2f42cea139f5403ae30cd29cd2ab797580424681c55b8e0f2cee4eweb1b5f85636ac2f42cea139f5403ae30cd2->9cd2ab797580424681c55b8e0f2cee4ed1363115549146c99e6a7353c83a5a64web2b5f85636ac2f42cea139f5403ae30cd2->d1363115549146c99e6a7353c83a5a646de90816dd9c420490adad82155b80f4web3b5f85636ac2f42cea139f5403ae30cd2->6de90816dd9c420490adad82155b80f413880f283b39453eb25868055662ccd4userdb9cd2ab797580424681c55b8e0f2cee4e->13880f283b39453eb25868055662ccd4f825f0341d014b95af3eb444119d15b5memcached9cd2ab797580424681c55b8e0f2cee4e->f825f0341d014b95af3eb444119d15b5d1363115549146c99e6a7353c83a5a64->13880f283b39453eb25868055662ccd4d1363115549146c99e6a7353c83a5a64->f825f0341d014b95af3eb444119d15b56de90816dd9c420490adad82155b80f4->13880f283b39453eb25868055662ccd46de90816dd9c420490adad82155b80f4->f825f0341d014b95af3eb444119d15b5d59bcba61ee449eab988ec09d8d8bb90userdb ro13880f283b39453eb25868055662ccd4->d59bcba61ee449eab988ec09d8d8bb90

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 HA8a3e10c4a07e498687a2a93754d00daeingressa050879db08842a4a2d4ac74d6e285d7grpc18a3e10c4a07e498687a2a93754d00dae->a050879db08842a4a2d4ac74d6e285d78af732859f0043399403f2d235a63b0fgrpc28a3e10c4a07e498687a2a93754d00dae->8af732859f0043399403f2d235a63b0f8894a1b948374de19e938a4241b51981grpc38a3e10c4a07e498687a2a93754d00dae->8894a1b948374de19e938a4241b5198100e8ce5b919c448f99f221f1c34277d6metricb59a62b7d6f54f7a81a3a5e8e30e0babmonitoring00e8ce5b919c448f99f221f1c34277d6->b59a62b7d6f54f7a81a3a5e8e30e0bab8440f785d519460c92fd5e6a49912beasessiona050879db08842a4a2d4ac74d6e285d7->8440f785d519460c92fd5e6a49912bea4af8a31f38de4caca89779095505ef3eusersa050879db08842a4a2d4ac74d6e285d7->4af8a31f38de4caca89779095505ef3e0068fe71f3bd42e4bce67b7daaf87896logginga050879db08842a4a2d4ac74d6e285d7->0068fe71f3bd42e4bce67b7daaf878968af732859f0043399403f2d235a63b0f->8440f785d519460c92fd5e6a49912bea8af732859f0043399403f2d235a63b0f->4af8a31f38de4caca89779095505ef3e8af732859f0043399403f2d235a63b0f->0068fe71f3bd42e4bce67b7daaf878968894a1b948374de19e938a4241b51981->8440f785d519460c92fd5e6a49912bea8894a1b948374de19e938a4241b51981->4af8a31f38de4caca89779095505ef3e8894a1b948374de19e938a4241b51981->0068fe71f3bd42e4bce67b7daaf87896fbbc5ed7c04745b6b8a5cc3563ff8897replica8440f785d519460c92fd5e6a49912bea->fbbc5ed7c04745b6b8a5cc3563ff8897fbbc5ed7c04745b6b8a5cc3563ff8897->00e8ce5b919c448f99f221f1c34277d6collect33e1e7a140be44f08fa5612bf197135areplica4af8a31f38de4caca89779095505ef3e->33e1e7a140be44f08fa5612bf197135a33e1e7a140be44f08fa5612bf197135a->00e8ce5b919c448f99f221f1c34277d6collectbad2e8090fe24aa4b403193da11f227dstream0068fe71f3bd42e4bce67b7daaf87896->bad2e8090fe24aa4b403193da11f227dparse8e507293055c4d48a57fd3ab64b62684analyticsbad2e8090fe24aa4b403193da11f227d->8e507293055c4d48a57fd3ab64b62684

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