Python 3.8 วาดกราฟด้วย Graphviz

ติดตั้ง graphviz บน Ubuntu

sudo apt install graphviz

ติดตั้ง graphviz บน Windows เปิด cmd ด้วยสิทธิ administrator

choco install graphviz

dependency-graph

ติดตั้งไพธอนไลบรารี graphviz-0.19

python3.8 -m pip install graphviz

ที่บรรทัดแรกของไฟล์ dependency_graph.py เพิ่ม

#!/usr/bin/python3.8

ทำให้ไฟล์ dependency_graph.py รันได้

chmod 755 dependency_graph.py

หาตัวอย่าง source code ภาษา c (GitHub – pvigier/ecs: A simple and easy to use entity-component-system C++ library) มาไว้ใน folder src แล้วทดลองรัน

./dependency_graph.py src/ out -f png

จะได้ไฟล์ภาพ out.png

usage: dependency_graph.py [-h] [-f {bmp,gif,jpg,png,pdf,svg}] [-v] [-c]
                           folder output

positional arguments:
  folder                Path to the folder to scan
  output                Path of the output file without the extension

optional arguments:
  -h, --help            show this help message and exit
  -f {bmp,gif,jpg,png,pdf,svg}, --format {bmp,gif,jpg,png,pdf,svg}
                        Format of the output
  -v, --view            View the graph
  -c, --cluster         Create a cluster for each subfolder

แต่ถ้ารันแล้วได้ error

'dot' is not recognized as an internal or external command

แสดงว่ายังติดตั้ง graphviz ไม่สำเร็จ หรือ หา path ไม่เจอ Graphviz’s dot tool on Windows

pydeps

ติดตั้ง pydeps

python -m pip install pydeps

ตัวอย่างการเรียกใช้

pydeps pydeps
pydeps pydeps --rankdir TB
pydeps pydeps --rankdir BT

ให้ output เป็นไฟล์ png

pydeps -T png pydeps