Skip to main content

Generate Plantuml diagrams to document your python code

Project description

Python logo PlantUML logo

Python to PlantUML

Generate Plantuml diagrams to document your python code

How it works

From a given path corresponding to a folder containing python code, py2puml loads each file as a module and generate a class diagram with the PlantUML using:

  • inspection to detect the classes to document (see the inspect module)
  • annotations (the python type hinting syntax) to detect the attributes and their types (see the typing module)

Current limitations:

  • type hinting is optional when writing the code and discarded when it is executed, as mentionned in the official documentation. The quality of the diagram output by py2puml depends on the reliability with which the annotations were written

The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc.

  • complex type hints with more than one level of genericity are not properly handled for the moment: List[MyClass] or Dict[str, MyClass] are handled properly, Dict[str, List[MyClass]] is not. If your domain classes (also called business objects or DTOs) have attributes with complex type hints, it may be a code smell indicating that you should write a class which would better represent the business logic. But I may improve this part of the library as well 😀

  • composition relationships are detected and drawn. Inheritance relationships are not handled for now

  • py2puml does not inspect sub-folders recursively, but it is planned

  • py2puml outputs diagrams in PlantUML syntax, which can be saved in text files along your python code and versioned with them. To generate image files, use the PlantUML runtime or a docker image (see think/plantuml)

  • py2puml uses features of python 3 (generators for example) and thus won't work with python 2 runtimes. It relies on native python modules and uses no 3rd-party library, except pytest as a development dependency for running the unit-tests

You may also be interested in this lucsorel/plantuml-file-loader project: A webpack loader which converts PlantUML files into images during the webpack processing (useful to include PlantUML diagrams in your slides with RevealJS or RemarkJS).

Install

Install from the github repository:

  • with pip:
pip install py2puml
poetry add py2puml
pipenv install py2puml

Usage

For example, to create the diagram of the classes used by py2puml:

from py2puml.py2puml import py2puml

# outputs the PlantUML content in the terminal
print(''.join(py2puml('py2puml/domain', 'py2puml.domain')))

# writes the PlantUML content in a file
with open('py2puml/domain.puml', 'w') as puml_file:
    puml_file.writelines(py2puml('py2puml/domain', 'py2puml.domain'))
  • running it (python3 -m py2puml.example) will output the PlantUML diagram in the terminal and write it in a file
@startuml
class py2puml.domain.umlclass.UmlAttribute {
  name: str
  type: str
}
class py2puml.domain.umlclass.UmlClass {
  attributes: List[UmlAttribute]
}
class py2puml.domain.umlitem.UmlItem {
  name: str
  fqdn: str
}
class py2puml.domain.umlenum.Member {
  name: str
  value: str
}
class py2puml.domain.umlenum.UmlEnum {
  members: List[Member]
}
enum py2puml.domain.umlrelation.RelType {
  COMPOSITION: *
  INHERITANCE: <|
}
class py2puml.domain.umlrelation.UmlRelation {
  source_fqdn: str
  target_fqdn: str
  type: RelType
}
py2puml.domain.umlclass.UmlClass *-- py2puml.domain.umlclass.UmlAttribute
py2puml.domain.umlitem.UmlItem <|-- py2puml.domain.umlclass.UmlClass
py2puml.domain.umlenum.UmlEnum *-- py2puml.domain.umlenum.Member
py2puml.domain.umlitem.UmlItem <|-- py2puml.domain.umlenum.UmlEnum
py2puml.domain.umlrelation.UmlRelation *-- py2puml.domain.umlrelation.RelType
@enduml

Which renders like this:

Tests

python3 -m pytest -v

Changelog

  • 0.2.0: handle inheritance relationships and enums. Unit tested
  • 0.1.3: first release, handle all module of a folder and compositions of domain classes

Licence

Unless stated otherwise all works are licensed under the MIT license, a copy of which is included here.

Contributions

Pull-requests are welcome and will be processed on a best-effort basis.

Alternatives

If py2puml does not meet your needs (suggestions and pull-requests are welcome), you can have a look at these projects which follow other approaches (AST, linting, modeling):

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py2puml-0.2.0.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py2puml-0.2.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file py2puml-0.2.0.tar.gz.

File metadata

  • Download URL: py2puml-0.2.0.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.6 Linux/4.15.0-101-lowlatency

File hashes

Hashes for py2puml-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1beecbd9c79a672381696b9ab8811028ebb7a9bd727a2179cb74b7740b73cdec
MD5 326788bbce0ca2903c80b7b97c24d89f
BLAKE2b-256 d910ac72bf379a55cd6aba86f6b89e495400a3b1f78aed7516d862c27090e3b5

See more details on using hashes here.

File details

Details for the file py2puml-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: py2puml-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.6 Linux/4.15.0-101-lowlatency

File hashes

Hashes for py2puml-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2628669d62e5a5a96e93641c15dd6fc78ec046a0bcd237b34077d3da38a0a63d
MD5 25d68ee15c5c5ecb5c84594c48ccfb32
BLAKE2b-256 5b9ca6d4d3ea059f97f129ccd33f57720f7f6eb919241e0170ec95caaa01ec9d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page