「 Python で Graphviz ってどうやって使うんだぜ?」
📖 Python上でGraphvizを使って綺麗なグラフを描く
📖 pythonでgraphvizの使い方(美しいツリー構造や状態遷移図の描画)
「 Windows に Graphviz をインストールするには どうやったらいいんだぜ?」
📖 Download
pip install graphviz
「 👆 じゃあ Python でも使えるようにインストールしろだぜ」
graphviz.backend.execute.ExecutableNotFound: failed to execute WindowsPath('dot'), make sure the Graphviz executables are on your systems' PATH
「 Visual Studio Code を全て閉じてから ターミナルを開けだぜ」
「 じゃあ試しに CSAサーバープロトコル1.2.1 を描いてみてちょうだい」
from graphviz import Digraph
# グラフの設定
g = Digraph(format="png")
g.attr("node", shape="square", style="filled")
# クラスター 'cluster_' から名前を始める必要あり
with g.subgraph(name='cluster_root') as c:
# 一番外側のクラスターのラベルは図のタイトルのように見える
c.attr(color='white', label='CSA Server protocol 1.2.1')
# 開始ノード
c.node("Start", shape="circle", color="pink")
# 2つのノードと、その二間の有向な辺
c.edge("Start", "1", label="Start")
with c.subgraph(name='cluster_init') as c2:
c2.attr(color='pink', label='Init')
c2.node('1')
c2.node('Login')
c2.edge('1', 'Login', label='login')
c2.edge('Login', '1', label='incorrect')
with c.subgraph(name='cluster_lobby') as c2:
c2.attr(color='pink', label='Lobby')
c2.node('2')
c2.node('Logout')
c2.edge("2", "Logout", label="logout")
with c.subgraph(name='cluster_reply') as c2:
c2.attr(color='pink', label='Reply')
c2.node('3')
c2.node('Agree')
c2.node('Reject')
c2.edge("3", "Agree", label="agree")
c2.edge("3", "Reject", label="reject")
with c.subgraph(name='cluster_game') as c2:
c2.attr(color='pink', label='Game')
c2.node('4')
c2.edge("4", "4", label="Move")
c2.edge("4", "4", label="MoveEcho")
c.edge("Login", "2", label="ok")
c.edge("Logout", "1", label="completed")
c.edge("Reject", "2", label="reject")
c.edge("2", "3", label="game_summary")
c.edge("Agree", "4", label="start")
c.edge("4", "2", label="GameoverWcsc")
c.edge("4", "1", label="GameoverFloodgate")
# 描画
g.render("graphs")
Crieitは個人で開発中です。
興味がある方は是非記事の投稿をお願いします! どんな軽い内容でも嬉しいです。
なぜCrieitを作ろうと思ったか
また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!
こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください!