'How to get Class diagram from Python source code?
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()
Solution 1:[1]
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
:
Solution 2:[2]
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.
Solution 3:[3]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Dash |
Solution 3 | Thomas |