Diagramsの使い方 – 初心者向けガイド

AI

Diagramsは、Pythonのコードを使ってシーケンス図、フローチャート、ネットワーク図などを作成するパッケージです。このガイドでは、初心者でも簡単にDiagramsを使いこなせるように、丁寧に説明していきます。


こちらの記事もおすすめ

PlantUMLでシーケンス図を描こう!
PlantUMLは、シンプルなテキストベースの言語を使ってUML図を描くことができるオープンソースツールです。Pythonから簡単にPlantUMLを使うことができ、シーケンス図やクラス図などを手軽に作成できます。ここでは、PlantUML...
SourceSage v2.0~Gitログも活用~ 高品質リリースノートを爆速作成
はじめにSourceSage 2.0.0のリリースを発表できることを嬉しく思います!このバージョンでは、ツールの使いやすさ、保守性、効率性を向上させるために、いくつかの重要な機能強化と新機能が導入されています。SourceSageは、プロジ...

インストール方法

Diagramsを使うには、以下のコマンドでパッケージをインストールします。

pip install diagrams

ローカル環境の場合

Diagramsを使うには、Graphvizというツールが必要です。以下のリンクからGraphvizをダウンロードしてインストールしてください。

Download
Graph Visualization Software

Dockerを使う場合

Dockerを使う場合は、以下のコマンドでDockerイメージを起動します。

docker-compose up

基本的な使い方

example01.py


from diagrams import Cluster, Diagram
from diagrams.aws.compute import EC2
from diagrams.aws.database import RDS
from diagrams.aws.network import ELB

with Diagram("/app/examples/diagrams/examples01", show=False):
    lb = ELB("Load Balancer")

    with Cluster("Web Servers"):
        web_servers = [EC2("Web Server 1"),
                       EC2("Web Server 2"),
                       EC2("Web Server 3")]

    with Cluster("Database Cluster"):
        db_primary = RDS("Primary")
        db_replica = RDS("Replica")

    lb >> web_servers >> db_primary
    db_primary - db_replica

このコードは、AWSのアーキテクチャを表現したシンプルな例です。

  1. 必要なクラスをインポートします。
from diagrams import Cluster, Diagram
from diagrams.aws.compute import EC2
from diagrams.aws.database import RDS 
from diagrams.aws.network import ELB
  1. Diagramクラスを使って図全体の設定を行います。
with Diagram("/app/examples/diagrams/examples01", show=False):
  • 第1引数は図の出力先パス、show=Falseは図を自動で表示しないという設定です。
  1. ロードバランサーを表すELBノードを定義します。
lb = ELB("Load Balancer")
  1. ウェブサーバーのクラスターを定義します。
with Cluster("Web Servers"):
web_servers = [EC2("Web Server 1"),
EC2("Web Server 2"),
EC2("Web Server 3")]
  • クラスター内のノードは、インデントを使って表現します。
  1. データベースクラスターを定義します。
with Cluster("Database Cluster"):
db_primary = RDS("Primary") 
db_replica = RDS("Replica")
  1. ノード間の関係を定義します。
lb >> web_servers >> db_primary
db_primary - db_replica  
  • >>は「から」「へ」の関係、-は双方向の関係を表します。

コードを実行すると、指定したパスに図が生成されます。

より複雑な例

example02.py

from diagrams import Cluster, Diagram
from diagrams.onprem.vcs import Git
from diagrams.azure.compute import FunctionApps
from diagrams.azure.ml import MachineLearningServiceWorkspaces
from diagrams.aws.management import Cloudformation
from diagrams.aws.integration import StepFunctions

graph_attr = {
    "fontsize": "35",
    # "bgcolor": "transparent"
}

with Diagram("/app/examples/diagrams/examples02", show=False, filename="SourceSage_Architecture", direction="LR", graph_attr=graph_attr):

    github = Git("GitHub Repository")

    with Cluster("SourceSage"):
        main = FunctionApps("SourceSage.py")

        with Cluster("Modules"):
            modules = [
                FunctionApps("GitHubIssueRetrieve"),
                FunctionApps("StagedDiffGenerator"),
                FunctionApps("ChangelogGenerator"),
                FunctionApps("StageInfoGenerator"),
                FunctionApps("IssuesToMarkdown")
            ]

        outputs = Cloudformation("Generated Assets")

    claude = MachineLearningServiceWorkspaces("Claude AI")

    github >> main
    main >> modules >> outputs
    outputs << StepFunctions("Feed") << claude

