Python: use Graphviz and pyreverse analysis class files to automatically generate UML diagrams

demand

When reading some source code, I want to take a look at its UML class diagram.
The search found that the existing software can achieve this function.

install software

Personal development environment: windows + vscode

  1. Graphviz
    can be installed on the official website: graphviz
    Note: After installation, add the installation path to the environment variables. My path isC:\Program Files (x86)\Graphviz2.38\bin
  2. pyreverse
    This program has been integrated in the pylint module. If pylint is not installed, you need to install it first:pip install pylint

use

Operating procedures:

  1. Want to perform class analysis on test.py
  2. Open the cmd or powershell window under the script folder. After executing the following command, classes.png will be generated in the current folder.
    pyreverse -ASmy -o png test.py
    You can also specify the output file name:, pyreverse -ASmy -o dot -p test test.pyand classes_test.png will be generated after running.

Note: I could not directly generate this command in one step, so it can be divided into two steps.

  1. pyreverse -ASmy -o dot test.py
    Execute this command on the command line (python), the role: pyreverse will analyze the class relationship in test.py, and then generate the classes.dot file.
    The output file name can be specified:, pyreverse -ASmy -o dot -p test test.pyand the classes_test.dot file will be generated after running.
    You can also specify a folder analysis: pyreverse -ASmy -o dot folder/
    Note: see other blog can generate images directly step: pyreverse -ASmy -o png test.py. But I will report an error, see the prompt message: only support dot file output. Then turn around again.
  2. dot -Tpng classes.dot -o test.png
    Executing this command on the command line has the effect: graphviz parses the .dot file into a picture.

The result diagram is as follows:
Only the inheritance relationship can be seen (simple conjecture: if you want to analyze other types of relationships, such as dependencies, combinations, etc., it will greatly increase the complexity.) Anyway, temporarily meet the simple needs. I want to look at other relationships in the future, and then study it carefully.

Insert picture description here

Published 47 original articles · Like 33 · Visit 310,000+

Guess you like

Origin blog.csdn.net/kaever/article/details/105276764