graphtty
graphtty is a Python library that transforms any directed graph into colored ASCII art, designed for display in your terminal. It is a pure Python solution with zero external dependencies. The library is currently at version 0.1.8 and is actively maintained by UiPath, requiring Python 3.11 or newer.
Common errors
-
ModuleNotFoundError: No module named 'graphtty'
cause The graphtty library is not installed in the current Python environment or the environment is not activated.fixRun `pip install graphtty` in your terminal to install the package. -
TypeError: render() missing 1 required positional argument: 'graph'
cause `graphtty.render` was called without providing the necessary graph dictionary.fixThe `render` function expects a dictionary argument that defines the graph's nodes and edges. Ensure you pass a valid graph dictionary, e.g., `render(my_graph_dict)`. -
KeyError: 'id' (or 'name') is missing from a node definition.
cause Node dictionaries within the 'nodes' list of your graph are missing the mandatory 'id' or 'name' keys. These keys are crucial for graphtty to identify and label nodes.fixEnsure every node dictionary in your graph includes at least both an 'id' (unique identifier) and a 'name' (display label) key.
Warnings
- gotcha Be aware of name collisions with other Python libraries like `graffiti`, `graphiti`, or `graphyte` that serve different purposes (e.g., declarative computation, temporal context graphs, Graphite metrics). Ensure you are importing `graphtty` from the correct package.
- gotcha graphtty is explicitly designed for *directed* graphs. While you can represent undirected relationships by adding two directed edges (A->B and B->A), the rendering will reflect these as distinct directed connections. Attempting to pass a graph structure not adhering to the directed graph format may lead to unexpected visual output.
Install
-
pip install graphtty
Imports
- render
from graphtty import render
Quickstart
from graphtty import render
graph = {
"nodes": [
{"id": "start", "name": "Initialize"},
{"id": "step1", "name": "Process Data", "description": "Extract, Transform"},
{"id": "step2", "name": "Analyze Results", "description": "Apply ML Model"},
{"id": "end", "name": "Final Output"}
],
"edges": [
{"source": "start", "target": "step1"},
{"source": "step1", "target": "step2"},
{"source": "step2", "target": "end"}
]
}
ascii_art = render(graph)
print(ascii_art)