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/deactivation 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 activation/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,plantuml_svg,plantuml_txt}] [--output-dir OUTPUT_DIR] [--version] [--server-url SERVER_URL]
              srcs [srcs ...]

Generate UML sequence diagram from Python code

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,plantuml_svg,plantuml_txt}, -f {plantuml,plantuml_png,plantuml_svg,plantuml_txt}
  --output-dir OUTPUT_DIR, -o OUTPUT_DIR
  --version             show program's version number and exit
  --server-url SERVER_URL
                        (only for plantuml_png/svg format) Default is the public server

Supported output formats:
  plantuml         : PlantUML script (default)
  plantuml_png     : PlantUML script and PNG image
  plantuml_svg     : PlantUML script and SVG image
  plantuml_txt     : PlantUML script and ASCII art text

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 image files using PlantUML server

Napkin can generate PNG/SVG image or ASCII art text files by asking PlantUML server.

In order to generate image file, image format needs to be specified as plantuml_<png|svg|txt>, which will generate image file along with puml file.

$ napkin -f plantuml_png hello.py

As default, the public server is used and it can be changed by --server-url.

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.6.1.tar.gz (13.5 kB view details)

Uploaded Source

Built Distributions

napkin-0.6.1-py3-none-any.whl (18.8 kB view details)

Uploaded Python 3

napkin-0.6.1-py2-none-any.whl (18.8 kB view details)

Uploaded Python 2

File details

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

File metadata

  • Download URL: napkin-0.6.1.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/2.7.15

File hashes

Hashes for napkin-0.6.1.tar.gz
Algorithm Hash digest
SHA256 8ffe683c53849d35baf1b297e788674da4e1d51911138abfb407b74fa9859e26
MD5 35c3196d5c271081b425b578e853aecd
BLAKE2b-256 1d1a10352f0d76b079ab8d6b6af6eaf477612868847c1484f21b5bc032565b62

See more details on using hashes here.

File details

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

File metadata

  • Download URL: napkin-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.1

File hashes

Hashes for napkin-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 08806642c1efdbaf29918e79dc774542507884ac91de4c61a30de69c1f82577a
MD5 054b0c640fe30ac86ba234c62c2c9619
BLAKE2b-256 b7381afac3e5902d69231a7c412d17c1af3ab35fed96677750417943f847f818

See more details on using hashes here.

File details

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

File metadata

  • Download URL: napkin-0.6.1-py2-none-any.whl
  • Upload date:
  • Size: 18.8 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/44.1.1 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/2.7.15

File hashes

Hashes for napkin-0.6.1-py2-none-any.whl
Algorithm Hash digest
SHA256 7c3ef11bffc1ec4cd2b8fdf9ffc3b5770584bef8d405ecf8a095c60ee7d38303
MD5 2fe37da7494f4255fa765c9e3b351444
BLAKE2b-256 bb2516b4cb586d13295e3cc4a4f23715c33eee284408f657807f8b32616f8f35

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