Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Diagram Architecture using the diagrams module in Python

Diagrams module Python Min

Today, we’re going to work on the diagrams module in Python. If you worked on large-scale projects and have needed to present, chances are that you’ve drawn diagrams to demonstrate the same.

Technically speaking, diagrams are simply lines joined together to form different shapes.

Now, we understand it’s a lot of work trying to align diagrams the exact way you want them to be aligned, and, it’s a hassle getting the boxes and the labels to line up.

So, there goes a lot of valuable time wasted in creating a single diagram which may not even be as useful as the time you’ve spent on it.

Well, lucky for us, there’s a better method to create diagrams, and flowcharts to display those pesky AWS, and GCP connections.

The diagrams module!

The diagrams module allows us to create diagrams through a simple few lines of code, used to just connect the components to one another.

We don’t need to specify their positions in the images, not the paths that the arrow lines need to take.

It’s a very efficient tool, and I’m pretty sure we’d like to be efficient as well, so, let’s get right into working with it!

Installing the diagrams module in Python

In order to work with the diagrams module, we’ll first need to install GraphViz, which provides the images for the diagrams, so, here’s the link.

Along with that, we’ll also need to install the diagrams module and we’ll do so using the pip command:

pip install diagrams

Using the diagrams module

Now that we’re all set up, we can now work with the diagrams module through our console!

1. Initialization

In order to work with the diagrams module, we’ll first need to import it into the Python Script.

This can be done using the following command,

from diagrams import Diagram

We’ll also need to import a few classes from the diagrams.aws package, which allows us to work with the different components of the AWS system in the Script.

from diagrams.aws.compute import EC2
from diagrams.aws.database import RDS
from diagrams.aws.network import ELB

Of course, this doesn’t mean however that we’ll be running AWS components, it just means that we can visualize them with these components.

2. Working with the Components

The way that the diagrams module works is simple.

We open up a file, and perform operations on it, be it adding text, adding images, or even just drawing arrows.

We open up the file, using the with command, and provide a name for the file, which will also be used as the name of the image.

The show=False command is used just so that we don’t have the image pop out at us when we run the script,

with Diagram("Hello World", show=False):

Proceeding ahead, we use the classes that we’ve imported from the diagrams module, and provide a label to them as well:

ELB("lb"), EC2("web"), RDS("userdb")

Then, we connect these different components, using >> signifying arrow marks in the diagram.

Upon doing so, the end result we receive should look something like this,

with Diagram("Web Service", show=False):
    ELB("lb") >> EC2("web") >> RDS("userdb")

When we execute this script, we receive an output file, in the .png format, which looks something like this,

Diagrams module Ws
Diagram Ws

Another example on working with these components, would be to connect a whole lot of components to a single one,

This can be done, simply by grouping all of these components, into a single list.

with Diagram("Grouped Workers", show=False, direction="TB"):
    ELB("lb") >> [EC2("worker1"),
                  EC2("worker2"),
                  EC2("worker3"),
                  EC2("worker4"),
                  EC2("worker5")] >> RDS("events")

When we group them up, we consider all of the components as ones which receive resources from the same input. So, we draw arrows to them individually.

Upon executing this script, you should receive an image, which looks something like this,

Diagram Gw
Grouped Workers are in the the .png file.

These diagrams are stored in a .png format in the same directory that the script was run in.

3. Moving Forward

The diagrams module has amazing documentation, and ranges through different services, and provides guides on Diagrams, Nodes, Clusters, and Edges as well.

These are very well documented and are explained pretty well.
However, like every other module, this too is open-sourced, so, here’s the GitHub link in order to figure out how exactly this module works!

Conclusion

Gone are the days where we are forced to sit and align the lines to the diagrams, spending more than needed time to fix it by a pixel’s count.

Now, we can just automate it using the diagrams module, and not worry about it anymore.

Automating stuff is a lot of fun, and that being said, here’s a few other articles, that can help you automate using Python – Facebook Login, Scheduling using crontab, and the sched module.

References