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

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay