DEV Community

Thiago (Zozô) Ozores
Thiago (Zozô) Ozores

Posted on • Originally published at logs.zozo.dev.br on

Diagrams as Code

Come, come, come and find out how to generate diagrams using Python code!!

Em Português 🇧🇷 🇵🇹

In my day-to-day as an instructor, one of the jobs is to create diagrams that clearly illustrate the topic being presented to students, there are many good graphic tools online like draw.io, LucidChart, among others.

But despite these tools being very intuitive and easy to use, when you need to scale the creation of diagrams, need to create diagrams bringing information from external tools or even create simple diagrams quickly, you end up running into problems with formatting options and lack of automation of these tools, which make it difficult to create a “factory” of diagrams.

Thinking about this scenario, Python has a package that can be used to represent and generate diagrams as code, facilitating the creation of this “factory”.

The package is called diagrams and it has a very interesting way of working, it makes use of Python’s operator overload to perform in a more intuitive way the connection that the nodes will have in diagram graphics. For example:

  • The >> operator represents a right-to-left binding
  • The << operator represents a left-to-right binding
  • The - operator represents a directionless binding
  • And it’s still possible to make bidirectional bindings using the Edge class

If you’re looking for a more programmatic way to generate diagrams, this package is worth checking out.

Below are some examples I developed, which can be tested on Google Colab

Top comments (0)