Python で PlantUML の作図を行う
Plantweb は Python ベースで PlantUML ライクな作図を実行出来るツールです。 今回は Plantweb を使って作図する方法をメモしておきます。
検証環境¶
| 対象 | バージョン | 
|---|---|
| macOS | 14.5 | 
| Python | 3.11.9 | 
インストール¶
plantweb を pip でインストールします。 今回は uv を使ってインストールします。
uv pip install plantweb
基本的な使い方¶
plantweb を実行する為の入力ファイルを作成します。 今回は以下の内容にしました。
| sample.dot | |
|---|---|
| 1 2 3 4 5 |  | 
作成した .dot ファイルを引数に plantweb を実行します。 オプションを指定しない場合、SVG 形式で出力されます。
# plantweb sample.dot
Writing output for /path/to/sample.dot to sample.svg
具体的には以下の画像が出力されました。
PNG 形式で出力する¶
PNG 形式で出力したい場合は --format png オプションを指定します。
plantweb --format png sample.dot
入出力例¶
サンプル 1¶
入力¶
@startuml test
node "node1" {
    agent "Gi2" as node1.Gi2
}
node "node2" {
    agent "Gi1" as node2.Gi1
    agent "Gi2" as node2.Gi2
}
node "node3" {
    agent "Gi1" as node3.Gi1
    agent "Gi2" as node3.Gi2
}
node "node4" {
    agent "Gi1" as node4.Gi1
    agent "Gi2" as node4.Gi2
}
node "node5" {
    agent "Gi2" as node5.Gi1
}
node1.Gi2 - node2.Gi1
node2.Gi2 - node3.Gi1
node3.Gi2 - node4.Gi1
node4.Gi2 - node5.Gi1
@enduml
出力¶
サンプル 2¶
入力¶
@startuml
nwdiag {
  network NW1 {
      address = "10.0.1.x/24"
      node1 [address = "10.0.1.1"];
      node2 [address = "10.0.1.2"];
  }
  network NW2 {
      address = "10.0.2.x/24"
      node2 [address = "10.0.2.2"];
      node3 [address = "10.0.2.3"];
  }
  network NW3 {
      address = "10.0.3.x/24"
      node3 [address = "10.0.3.3"];
      node4 [address = "10.0.3.4"];
  }
  network NW4 {
      address = "10.0.4.x/24"
      node4 [address = "10.0.4.4"];
      node5 [address = "10.0.4.5"];
  }
}
@enduml
出力¶
サンプル 3¶
入力¶
digraph finite_state_machine {
    rankdir=LR;
    size="8,5"
    node [shape = doublecircle]; LR_0 LR_3 LR_4 LR_8;
    node [shape = circle];
    LR_0 -> LR_2 [ label = "SS(B)" ];
    LR_0 -> LR_1 [ label = "SS(S)" ];
    LR_1 -> LR_3 [ label = "S($end)" ];
    LR_2 -> LR_6 [ label = "SS(b)" ];
    LR_2 -> LR_5 [ label = "SS(a)" ];
    LR_2 -> LR_4 [ label = "S(A)" ];
    LR_5 -> LR_7 [ label = "S(b)" ];
    LR_5 -> LR_5 [ label = "S(a)" ];
    LR_6 -> LR_6 [ label = "S(b)" ];
    LR_6 -> LR_5 [ label = "S(a)" ];
    LR_7 -> LR_8 [ label = "S(b)" ];
    LR_7 -> LR_5 [ label = "S(a)" ];
    LR_8 -> LR_6 [ label = "S(b)" ];
    LR_8 -> LR_5 [ label = "S(a)" ];
}
出力¶
サンプル 4¶
入力¶
@startuml
nwdiag {
  group nightly {
    color = "#FFAAAA";
    description = "<&clock> Restarted nightly <&clock>";
    web02;
    db01;
  }
  network dmz {
      address = "210.x.x.x/24"
      user [description = "<&person*4.5>\n user1"];
      // set multiple addresses (using comma)
      web01 [address = "210.x.x.1, 210.x.x.20",  description = "<&cog*4>\nweb01"]
      web02 [address = "210.x.x.2",  description = "<&cog*4>\nweb02"];
  }
  network internal {
      address = "172.x.x.x/24";
      web01 [address = "172.x.x.1"];
      web02 [address = "172.x.x.2"];
      db01 [address = "172.x.x.100",  description = "<&spreadsheet*4>\n db01"];
      db02 [address = "172.x.x.101",  description = "<&spreadsheet*4>\n db02"];
      ptr  [address = "172.x.x.110",  description = "<&print*4>\n ptr01"];
  }
}
@enduml
出力¶
参考¶
plantweb のヘルプ表示¶
$ plantweb --help
usage: plantweb [-h] [-v] [--version] [--engine {auto,plantuml,graphviz,ditaa}] [--format {auto,svg,png}]
                [--server SERVER] [--no-cache] [--cache-dir CACHE_DIR]
                sources [sources ...]
Python client for the PlantUML server
positional arguments:
  sources               source files to render
options:
  -h, --help            show this help message and exit
  -v, --verbose         increase verbosity level
  --version             show program's version number and exit
  --engine {auto,plantuml,graphviz,ditaa}
                        engine to use to render diagram
  --format {auto,svg,png}
                        diagram export format
  --server SERVER       server to use for rendering
  --no-cache            do not use cache
  --cache-dir CACHE_DIR
                        directory to store cached renders