Skip to main content

Python DSL for writing PlantUML sequence diagram

Project description

Build Status PyPI version

Napkin

Napkin is a tool to "write" sequence diagrams effectively as Python code.

Motivation

The sequence diagrams are useful tool to capture the behavioural aspect of the design. PlantUML is a great tool to draw nice sequence diagrams with simple human readable plain text.

However, the syntax of PlantUML is hard to use when there are nested calls, where lifeline with multiple activation/deactivations are involved. Unfortunately, this situation is quite common in sequence diagram for S/W.

For example, consider the following common sequence diagram, which is from Figure 4.2, UML Distilled 3E: Figure 4.2, UML Distilled 3E

The PlainUML script for the diagram will be as follows:

@startuml
participant User
participant Order
participant OrderLine
participant Product
participant Customer

User -> Order : calculatePrice()
activate Order
Order -> OrderLine : calculatePrice()
activate OrderLine
OrderLine -> Product : getPrice(quantity:number)
OrderLine -> Customer : getDiscountedValue(Order)
activate Customer
Customer -> Order : getBaseValue()
activate Order
Customer <-- Order: value
deactivate Order
OrderLine <-- Customer: discountedValue
deactivate Customer
deactivate OrderLine
deactivate Order
@enduml

It is quite hard to follow especially as there are multiple level of nested actviation/deactivation.

What if we express the same thing as the following Python code ?

@napkin.seq_diagram()
def distributed_control(c):
    user = c.object('User')
    order = c.object('Order')
    orderLine = c.object('OrderLine')
    product = c.object('Product')
    customer = c.object('Customer')

    with user:
        with order.calculatePrice():
            with orderLine.calculatePrice():
                product.getPrice('quantity:number')
                with customer.getDiscountedValue(order):
                    order.getBaseValue().ret('value')
                    c.ret('discountedValue')

distributed_control is normal function accepting a context object, c to access APIs. The function defines objects and the control starts with user object, which then calls orderLine.calculatePrice(). Basically, the sequence diagram is expressed as "almost" normal python code.

There are several advantages in using Python instead of using other special syntax language:

  • Easy to write/maintain scripts for the correct diagrams
  • Many common mistakes are detected as normal Python error. For example, method call to an undefined object will be just normal Python error.(This can be even checked by IDE without running scripts).
  • Any Python editor can become sequence diagram editor
  • There can be many interesting usages by taking advantage of Python as general language. For example, we can build a library for patterns.

Installation

Install and update using pip

pip install napkin

Hello world

Write a simple script called hello.py as follows:

import napkin

@napkin.seq_diagram()
def hello_world(c):
    user = c.object('user')
    world = c.object('world')
    with user:
        world.hello()

Then, the following command will generate hello_world.puml:

$ napkin hello.py

Usages

Command line

usage: napkin [-h] [--output-format {plantuml,plantuml_png}]
              [--output-dir OUTPUT_DIR] [--version]
              srcs [srcs ...]

positional arguments:
  srcs                  Python file or directory containing diagram functions

optional arguments:
  -h, --help            show this help message and exit
  --output-format {plantuml,plantuml_png}, -f {plantuml,plantuml_png}
  --output-dir OUTPUT_DIR, -o OUTPUT_DIR
  --version             show program's version number and exit

Supported output formats:
  plantuml         : PlantUML script (default)
  plantuml_png     : PlantUML script and PNG image

Standalone code to generate diagrams

Instead of passing napkin binary Python files, we can generate diagrams simply by running the Python source code containing the diagrams as follows:

import napkin

@napkin.seq_diagram()
def hello_world(c):
    ...


if __name__ == '__main__':
    napkin.generate()

napkin.generate(output_format='plantuml', output_dir='.') will generate all the diagrams described in the same file.

Generate PNG files directly

Napkin can generate PNG formt image files directly by using plantuml Python package.

After installing the optional package as follows:

$ pip install plantuml

napkin can specify the out format with plantuml_png, which will generate PNG file along with puml file.

$ napkin -f plantuml_png hello.py

Python script examples

Basic Examples

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

napkin-0.5.4.9.tar.gz (12.6 kB view details)

Uploaded Source

Built Distributions

napkin-0.5.4.9-py3-none-any.whl (16.9 kB view details)

Uploaded Python 3

napkin-0.5.4.9-py2-none-any.whl (16.9 kB view details)

Uploaded Python 2

File details

Details for the file napkin-0.5.4.9.tar.gz.

File metadata

  • Download URL: napkin-0.5.4.9.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/2.7.15

File hashes

Hashes for napkin-0.5.4.9.tar.gz
Algorithm Hash digest
SHA256 cb0f1e77c48ff3941270d0a5dd1e43cb2f2cf2150620e01090f41ea41d46b5a9
MD5 3813c88181a55eff47bbc8bf422129bb
BLAKE2b-256 34d1e5cf1a68547db96f41471e234d99caeef1121c9238a28d07fa81fc4690a7

See more details on using hashes here.

File details

Details for the file napkin-0.5.4.9-py3-none-any.whl.

File metadata

  • Download URL: napkin-0.5.4.9-py3-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.3.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.7

File hashes

Hashes for napkin-0.5.4.9-py3-none-any.whl
Algorithm Hash digest
SHA256 2b6d761e72069466194461c99c3c6f0e4c06be56f9b112e8bbb8993ec8a5f28f
MD5 b2d4825e141f6924c799807cbc5691bc
BLAKE2b-256 20f5a6c38e45f0709a4f49d06fb53e9833333e3268ca83e9117395f94a694f76

See more details on using hashes here.

File details

Details for the file napkin-0.5.4.9-py2-none-any.whl.

File metadata

  • Download URL: napkin-0.5.4.9-py2-none-any.whl
  • Upload date:
  • Size: 16.9 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/2.7.15

File hashes

Hashes for napkin-0.5.4.9-py2-none-any.whl
Algorithm Hash digest
SHA256 2ec65f982212217ef5c54030996b5c23f5ef6403d31c5530c2a0e5c85e082d54
MD5 0bf32f8b1c3529eb0ff3c939f1e1d3fb
BLAKE2b-256 ec2acbfc86b5d2e363897d4de5bbdf356c2e604ee527a1c77642b501e9c2f3ba

See more details on using hashes here.

Supported by

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