Skip to main content

Python DSL for writing PlantUML sequence diagram

Project description

Napkin

Python as DSL for writing sequence diagram.

The sequence diagrams are useful tool to capture the S/W design and PlantUML is a great tool to write nice sequence diagrams in plain text.

However, the syntax of PlantUML is quite error prone especially when there are nested calls involved.

For example:

participant User

User -> Foo: DoWork()
activate Foo 

Foo -> Foo: InternalCall()
activate Foo

Foo -> Bar: CreateRequest()
activate Bar

Bar --> Foo: Request
deactivate Bar
deactivate Foo
deactivate Foo

By using normal Python code, it can be naturally expressed with 'with' statement as below:

@napkin.seq_diagram()
def sd_simple(c):
    user = c.object('User')
    foo = c.object('Foo')
    bar = c.object('Bar')

    with user:
        with foo.DoWork():
            with foo.InternalCall():
                with bar.CreateRequest():
                    c.ret('Done')

Basically, sequence diagram is expressed as methods calls between objects.

There are several advantages of using Python as DSL:

  • Easy to write 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.
  • Any Python editor can become sequence diagram editor

Installation

Install and update using pip

pip install -U 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.uml:

$ napkin hello.py

More examples

See example

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.1.tar.gz (10.6 kB view hashes)

Uploaded Source

Built Distributions

napkin-0.5.1-py3-none-any.whl (14.1 kB view hashes)

Uploaded Python 3

napkin-0.5.1-py2-none-any.whl (14.1 kB view hashes)

Uploaded Python 2

Supported by

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