Skip to main content

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

Examples

Misc

Helper tool to convert PlantUML text files to image files

As Napkin has a functionality to generate image files from PlantUML text file, a simple script, napkin_plantuml is provided to use PlantUML server to generate images.

For example, in order to generate hello.png image from hello.puml file:

$ napkin_plantuml hello.puml hello.png

Emacs org babel support

In order to use napkin as literate programming tool ob-napkin package is supported for Emacs.

Metadata

Release files for napkin 0.6.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for napkin 0.6.7
File Size Uploaded
napkin-0.6.7.tar.gz 14.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for napkin 0.6.7
File Interpreter ABI Platform
napkin-0.6.7-py3-none-any.whl Python 3 none any Details

Total release size: 35.3 kB

Release files / napkin-0.6.7.tar.gz

Download URL napkin-0.6.7.tar.gz
Size 14.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8c2188483ed7d636b13cd7a9914b6c1d3b0a1b53a3f2cb3a88fe055962261361
BLAKE2b-256 checksum
How to use checksums
43d57bfc5a6fae94e21ba876a27e7a7082735eab54199671c449cb1790a21a30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0.post20201221 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.6.7

Release files / napkin-0.6.7-py3-none-any.whl

Download URL napkin-0.6.7-py3-none-any.whl
Size 20.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2369c0df2f1474bf2fd322c487876209a0917a2d79090fbcca7b3ed3dd31d4e4
BLAKE2b-256 checksum
How to use checksums
6816fa5354349d672f2950444cad3033cc1abc7f0c19d6a5cac2a765d2cd108b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0.post20201221 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.6.7

Release history Release notifications | RSS feed

0.6.9

2 release files

0.6.8

2 release files

This release

0.6.7 This release

2 release files

0.6.6

2 release files

0.6.5

2 release files

0.6.4

2 release files

0.6.1

3 release files

0.6.0

3 release files

0.5.3

3 release files

0.5.2

3 release files

0.5.1

3 release files

0.5.0

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page