Programmer treasure trove: github.com/Jackpopc/CS…

What is your favorite flowchart drawing tool?

IO, Visio, OmniGraffle and so on, different users will have different choices.

So, have you ever thought about drawing a flow chart using a piece of Python code?

Recently, I noticed a Project on Github called Diagrams, where you can map the architecture of cloud systems in Python code.

It was born to provide prototypes for new system architecture designs without any design tools, and can draw visual system architectures.

Diagrams Currently the main providers include AWS, Azure, GCP, Kubernetes, Ali Cloud, Oracle Cloud, etc.

Diagrams allow you to plot cloud system architectures in Python code. It was born to provide prototypes for new system architecture designs without any design tools. You can also describe or visualize the existing system architecture. Chart currently supports major major providers including: AWS, Azure, GCP, Kubernetes, Ali Cloud, Oracle Cloud, etc. It also supports native nodes, SaaS, and other programming frameworks and languages.

Today, I’ll show you how to use Python code to draw a nice flow chart.

Install the configuration

Environment configuration

Diagrams use Requires Python version 3.6 and above, and GraphViz needs to be installed beforehand, which is primarily used for charting. Next, you need to install the Diagrams toolkit.

Install GraphViz

On Linux, you can install it directly using the package management tool. In Ubuntu, for example,

$ sudo apt install graphviz
Copy the code

In Fedora, Redhat, and CentOS, you can use yum for installation.

Under Windows, Winget and Choco can be used for installation. Take Winget as an example.

$ winget install graphviz
Copy the code

On Mac, Homebrew can be used to install:

$ brew install graphviz
Copy the code

The installation Diagrams

As a Python toolkit, Diagrams are relatively easy to install using package management tools such as PIP, Pipenv, and Poetry.

# using pip (pip3)
$ pip install diagrams

# using pipenv
$ pipenv install diagrams

# using poetry
$ poetry add diagrams
Copy the code

At this point, the Diagrams environment is configured.

use

Component type

Diagrams support Diagrams from different providers of AWS, Azure, GCP, Kubernetes, Ali Cloud, Oracle Cloud.

Therefore, different component types from AWS, Azure, GCP, Kubernetes, Ali Cloud, Oracle Cloud, K8S and so on can be used.

drawing

To create a flowchart, begin by abstracting some key elements of a diagram:

  • Diagrams – Diagrams are the primary object for representing diagrams
  • Node – An abstraction that represents a single system component
  • Groups – allows you to organize nodes into groups rather than isolated components
  • Edges – represent connections between nodes

The four points above are the four key elements in making a chart. In the subsequent drawing process, these elements are put together.

You are now ready to chart in Python!

Step 1: Create a chart workspace

from diagrams import Diagram

with Diagram("Simple Website Diagram") as diag:
    pass
diag
Copy the code

This creates a blank graph with the specified label, as shown below:

Step 2: Add a node

Now that we have our workspace, it’s time to add the nodes needed for the site.

The nodes we are adding come from two different providers, AWS and the OnPrem provider.

from diagrams import Diagram, Cluster
from diagrams.aws.compute import EC2
from diagrams.aws.network import ELB
from diagrams.aws.network import Route53
from diagrams.onprem.database import PostgreSQL # Would typically use RDS from aws.database
from diagrams.onprem.inmemory import Redis # Would typically use ElastiCache from aws.database

with Diagram("Simple Website Diagram") as diag:
    dns = Route53("dns")
    load_balancer = ELB("Load Balancer")
    database = PostgreSQL("User Database")
    cache = Redis("Cache")
    svc_group = [EC2("Webserver 1"),
                 EC2("Webserver 2"),
                 EC2("Webserver 3")]
diag
Copy the code

At this point, you’ll see that it’s no longer a blank chart.

Each of our nodes is plotted, and these are the “elements” of the architecture we are building.

The next step will be to organize some of our nodes into logical groups and then link each node with edges.

Step 3: Grouping nodes

In this example, we will group load-balanced Web servers.

As you can see below, to do this, we simply move the instance of the node to the scope of the cluster we are creating.

from diagrams import Diagram, Cluster
from diagrams.aws.compute import EC2
from diagrams.aws.network import ELB
from diagrams.aws.network import Route53
from diagrams.onprem.database import PostgreSQL # Would typically use RDS from aws.database
from diagrams.onprem.inmemory import Redis # Would typically use ElastiCache from aws.database

with Diagram("Simple Website Diagram") as diag:
    dns = Route53("dns")
    load_balancer = ELB("Load Balancer")
    database = PostgreSQL("User Database")
    cache = Redis("Cache")
    with Cluster("Webserver Cluster"):
        svc_group = [EC2("Webserver 1"),
                    EC2("Webserver 2"),
                    EC2("Webserver 3")]
diag
Copy the code

As you can see, the diagram is still just a list of nodes, but now we cluster the appropriate nodes into logical groups.

Step 4: Connect nodes

Now for the final step, we join the isolated nodes together.

from diagrams import Diagram, Cluster
from diagrams.aws.compute import EC2
from diagrams.aws.network import ELB
from diagrams.aws.network import Route53
from diagrams.onprem.database import PostgreSQL # Would typically use RDS from aws.database
from diagrams.onprem.inmemory import Redis # Would typically use ElastiCache from aws.database

with Diagram("Simple Website Diagram", direction='LR') as diag: # It's LR by default, but you have a few options with the orientation
    dns = Route53("dns")
    load_balancer = ELB("Load Balancer")
    database = PostgreSQL("User Database")
    cache = Redis("Cache")
    with Cluster("Webserver Cluster"):
        svc_group = [EC2("Webserver 1"),
                    EC2("Webserver 2"),
                    EC2("Webserver 3")]
    dns >> load_balancer >> svc_group
    svc_group >> cache
    svc_group >> database
diag
Copy the code

The resulting image can be seen below, and now you can see the logical flow between each node in the figure.

You can change this process by changing the order in which the nodes are defined.

In addition to tweaking the flow, you can change a lot of things because the edge object contains three properties: label, color, and style.

For these details, I will not repeat them here. Interested students can visit the documentation for more details.

conclusion

As you can see from the previous results, the architecture diagrams drawn are very beautiful and can be done in less than 20 lines of code in terms of implementation logic. The implementation method is very simple and the post-adjustment is very easy.

If you’re interested in automatic drawing, give it a try.