1. Products
  2.   Diagram
  3.   Python
  4.   Py2pUML
 
  

Free Python Library to Create & Manage Visio Diagrams

Open Source Visio Python Library allows Developers to Generate Class Diagrams in PlantUML Syntax, Providing a Visual Representation of Python Codebases.

What is Py2pUML Library?

In modern software development, understanding and documenting code structure is crucial for maintainability and collaboration. The Py2pUML library is an open-source tool that helps developers automatically generate UML class diagrams from Python code. These diagrams visually represent class relationships, attributes, and dependencies, making it easier to grasp the architecture of a project. The library supports generating UML diagrams for Python projects, visualize class hierarchies and relationships, enhance project documentation, improve team collaboration by simplifying code structure analysis, and so on. To avoid potential side effects, py2puml does not evaluate assigned values or expressions during the detection of composition relationships. This design choice ensures the safety and integrity of the code analysis process.

Py2pUML is an open source Python library designed to automatically generate UML diagrams from Python code. UML diagrams are a standard way to visualize the structure and design of a software system, including classes, relationships, and dependencies. Traditionally, creating these diagrams has been a manual and time-consuming process. Py2pUML simplifies this by parsing Python code and producing UML diagrams in PlantUML format, which can then be rendered into visual diagrams. Currently, py2puml may not handle complex type hints with multiple levels of generics effectively. This limitation is acknowledged, and future improvements are considered. By addressing these considerations, py2puml serves as a valuable tool for developers seeking to document and visualize the architecture of their Python applications through class diagrams.

Previous Next

Getting Started with Py2pUML

PThe recommend way to install Py2pUML library is using pypi. Please use the following command for a smooth installation.

Install Py2pUML Library via pypi

pip install py2puml

Install Py2pUML Library using Poetry

poetry add py2puml

Generate UML Diagram from Python Module

The open source Py2pUML library has provided complete functionality enabling software developers to generate a PlantUML class diagram for any Python module. Utilizing Python's inspection capabilities and type annotations, py2puml identifies both static class attributes and dataclass fields. This ensures that the generated diagrams accurately reflect the structure and properties of classes within the codebase. Here’s a basic example that demonstrates, how software developers can generate a PlantUML class diagram for a Python module.

How to Generate a PlantUML Class Diagram for a Python Module?

from py2puml.py2puml import py2puml

# Generate PlantUML content
puml_content = py2puml('path/to/your/module', 'module_name')

# Output the PlantUML content to the terminal
print(''.join(puml_content))

# Write the PlantUML content to a file
with open('module_name.puml', 'w') as puml_file:
    puml_file.writelines(puml_content)

Class and Relationship Detection

Developers can integrate Py2pUML into a Flask or Django web app to provide a UML generation service. It analyzes Python code and generates UML diagrams without requiring manual input. It identifies classes, attributes, methods, and relationships, making it easy to visualize the structure of your code. This API allows users to upload a Python module and receive a UML class diagram as a downloadable .puml file. Consider the following code example, the library will generate the PlantUML output, which will contain information about the classes and their relationship.

How to Generate the PlantUML Output from a Python Code?

class Engine:
    def start(self):
        print("Engine started")

class Car:
    def __init__(self, engine: Engine):
        self.engine = engine

    def drive(self):
        self.engine.start()
        print("Car is moving")

# Py2pUML will generate the following PlantUML output:

@startuml
class Engine {
    + start()
}

class Car {
    - engine: Engine
    + drive()
}

Car --> Engine
@enduml

#This diagram shows that Car has a dependency on Engine.

Code Customization & Ease of Use

The Py2pUML library is designed to be simple and straightforward. It requires minimal setup and can be integrated into your development workflow with ease. As an open-source library, Py2pUML can be customized to meet specific project requirements. Developers can fork the repository, modify the code, and contribute to its development. It is very easy to modify the source code to suit your needs. For instance, you could add support for detecting and visualizing abstract classes.

Improving Code Documentation

UML diagrams are an excellent way to document your code. They provide a clear and concise representation of the system’s architecture, making it easier for other developers (or your future self) to understand how the code works. Py2pUML can be integrated into your documentation workflow to automatically generate up-to-date diagrams.