Tue. Oct 4th, 2022

Hello Android

All android in one place

Python diagrams module – Creating Architecture Diagrams

5 min read

Hello, everyone! Today, we will be working on the Python diagrams module. If you work on large-scale projects and need to show them, you will almost certainly need to design diagrams to demonstrate the same.

You would create these diagrams typically by haphazardly copying photos into draw.io or Google Diagrams and then spend hours aligning everything correctly. Furthermore, when you subsequently have to update these diagrams, you have to lift and relocate more than half of the components merely to make a few modifications to the design.

We realize how difficult it is to align diagrams exactly the way you want them to be aligned and how difficult it is to get the boxes and labels to line up.


Introduction to the Diagrams Module

The diagram depicts an open-source Python library, and as stated on the website:

Diagrams allow you to draw the architecture of a cloud system in Python code. It was created to develop a novel system architecture without using any design tools. You can also explain or depict the existing system architecture.

The rendering of diagrams happens with the help of Graphviz. The diagrams are graphviz-generated directed graphs. The diagrams module enables us to generate diagrams with just a few lines of code, which are then utilized to connect the components. We don’t need to describe their positions in the photos, nor do we specify the pathways that the arrow lines must take.


Some Important Terms to know

Diagrams: A diagram is a basic object that represents a diagram.
Nodes: An abstract concept that represents a single component of a system.
Clusters: This allows you to organize nodes into groups (or clusters) rather than individual components.
Edges: Represents a connection between Nodes.


Using the diagrams module to draw architectural diagrams

Let’s now get into implementing the code for creating architectural diagrams using the Python diagrams module.

1. Start by Installing the graphviz and diagrams modules

The first step toward creating some of these diagrams is to install Python 3.6 or above. You will need to install the Graphviz module responsible for rendering the diagrams.

Along with that, we’ll also need to install the diagrams module. We’ll install both modules using the pip command.

%%capture
!pip install graphviz
!pip install diagrams

2. Creating a Blank Diagram

Now that you understand the fundamental ideas, let’s create a very simple diagram using code in the order we learned them.

We will import the diagrams module and then open a file and conduct actions, such as adding text, photos, or just drawing arrows. We open the file with the with command and give it a name that will also be used as the name of the image.

# Importing Diagrams Module
from diagrams import Diagram
# Opening up a new file to create diagram
with Diagram("Welcome to JournalDev.com") as diag:
    pass
# Displaying the diagram
diag

This creates a blank diagram with the specified label, as shown below.

FirstDiag Diagrams

3. Adding Nodes to the Diagram

Now that we have our workspace, we can add the nodes we’ll need for our project. We want to add nodes from two separate providers. AWS and on-premises providers

If you were doing this for real, you’d probably stick with AWS because they have services like RDS and ElastiCache that you’d probably use with that cloud provider.

The AWS providers expose the official cloud service assets that you would use in any diagram that uses one of the major cloud providers.

We’ll need to import a few classes from the diagrams.aws package to deal with the various AWS system components in the Script.

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

Of course, this does not imply that we will be running AWS components; it simply means that we will visualize them using these components.

Moving forward, we’ll use the classes we imported from the diagrams module and give them a label.

with Diagram("Welcome to JournalDev.com") as diag:
    ELB_obj = ELB("Load Balancer")
    EC2_obj = EC2("Webserver 1")
    RDS_obj = RDS("Relational DB")
diag
AddedNodes Diagrams Module
AddedNodes Diagrams Module

As you can see, there is no longer a blank diagram. Each of our nodes depicts the “ingredients” for the architecture we wish to construct. The following steps will involve connecting components with edges.

4. Linking Components Together

In the diagram, we utilize >> to represent arrow marks to distinguish these various components.

If you look at the diagram below, you can see that it is simply a question of describing the flow to each node with double arrows, and you have your diagram!

with Diagram("Welcome to JournalDev.com") as diag:
    ELB_obj = ELB("Load Balancer")
    EC2_obj = EC2("Webserver 1")
    RDS_obj = RDS("Relational DB")
    ELB_obj>>EC2_obj>>RDS_obj
diag

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

AddedLinks Diagrams Module
AddedLinks Diagrams Module

You can now see a logical flow between each node in the figure. You can reverse this flow by modifying the order of defining the nodes.

5. Linking with Multiple Components

Another example of how to use these components is to connect a large number of them to a single one.

This is easily accomplished by grouping these components into a single list.

with Diagram("Welcome to JournalDev.com") as diag:
    ELB_obj = ELB("Load Balancer")
    EC2_obj = EC2("Webserver 1")
    RDS_obj = RDS("Relational DB")

    ELB_obj>>[EC2_obj,RDS_obj]
    
diag

When we aggregate them, we treat all components as receiving resources from the same input. As a result, we draw arrows to each of them independently.

When you run the above code, you will see an image somewhat like this.

Added Multiple Links Diagrams Module
Added Multiple Links Diagrams Module

A Sample Architectural Diagram using Diagram Module

with Diagram("Welcome to JournalDev.com",direction="TB") as diag:
    ELB_obj_1 = ELB("Load Balancer 1")
    ELB_obj_2 = ELB("Load Balancer 2")
    EC2_obj_1 = EC2("Webserver 1")
    EC2_obj_2 = EC2("Webserver 2")
    EC2_obj_3 = EC2("Webserver 3")
    EC2_obj_4 = EC2("Webserver 4")
    EC2_obj_5 = EC2("Webserver 5")
    EC2_obj_6 = EC2("Webserver 6")
    RDS_obj = RDS("Relational DB")

    ELB_obj_1>>[EC2_obj_1,EC2_obj_2,EC2_obj_3]>>RDS_obj

    RDS_obj>>[EC2_obj_4,EC2_obj_5,EC2_obj_6]>>ELB_obj_2

diag
Sample Diagram Module
Sample Diagram Module

Conclusion

Gone are the days when we had to sit and align the lines to the diagrams, spending more time than necessary to fix it by the pixel count.

Now that you’ve honed your skills at creating a beautiful diagram using code, there’s a lot of opportunity for leveraging this workflow in terms of automation and time saved with the general maintenance of an architecture diagram.

Automation is a lot of fun, so here is another great article to help you automate using Python: Twilio – Send Text Message Using Python

www.hello-android.com

Leave a Reply

Your email address will not be published. Required fields are marked *

Hello android © All rights reserved. | Newsphere by AF themes.