How to get Class diagram from Python source code?
Asked Answered
D

3

9

I try to get a class diagram from Python source code in Client folder with pyreverse but it requires __init__.py

(venv) C:\Users\User\Desktop\project> pyreverse Client
parsing Client\__init__.py...
Failed to import module Client\__init__.py with error:
No module named Client\__init__.py.

I don't find any solution for this. Is there a way to get the diagram?

Update: There are many files in Client folder:

Client.py
GUI.py
script.py
...

This is a part of the Client.py code:

import threading


class Client:
    def __init__(self):
        self.socket = None
        self.listen_socket = None
        self.buff_dict = {}
        self.message_list_dict = {}
        self.lock = threading.Lock()
        self.target = None
        self.listen_flag = True

This is a part of the GUI.py code:

import tkinter as tk


class Window(object):
    def __init__(self, title, font, client):
        self.title = title
        self.font = font
        self.client = client
        self.root = tk.Tk()
        self.root.title(title)
        self.build_window()

    def build_window(self):
        pass


class LoginWindow(Window):
    def __init__(self, client, font):
        super(LoginWindow, self).__init__('Login', font, client)
        self.build_window()
Doy answered 26/6, 2020 at 6:26 Comment(12)
Could you post the code for Client as well?Craven
@Craven I should post all the code or all the names of the file in Client folder?Doy
So, Client is a folder not a class ... right?Craven
@Craven yes, Client is a folder contains many .py file.Doy
@Doy do you define class(es) in these files ?Appall
@Appall I just update some example of my folder and some .py file inside the folder.Doy
@huy, then you need to create __init__.py file inside this folderCraven
@Craven do you know what we code in __init__.py?Doy
@huy, nothing... just an empty fileCraven
@Craven it seems work, now I just need to convert .dot file to .png file.Doy
@huy, Glad I could helpCraven
@Doy just use the option -o pngAppall
D
10

Thanks to @Anwarvic and @bruno, I came up with the solution for this.

Firstly, create empty __init__.py file inside Client folder:

(venv) C:\Users\User\Desktop\project\Client> type NUL >  __init__.py

Then go to the parent folder of the Client folder where I want to get the class diagram:

(venv) C:\Users\User\Desktop\project> pyreverse Client -o png

But I got this error:

The output format 'png' is currently not available.
Please install 'Graphviz' to have other output formats than 'dot' or 'vcg'.

After some findings, I found this solution. Then I can run the pyreverse without any error.

This is the class diagram I got using pyreverse:

enter image description here

Doy answered 26/6, 2020 at 8:21 Comment(1)
Simply creating an empty via touch __init__.py and then running pyreverse ./ -a1 -s1 and then dot -Tpng ./output.png ./classes.dot worked for me.Counterattraction
G
1

It seems like you don't have an __init__.py in the folder that contains Client.py. You should be able to just create the file without putting anything in it, as its main purpose is to indicate that the folder is a package.

See this SO question about __init__.py for a more in-depth explanation about the file.

Goins answered 26/6, 2020 at 6:52 Comment(0)
S
1

The most convenient approach should be to have a Jupyter notebook extension that dynamically generates diagrams of the classes defined in a notebook somehow like the variable inspector - possibly with the option to specify an alternative root like datetime in the first answer to creating UML charts by Pylint/pyreverse within Jupyter labs / console.

Note: This would remove the restriction of pyreverse requiring the displayed classes to be part of a module.

Stenographer answered 8/5, 2022 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.