このコードは、SourceSageというプロジェクトのアーキテクチャを表現した例です。

  1. 必要なクラスをインポートします。
from diagrams import Cluster, Diagram
from diagrams.onprem.vcs import Git
from diagrams.azure.compute import FunctionApps
from diagrams.azure.ml import MachineLearningServiceWorkspaces
from diagrams.aws.management import Cloudformation
from diagrams.aws.integration import StepFunctions
  1. 図全体の属性を設定します。

    graph_attr = {
       "fontsize": "35", 
       # "bgcolor": "transparent"
    }
  2. Diagramクラスを使って図全体の設定を行います。

    with Diagram("/app/examples/diagrams/examples02", show=False, filename="SourceSage_Architecture", direction="LR", graph_attr=graph_attr):
    • direction="LR"で、図を左から右に流れるように設定しています。
  3. GitHubリポジトリを表すノードを定義します。

    github = Git("GitHub Repository")
  4. SourceSageのメインモジュールとサブモジュールを定義します。

    with Cluster("SourceSage"):
       main = FunctionApps("SourceSage.py")
    
       with Cluster("Modules"):
           modules = [
               FunctionApps("GitHubIssueRetrieve"),
               FunctionApps("StagedDiffGenerator"),
               FunctionApps("ChangelogGenerator"),
               FunctionApps("StageInfoGenerator"), 
               FunctionApps("IssuesToMarkdown")
           ]
    
       outputs = Cloudformation("Generated Assets")
  5. Claude AIを表すノードを定義します。

    claude = MachineLearningServiceWorkspaces("Claude AI")
  6. ノード間の関係を定義します。

    github >> main
    main >> modules >> outputs
    outputs << StepFunctions("Feed") << claude

コードを実行すると、指定したパスに図が生成されます。

example03.py

from diagrams import Cluster, Diagram, Edge
from diagrams.onprem.vcs import Git
from diagrams.azure.compute import FunctionApps
from diagrams.azure.ml import MachineLearningServiceWorkspaces
from diagrams.aws.management import Cloudformation
from diagrams.aws.integration import StepFunctions

graph_attr = {
    "fontsize": "35",
    # "bgcolor": "transparent"
}

with Diagram("/app/examples/diagrams/examples03", show=False, filename="SourceSage_Architecture", direction="LR", graph_attr=graph_attr):

    github = Git("GitHub Repository")

    with Cluster("SourceSage"):
        main = FunctionApps("SourceSage.py")

        with Cluster("Modules"):
            modules = [
                FunctionApps("GitHubIssueRetrieve"),
                FunctionApps("StagedDiffGenerator"),
                FunctionApps("ChangelogGenerator"),
                FunctionApps("StageInfoGenerator"),
                FunctionApps("IssuesToMarkdown")
            ]

        outputs = Cloudformation("Generated Assets")

    claude = MachineLearningServiceWorkspaces("Claude AI")

    github >> Edge(color="lightblue", style="bold") >> main
    main >> Edge(color="lightgreen", style="bold") >> modules >> Edge(color="lightpink", style="bold") >> outputs
    outputs << Edge(color="lightyellow", style="bold") << StepFunctions("Feed") << Edge(color="lightsalmon", style="bold") << claude

このコードは、example02.pyとほぼ同じですが、ノード間の関係を表す矢印にカスタムスタイルを適用しています。

  1. 必要なクラスをインポートします。

    from diagrams import Cluster, Diagram, Edge
    from diagrams.onprem.vcs import Git
    from diagrams.azure.compute import FunctionApps
    from diagrams.azure.ml import MachineLearningServiceWorkspaces
    from diagrams.aws.management import Cloudformation
    from diagrams.aws.integration import StepFunctions
    • Edgeクラスを追加でインポートしています。
  2. 図全体の属性を設定します。(example02.pyと同じ)

  3. Diagramクラスを使って図全体の設定を行います。(example02.pyと同じ)

  4. GitHubリポジトリを表すノードを定義します。(example02.pyと同じ)

  5. SourceSageのメインモジュールとサブモジュールを定義します。(example02.pyと同じ)

  6. Claude AIを表すノードを定義します。(example02.pyと同じ)

  7. ノード間の関係を定義します。ここでEdgeクラスを使ってスタイルを適用しています。

    github >> Edge(color="lightblue", style="bold") >> main
    main >> Edge(color="lightgreen", style="bold") >> modules >> Edge(color="lightpink", style="bold") >> outputs
    outputs << Edge(color="lightyellow", style="bold") << StepFunctions("Feed") << Edge(color="lightsalmon", style="bold") << claude
    • Edgeクラスの引数で色やスタイルを指定しています。

