{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[DEBUG@graphviz.backend.execute] run [PosixPath('dot'), '-V']\n"
]
},
{
"data": {
"text/plain": [
"('0.20.dev0', (2, 40, 1))"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%cd -q ..\n",
"\n",
"import logging\n",
"\n",
"import graphviz\n",
"\n",
"logging.basicConfig(format='[%(levelname)s@%(name)s] %(message)s', level=logging.DEBUG)\n",
"\n",
"graphviz.__version__, graphviz.version()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[DEBUG@graphviz.backend.execute] run [PosixPath('dot'), '-Kdot', '-Tsvg']\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dot = graphviz.Digraph(comment='The Round Table')\n",
"\n",
"dot.node('A', 'King Arthur')\n",
"dot.node('B', 'Sir Bedevere the Wise')\n",
"dot.node('L', 'Sir Lancelot the Brave')\n",
"\n",
"dot.edges(['AB', 'AL'])\n",
"dot.edge('B', 'L', constraint='false')\n",
"\n",
"dot"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[DEBUG@graphviz.backend.execute] run [PosixPath('dot'), '-Kdot', '-Tsvg']\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"src = graphviz.Source('digraph \"the holy hand grenade\" { rankdir=LR; 1 -> 2 -> 3 -> lob }')\n",
"src"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[DEBUG@graphviz.backend.execute] run [PosixPath('dot'), '-Kdot', '-Tsvg']\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# http://www.graphviz.org/content/fsm\n",
"\n",
"f = graphviz.Digraph('finite_state_machine', filename='fsm.gv')\n",
"\n",
"f.attr(rankdir='LR', size='8,5')\n",
"\n",
"f.attr('node', shape='doublecircle')\n",
"f.node('LR_0')\n",
"f.node('LR_3')\n",
"f.node('LR_4')\n",
"f.node('LR_8')\n",
"\n",
"f.attr('node', shape='circle')\n",
"f.edge('LR_0', 'LR_2', label='SS(B)')\n",
"f.edge('LR_0', 'LR_1', label='SS(S)')\n",
"f.edge('LR_1', 'LR_3', label='S($end)')\n",
"f.edge('LR_2', 'LR_6', label='SS(b)')\n",
"f.edge('LR_2', 'LR_5', label='SS(a)')\n",
"f.edge('LR_2', 'LR_4', label='S(A)')\n",
"f.edge('LR_5', 'LR_7', label='S(b)')\n",
"f.edge('LR_5', 'LR_5', label='S(a)')\n",
"f.edge('LR_6', 'LR_6', label='S(b)')\n",
"f.edge('LR_6', 'LR_5', label='S(a)')\n",
"f.edge('LR_7', 'LR_8', label='S(b)')\n",
"f.edge('LR_7', 'LR_5', label='S(a)')\n",
"f.edge('LR_8', 'LR_6', label='S(b)')\n",
"f.edge('LR_8', 'LR_5', label='S(a)')\n",
"\n",
"f"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[DEBUG@graphviz.backend.execute] run [PosixPath('dot'), '-Kdot', '-Tsvg']\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# http://www.graphviz.org/pdf/dotguide.pdf, Figure 20\n",
"\n",
"g = graphviz.Digraph('G', filename='cluster_edge.gv')\n",
"\n",
"g.attr(compound='true')\n",
"\n",
"with g.subgraph(name='cluster0') as c:\n",
" c.edges(['ab', 'ac', 'bd', 'cd'])\n",
"\n",
"with g.subgraph(name='cluster1') as c:\n",
" c.edges(['eg', 'ef'])\n",
"\n",
"g.edge('b', 'f', lhead='cluster1')\n",
"g.edge('d', 'e')\n",
"g.edge('c', 'g', ltail='cluster0', lhead='cluster1')\n",
"g.edge('c', 'e', ltail='cluster0')\n",
"g.edge('d', 'h')\n",
"\n",
"g"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}