diff --git a/python/poetry.lock b/python/poetry.lock index 71fa0539..4a3e859c 100644 --- a/python/poetry.lock +++ b/python/poetry.lock @@ -1,4 +1,4 @@ -# This file is automatically @generated by Poetry 1.7.1 and should not be changed by hand. +# This file is automatically @generated by Poetry 1.8.2 and should not be changed by hand. [[package]] name = "aiohttp" @@ -1073,6 +1073,22 @@ protobuf = ">=3.20.2,<4.21.1 || >4.21.1,<4.21.2 || >4.21.2,<4.21.3 || >4.21.3,<4 [package.extras] grpc = ["grpcio (>=1.44.0,<2.0.0.dev0)"] +[[package]] +name = "graphviz" +version = "0.20.3" +description = "Simple Python interface for Graphviz" +optional = false +python-versions = ">=3.8" +files = [ + {file = "graphviz-0.20.3-py3-none-any.whl", hash = "sha256:81f848f2904515d8cd359cc611faba817598d2feaac4027b266aa3eda7b3dde5"}, + {file = "graphviz-0.20.3.zip", hash = "sha256:09d6bc81e6a9fa392e7ba52135a9d49f1ed62526f96499325930e87ca1b5925d"}, +] + +[package.extras] +dev = ["flake8", "pep8-naming", "tox (>=3)", "twine", "wheel"] +docs = ["sphinx (>=5,<7)", "sphinx-autodoc-typehints", "sphinx-rtd-theme"] +test = ["coverage", "pytest (>=7,<8.1)", "pytest-cov", "pytest-mock (>=3)"] + [[package]] name = "grpcio" version = "1.64.1" @@ -3237,4 +3253,4 @@ wallet = ["fetchai-babble"] [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.13" -content-hash = "fad786cb200fdb317bb9d48864eccd765e794f66f0102dd28d6deb6c1b589614" +content-hash = "c1e9e6dab77d671af3cfc48321883b686dba21440671bec6f9bd6b006c06576f" diff --git a/python/pyproject.toml b/python/pyproject.toml index acf29216..a3de7209 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -37,6 +37,7 @@ mkdocs = "^1.4.2" mkdocs-material = "^9.1.13" ruff = "^0.3.4" pre-commit = "^3.6.2" +graphviz = "^0.20.3" [tool.poetry.extras] all = ["tortoise-orm", "geopy", "fetchai-babble", "pyngrok", "fastapi"] diff --git a/python/src/uagents/experimental/dialogues/dialogue_graph_generator.py b/python/src/uagents/experimental/dialogues/dialogue_graph_generator.py new file mode 100644 index 00000000..ab766cde --- /dev/null +++ b/python/src/uagents/experimental/dialogues/dialogue_graph_generator.py @@ -0,0 +1,25 @@ +import graphviz +from uagents.experimental.dialogues import Dialogue + + +def generate_graph(dialogue: Dialogue): + """Create a graphviz diagram of the Dialogue's state graph to visually verify it + + Note requires local installation of Graphviz, + e.g. `brew install graphviz` on macOS""" + + dot = graphviz.Digraph(comment=dialogue.name) + + for node in dialogue.nodes: + dot.node(node.name, str(node.name)) + + for edge in dialogue.edges: + dot.edge(str(edge.parent.name), str(edge.child.name), str(edge.name)) + + # the dot source gives a nice text based summary of the edges and nodes + print(dot.source) + + # rendering the graph creates a pdf file in the current directory and + # opens it automatically. If you omit the `view=True` parameter, it + # just creates the file without auto opening it. + dot.render(f"{dialogue.name.lower()}.gv", view=True)