コードを実行すると、指定したパスに図が生成されます。矢印にカスタムスタイルが適用されているのが分かります。

カスタムアイコンを使う

example04.py

from diagrams import Diagram, Cluster
from diagrams.custom import Custom
from urllib.request import urlretrieve

with Diagram("Custom with remote icons", show=False, filename="custom_remote", direction="LR"):

  # download the icon image file
  diagrams_url = "https://raw.githubusercontent.com/Sunwood-ai-labs/PythonDiagrammatic/feature/Diagrams/examples/diagrams/icon/diagrams_icon1.png"
  diagrams_icon = "diagrams.png"
  urlretrieve(diagrams_url, diagrams_icon)

  diagrams = Custom("Diagrams", diagrams_icon)

  with Cluster("Some Providers"):

    openstack_url = "https://raw.githubusercontent.com/Sunwood-ai-labs/PythonDiagrammatic/feature/Diagrams/examples/diagrams/icon/diagrams_icon2.png"
    openstack_icon = "openstack.png"
    urlretrieve(openstack_url, openstack_icon)

    openstack = Custom("OpenStack", openstack_icon)

    elastic_url = "https://raw.githubusercontent.com/Sunwood-ai-labs/PythonDiagrammatic/feature/Diagrams/examples/diagrams/icon/diagrams_icon3.png"
    elastic_icon = "elastic.png"
    urlretrieve(elastic_url, elastic_icon)

    elastic = Custom("Elastic", elastic_icon)

  diagrams >> openstack
  diagrams >> elastic

このコードは、リモートのカスタムアイコンを使って図を作成する例です。

  1. 必要なクラスをインポートします。

    from diagrams import Diagram, Cluster
    from diagrams.custom import Custom
    from urllib.request import urlretrieve
    • Customクラスを使ってカスタムアイコンを定義します。
    • urlretrieve関数を使ってリモートのアイコン画像をダウンロードします。
  2. Diagramクラスを使って図全体の設定を行います。

    with Diagram("Custom with remote icons", show=False, filename="custom_remote", direction="LR"):
  3. Diagramsのアイコンをダウンロードし、Customクラスを使ってノードを定義します。

    diagrams_url = "https://raw.githubusercontent.com/Sunwood-ai-labs/PythonDiagrammatic/feature/Diagrams/examples/diagrams/icon/diagrams_icon1.png"
    diagrams_icon = "diagrams.png"
    urlretrieve(diagrams_url, diagrams_icon)
    
    diagrams = Custom("Diagrams", diagrams_icon)
  4. OpenStackとElasticのアイコンをダウンロードし、Customクラスを使ってノードを定義します。

    with Cluster("Some Providers"):
    
     openstack_url = "https://raw.githubusercontent.com/Sunwood-ai-labs/PythonDiagrammatic/feature/Diagrams/examples/diagrams/icon/diagrams_icon2.png"
     openstack_icon = "openstack.png"
     urlretrieve(openstack_url, openstack_icon)
    
     openstack = Custom("OpenStack", openstack_icon)
    
     elastic_url = "https://raw.githubusercontent.com/Sunwood-ai-labs/PythonDiagrammatic/feature/Diagrams/examples/diagrams/icon/diagrams_icon3.png"
     elastic_icon = "elastic.png"
     urlretrieve(elastic_url, elastic_icon)
    
     elastic = Custom("Elastic", elastic_icon)
  5. ノード間の関係を定義します。

    diagrams >> openstack
    diagrams >> elastic

コードを実行すると、指定したパスに図が生成されます。リモートのカスタムアイコンが使われているのが分かります。

example05.py

from diagrams import Diagram, Cluster
from diagrams.custom import Custom

