pythonumlclass-diagrampyreverse

How to generate the UML diagram from the python code


I have this code repo

I created manual UML which look like this: enter image description here

I am trying to auto generate the UML via pyreverse:

pyreverse -o png -p ShoppingCart ./mainService.py

Format png is not supported natively. Pyreverse will try to generate it using Graphviz...

Unfortunately, it gives me blank diagram. What can I do to get the project's classes in the diagram?

This is the file structure:

.
├── Entity
│   ├── Apple.py
│   ├── Buy1Get1FreeApple.py
│   ├── Buy3OnPriceOf2Orange.py
│   ├── Offer.py
│   ├── Orange.py
│   ├── Product.py
│   └── ShoppingCart.py
├── Enum
│   └── ProductType.py
└── mainService.py

Solution

  • In short

    Assuming the installation of pyreverse and graphviz is correct, all you need to do is to package your project adding some emplty __init__py files in each folder. Alternatively, you'd hjhave to add all the modules manually in the command line.

    More details - step by step

    About the error message

    Assuming everything is installed correctly, your command line should give you the warning message, which is absolutely normal:

    Format png is not supported natively. Pyreverse will try to generate it using Graphviz...

    What's wrong?

    The diagram will stay empty because you tell pyreverse to analyse a single file and there is no class defined in that file. If you'd add manually the different modules to be analysed:

    pyreverse -o png -p ShoppingCart mainService.py Entity\Apple.py Enum\ProductType.py Entity\Orange.py Entity\ShoppingCart.py
    

    you would then very well obtain a rudimentary diagram:

    four classes side by side

    If you would add the options -AS upfront you'd get all ancestors in the project and all associated classes recursively:

    enter image description here

    How to package your project?

    This is cumbersome. Fortunately, there's little missing to package your project. For a full reference, you may look here. But in short, it is sufficient to add an empty __init__.py file in your project folder, and each subfolder where you store the modules:

    .
    ├── Entity
    │   ├── __init__.py                   <<===== add this empty file
    │   ├── Apple.py
    │   ├── Buy1Get1FreeApple.py
    │   ├── Buy3OnPriceOf2Orange.py
    │   ├── Offer.py
    │   ├── Orange.py
    │   ├── Product.py
    │   └── ShoppingCart.py
    ├── Enum
    │   ├── __init__.py                   <<===== add this empty file
    │   └── ProductType.py
    ├── __init__.py                       <<===== add this empty file
    └── mainService.py
    

    You will then be able to run the simpler command line:

    pyreverse -AS -o png -p ShoppingCart .
    

    and obtain this magnificient diagram:

    enter image description here

    The packaging helps python and pyreverse to understand that these are not files to be analysed in isolation, but in the context of a package made of subpackages, etc.