{"id":1016,"date":"2021-03-14T22:02:48","date_gmt":"2021-03-14T20:02:48","guid":{"rendered":"http:\/\/www.netexpertise.eu\/en\/?p=1016"},"modified":"2021-10-17T20:19:44","modified_gmt":"2021-10-17T18:19:44","slug":"draw-beautiful-diagrams-with-diagram-as-code","status":"publish","type":"post","link":"http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html","title":{"rendered":"Draw Beautiful Diagrams with Diagram as Code"},"content":{"rendered":"\n<p>Diagram as Code is a hot topic these days that brings a lof of benefits. Among them:<br>&#8211; Keep track of changes, who and what<br>&#8211; Store on a repository as simple text<br>&#8211; No need to realign arrows or move things around when you add an item<br>&#8211; Much faster as a consequence once you have the knowledge<br>&#8211; It is Code, and you know how much devs are fond of making manual diagrams<br>&#8211; Simply beautiful, everything is well-aligned<br>&#8211; <a href=\"\/en\/code\/python\/show-all-available-icons-in-diagram-as-code.html\">many icons<\/a> available<\/p>\n\n\n\n<p><br>An open source diagram library is available on <a href=\"https:\/\/diagrams.mingrammer.com\/docs\/getting-started\/installation\">diagrams.mingrammer.com<\/a> in Python and Go. I will not go through the installation steps, everything is explained in the documentation. They also provide good diagrams examples.<br>I will try to translate an old diagram made on Microsoft Visio back then to a diagram as code. This image comes from a <a href=\"\/en\/database\/mysql\/mysql-replication.html\">Mysql replication post<\/a>.<br><br><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"\/images\/Replication.png\" alt=\"\"\/><\/figure><\/div>\n\n\n\n<p><br>Here&#8217;s the code I came up with and the generated image:<\/p>\n\n\n\n<pre title=\"\" class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">from diagrams import Cluster, Diagram, Edge\nfrom diagrams.azure.compute import *\nfrom diagrams.azure.database import *\n \nwith Diagram(\"Replication\", show=\"False\", direction='TB'):\n \n&nbsp; slaves = []\n&nbsp; clients= []\n \n&nbsp; master = SQLServers(\"Master\")\n&nbsp; with Cluster(\"slaves\"):\n&nbsp; &nbsp; for s in range(3):\n&nbsp; &nbsp; &nbsp; slave = SQLDatabases(\"slave\"+str(s+1))\n  &nbsp; &nbsp; slaves.append(slave)\n \n &nbsp;for c in range(3):\n &nbsp;&nbsp; client = VM(\"client\"+str(c+1))\n &nbsp;&nbsp; clients.append(client)\n &nbsp;&nbsp; clients[c] &gt;&gt; Edge(color=\"darkgreen\",label=\"Reads\") &gt;&gt; slaves[c]\n &nbsp; &nbsp;\n &nbsp;clients &gt;&gt; Edge(color=\"red\",label=\"Writes\") &gt;&gt; master\n &nbsp;master &gt;&gt; Edge(color=\"blue\",label=\"Replication\") &gt;&gt; slaves[1]\n<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-gallery columns-2 is-cropped wp-block-gallery-1 is-layout-flex\"><ul class=\"blocks-gallery-grid\"><li class=\"blocks-gallery-item\"><figure><img decoding=\"async\" loading=\"lazy\" width=\"614\" height=\"796\" src=\"http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_ReplicationAllReads.png\" alt=\"\" data-id=\"1112\" data-full-url=\"http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_ReplicationAllReads.png\" data-link=\"http:\/\/www.netexpertise.eu\/en\/diagram_as_code_replicationallreads\" class=\"wp-image-1112\" srcset=\"http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_ReplicationAllReads.png 614w, http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_ReplicationAllReads-463x600.png 463w\" sizes=\"(max-width: 614px) 100vw, 614px\" \/><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img decoding=\"async\" loading=\"lazy\" width=\"901\" height=\"961\" src=\"http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_Replication2Reads.png\" alt=\"\" data-id=\"1111\" data-full-url=\"http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_Replication2Reads.png\" data-link=\"http:\/\/www.netexpertise.eu\/en\/diagram_as_code_replication2reads\" class=\"wp-image-1111\" srcset=\"http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_Replication2Reads.png 901w, http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_Replication2Reads-563x600.png 563w, http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_Replication2Reads-768x819.png 768w\" sizes=\"(max-width: 901px) 100vw, 901px\" \/><\/figure><\/li><\/ul><\/figure>\n\n\n\n<p>Less than 20 lines of code, that&#8217;s all it takes. Icons are from Azure directory but I could have picked some others. The second image is basically the same thing but I removed the direction that becomes Left to Right. Labels are better handled in that direction as you can see, even if it&#8217;s still not perfect with multiple arrows.<br>I also removed the middle &#8220;Read&#8221; label for a better alignment and clarity linking single objects rather than arrays. I replaced<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">clients[c] &gt;&gt; Edge(color=\"darkgreen\",label=\"Reads\") &gt;&gt; slaves[c]<\/code><\/pre>\n\n\n\n<p><br>with (outside the for loop):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"python\" class=\"language-python\">for i in [0,2]: clients[i] &gt;&gt; Edge(color=\"darkgreen\",label=\"Reads\") &gt;&gt; slaves[i]<\/code><\/pre>\n\n\n\n<p><br>It is sometimes a bit difficult to get what you want especially with multiple levels connecting to each other. Now you can experiment and change some behaviours playing around with Cluster grouping. It can be handy when you want Level 2 and 4 on the same place for instance but it can also get messy like in this Mysql Cluster:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-medium is-resized\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_Replication_Mysql_Cluster-601x600.png\" alt=\"\" class=\"wp-image-1110\" width=\"323\" height=\"322\" srcset=\"http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_Replication_Mysql_Cluster-601x600.png 601w, http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_Replication_Mysql_Cluster-150x150.png 150w, http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_Replication_Mysql_Cluster-768x767.png 768w, http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_Replication_Mysql_Cluster.png 854w\" sizes=\"(max-width: 323px) 100vw, 323px\" \/><\/figure><\/div>\n\n\n\n<p><br>In a nutshell, this implementation of Diagram as Code is powerful and efficient even though you lose some control on placing things. A small downside for a great library. Once you get familiar with all these little tricks, you can achieve some nice and beautiful diagrams. Here&#8217;s a basic Kubernetes architecture example:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><a href=\"\/uploads\/Diagram_as_code_Kubernetes.png\"><img decoding=\"async\" src=\"\/uploads\/Diagram_as_code_Kubernetes-742x1024.png\" alt=\"Kubernetes diagram as code\" width=\"650\" title=\"Kubernetes diagram as code\"\/><\/a><\/figure><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Diagram as Code is a hot topic these days that brings a lof of benefits. Among them:&#8211; Keep track of changes, who and what&#8211; Store on a repository as simple text&#8211; No need to realign arrows or move things around when you add an item&#8211; Much faster as a consequence once you have the knowledge&#8211; [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[431],"tags":[432,414,19],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Netexpertise - Draw Beautiful Diagrams with Diagram as Code<\/title>\n<meta name=\"description\" content=\"Diagram as Code is the new hot topic that lets you draw beautiful diagrams in Python language. Here&#039;s how to start\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Netexpertise - Draw Beautiful Diagrams with Diagram as Code\" \/>\n<meta property=\"og:description\" content=\"Diagram as Code is the new hot topic that lets you draw beautiful diagrams in Python language. Here&#039;s how to start\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html\" \/>\n<meta property=\"og:site_name\" content=\"Netexpertise\" \/>\n<meta property=\"article:published_time\" content=\"2021-03-14T20:02:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-10-17T18:19:44+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_ReplicationAllReads.png\" \/>\n<meta name=\"author\" content=\"dave\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@netexpertise\" \/>\n<meta name=\"twitter:site\" content=\"@netexpertise\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html\",\"url\":\"http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html\",\"name\":\"Netexpertise - Draw Beautiful Diagrams with Diagram as Code\",\"isPartOf\":{\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#website\"},\"datePublished\":\"2021-03-14T20:02:48+00:00\",\"dateModified\":\"2021-10-17T18:19:44+00:00\",\"author\":{\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa\"},\"description\":\"Diagram as Code is the new hot topic that lets you draw beautiful diagrams in Python language. Here's how to start\",\"breadcrumb\":{\"@id\":\"http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.netexpertise.eu\/en\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Draw Beautiful Diagrams with Diagram as Code\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#website\",\"url\":\"https:\/\/www.netexpertise.eu\/en\/\",\"name\":\"Netexpertise\",\"description\":\"Systems \/ Networks \/ DevOps\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.netexpertise.eu\/en\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa\",\"name\":\"dave\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g\",\"caption\":\"dave\"},\"sameAs\":[\"http:\/\/www.netexpertise.eu\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Netexpertise - Draw Beautiful Diagrams with Diagram as Code","description":"Diagram as Code is the new hot topic that lets you draw beautiful diagrams in Python language. Here's how to start","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html","og_locale":"en_US","og_type":"article","og_title":"Netexpertise - Draw Beautiful Diagrams with Diagram as Code","og_description":"Diagram as Code is the new hot topic that lets you draw beautiful diagrams in Python language. Here's how to start","og_url":"http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html","og_site_name":"Netexpertise","article_published_time":"2021-03-14T20:02:48+00:00","article_modified_time":"2021-10-17T18:19:44+00:00","og_image":[{"url":"http:\/\/www.netexpertise.eu\/en\/wp-content\/uploads\/Diagram_as_code_ReplicationAllReads.png"}],"author":"dave","twitter_card":"summary_large_image","twitter_creator":"@netexpertise","twitter_site":"@netexpertise","schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html","url":"http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html","name":"Netexpertise - Draw Beautiful Diagrams with Diagram as Code","isPartOf":{"@id":"https:\/\/www.netexpertise.eu\/en\/#website"},"datePublished":"2021-03-14T20:02:48+00:00","dateModified":"2021-10-17T18:19:44+00:00","author":{"@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa"},"description":"Diagram as Code is the new hot topic that lets you draw beautiful diagrams in Python language. Here's how to start","breadcrumb":{"@id":"http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.netexpertise.eu\/en\/code\/python\/draw-beautiful-diagrams-with-diagram-as-code.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.netexpertise.eu\/en"},{"@type":"ListItem","position":2,"name":"Draw Beautiful Diagrams with Diagram as Code"}]},{"@type":"WebSite","@id":"https:\/\/www.netexpertise.eu\/en\/#website","url":"https:\/\/www.netexpertise.eu\/en\/","name":"Netexpertise","description":"Systems \/ Networks \/ DevOps","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.netexpertise.eu\/en\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/cb4cd666549d22e9070ec1cfc1a496fa","name":"dave","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.netexpertise.eu\/en\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/1129916e1f4955bd632f27f836f64e55?s=96&d=mm&r=g","caption":"dave"},"sameAs":["http:\/\/www.netexpertise.eu"]}]}},"_links":{"self":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts\/1016"}],"collection":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/comments?post=1016"}],"version-history":[{"count":0,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/posts\/1016\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/media?parent=1016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/categories?post=1016"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.netexpertise.eu\/en\/wp-json\/wp\/v2\/tags?post=1016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}