with Diagram("Custom with local icons", show=False, filename="custom_local", direction="LR"):
  cc_heart = Custom("Creative Commons", "./examples/diagrams/icon/diagrams_icon1.png")
  cc_attribution = Custom("Credit must be given to the creator", "./examples/diagrams/icon/diagrams_icon2.png")

  cc_sa = Custom("Adaptations must be shared\n under the same terms", "./examples/diagrams/icon/diagrams_icon3.png")
  cc_nd = Custom("No derivatives or adaptations\n of the work are permitted", "./examples/diagrams/icon/diagrams_icon4.png")
  cc_zero = Custom("Public Domain Dedication", "./examples/diagrams/icon/diagrams_icon2.png")

  with Cluster("Non Commercial"):
    non_commercial = [Custom("Y", "./examples/diagrams/icon/diagrams_icon8.png") - Custom("E", "./examples/diagrams/icon/diagrams_icon7.png") - Custom("S", "./examples/diagrams/icon/diagrams_icon1.png")]

  cc_heart >> cc_attribution
  cc_heart >> non_commercial
  cc_heart >> cc_sa
  cc_heart >> cc_nd
  cc_heart >> cc_zero

このコードは、ローカルのカスタムアイコンを使ってCreative Commonsのライセンスを表現した例です。

  1. 必要なクラスをインポートします。

    from diagrams import Diagram, Cluster
    from diagrams.custom import Custom
  2. Diagramクラスを使って図全体の設定を行います。

    with Diagram("Custom with local icons", show=False, filename="custom_local", direction="LR"):
  3. Creative CommonsのアイコンをCustomクラスを使って定義します。

    cc_heart = Custom("Creative Commons", "./examples/diagrams/icon/diagrams_icon1.png")
    cc_attribution = Custom("Credit must be given to the creator", "./examples/diagrams/icon/diagrams_icon2.png")
    
    cc_sa = Custom("Adaptations must be shared\n under the same terms", "./examples/diagrams/icon/diagrams_icon3.png")
    cc_nd = Custom("No derivatives or adaptations\n of the work are permitted", "./examples/diagrams/icon/diagrams_icon4.png")
    cc_zero = Custom("Public Domain Dedication", "./examples/diagrams/icon/diagrams_icon2.png")
    • 各アイコンのパスを指定して、Customクラスでノードを定義しています。
  4. "Non Commercial"クラスターを定義します。

    with Cluster("Non Commercial"):
     non_commercial = [Custom("Y", "./examples/diagrams/icon/diagrams_icon8.png") - Custom("E", "./examples/diagrams/icon/diagrams_icon7.png") - Custom("S", "./examples/diagrams/icon/diagrams_icon1.png")]
    • クラスター内のノードを-で連結しています。
  5. ノード間の関係を定義します。

    cc_heart >> cc_attribution
    cc_heart >> non_commercial
    cc_heart >> cc_sa
    cc_heart >> cc_nd 
    cc_heart >> cc_zero

コードを実行すると、指定したパスに図が生成されます。ローカルのカスタムアイコンを使ってCreative Commonsのライセンスが表現されています。

以上が、5つの例の詳細な解説です。これらの例を参考に、自分だけの図を作ってみてください。

Diagramsは、Pythonのコードを使って簡単に図を作成できるパワフルなツールです。以下の点を押さえておくと、より効果的に活用できるでしょう。

  • Diagramクラスで図全体の設定を行う
  • Clusterクラスでノードをグループ化する
  • Customクラスでカスタムアイコンを使う
  • Edgeクラスでノード間の関係にスタイルを適用する
  • ノード間の関係は>>-などの演算子で表現する

これらの機能を組み合わせることで、複雑なアーキテクチャやワークフローを分かりやすく可視化できます。ぜひ、Diagramsを使ってあなたのアイデアを図に表現してみてください!

まとめ

  • Diagramsは、Pythonのコードを使って図を作成するパッケージ
  • インストールはpip install diagramsで行う
  • ローカル環境ではGraphvizが必要、Dockerを使う方法もある
  • Diagramクラスで図全体の設定、Clusterで要素をグループ化
  • >>-などの演算子で要素間の関係を定義
  • Customクラスでカスタムアイコンを使える

以上が、Diagramsの基本的な使い方です。この知識を元に、自分だけの図を作ってみてください。徐々に複雑な図にチャレンジしていきましょう!

リポジトリ

GitHub - Sunwood-ai-labs/PythonDiagrammatic
Contribute to Sunwood-ai-labs/PythonDiagrammatic development by creating an account on GitHub.

コメント

タイトルとURLをコピーしました