Graphviz is a graphing tool that automatically lays out graphs. It’s open source from Bell LABS. The diagrams that we talked about in Python last time, where we quickly drew a beautiful system architecture diagram, the orchestration logic inside of that uses this open source toolkit.
Pycallgraph pyCallGraph pyCallGraph pyCallGraph pyCallGraph pyCallGraph pyCallGraph pyCallGraph pyCallGraph pyCallGraph
1. Prepare
Before you begin, make sure Python and PIP are successfully installed on your computer. If not, please visit this article: Super Detailed Python Installation Guide to install Python. If you are using Python for data analysis, you can install Anaconda directly: Python data analysis and mining helper – Anaconda
In Windows, open Cmd(Start – Run – Cmd). In Apple, open Terminal(command+ space enter Terminal).
Of course, I recommend that you use the VSCode editor, Copy this code, and run commands in the terminal below the editor to install dependency modules.
Enter the following command to install the PyCallGraph module.
pip install pycallgraph
Copy the code
If Successfully installed XXX is displayed, the installation is successful. In addition, you need to install Graphviz
For macOS users please install using BREW:
brew install graphviz
Copy the code
For Windows users, click on the link: Graphviz-2.38.msi to download and install it. After installation, write it into the environment variable:
2. Generate flow charts
This module can be called in two ways, one from context in code:
from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
with PyCallGraph(output=GraphvizOutput()):
# Need to draw the flow chart of the code, can be a function
#... .
Copy the code
For example, draw a flow chart of our last Python Minecraft source code:
#... Omit most of the code...
if __name__ == '__main__':
from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
with PyCallGraph(output=GraphvizOutput()):
main()
Copy the code
After running the Python file, a pyCallgraph.png file is generated in the current folder. Here is the flow of the code:
MacOS users can ignore this problem, but if you are a Windows user, open bash as follows (in VS Code’s case) :
Then press Ctrl + Shift + ‘to open a new terminal and you’ll see the command line launched under bash.
Generate a flowchart by entering the following command:
Pycallgraph Graphviz --./ You need the.py file that generates the flowchartCopy the code
This will generate a pyCallgraph.png file in the current folder. Here is a flowchart of the code.
Through this method, you can clearly see the call logic in the source code and the running time of each module, is a very convenient small tool, very suitable for beginners to learn others open source modules. You can use this tool to try out the code you need to study, and you may get unexpected results.
So that’s the end of our article, if you enjoyed our Python tutorial today, please keep checking us out, and if it helped, please give us a thumbs up/check it out below. If you have any questions, please leave them in the comments below, and we’ll be patient to answer them!
Python utility guide
It’s not just a bible
Welcome to Python
Python Flowcharts – One-click conversion of code to flowcharts