Code2graph, easily visualize a project’s dependency graph

Ali Varfan
2 min readJan 28, 2021

In the software industry, most documentations are user-facing which means they have been written with the goal of describing how to use the program/application like a black box. This is fine until you want to maintain legacy code and/or add a new feature to it or do knowledge transfer and change ownership or share ownership of a component with a colleague then lack of documentation regarding how the application works under the hood can make problems (if the developer is not available) or it takes a lot of time. On the other hand, writing documentation is very time consuming and sometimes boring and also needs a deep knowledge of that component.

Having some tools that could automatically generate some documentation on how the application is working under the hood can solve the above issues. We have two types of documentations, text and visual in the form of diagram or graph. Auto generating textual documentation is very hard and definitely needs AI and machine learning and it is still an open issue but what about visual documentation? Recently I created a program called Code2graph which automatically finds all the python files in a target directory and generates a dependency graph for them.

Code2graph is using the python lexer to find all the function calls, draw each function as a node and a directed edge between the caller and the function. Since Code2graph is using python lexer it is 100% offline and you can be sure that it will not send your code to any remote machine. In addition to the security, you can choose the output of the program. By default, Code2graph is generating dot files that later can be visualized using tools such as Graphviz but you can also define extra outputs in the format of SVG or PNG and the program will generate the graph and save the svg/png file in addition to the dot file.

Note: if you want to use the svg/png output format you need to install Graphviz on your machine too.

Installation of Code2graph is very simple and it can be done using `pip` which then makes the program available system-wide and you can use it just by typing `code2graph.py`.

Code2graph provides three methods of generating the graphs:

  • File: graph (dot file + svg/png) will be created for each python file and will be saved with the same name and at the same path of the file
  • Project: graph (dot file + svg/png) will be created only for the whole project (target directory) and single graphs will not be created for each python file
  • All: It is a combination of “File” and “Project”

For more information and detailed usage please check the Gitlab repo of Code2graph: https://gitlab.com/avarf/code2graph/

--

--