draw_ascii() — langchain Function Reference
Architecture documentation for the draw_ascii() function in graph_ascii.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 89ad4f5d_065c_e8f6_983b_d18b7460b090["draw_ascii()"] bd48fe30_3177_b41c_8c8f_66e31a6b4ecb["graph_ascii.py"] 89ad4f5d_065c_e8f6_983b_d18b7460b090 -->|defined in| bd48fe30_3177_b41c_8c8f_66e31a6b4ecb 8e2d21bc_e6f4_f21a_c769_7c8767bfdb7c["_build_sugiyama_layout()"] 89ad4f5d_065c_e8f6_983b_d18b7460b090 -->|calls| 8e2d21bc_e6f4_f21a_c769_7c8767bfdb7c 1dbc3354_2bab_b41d_7aa9_14f4624a2a20["line()"] 89ad4f5d_065c_e8f6_983b_d18b7460b090 -->|calls| 1dbc3354_2bab_b41d_7aa9_14f4624a2a20 333b0598_86ec_e08e_fb9e_97bb5d448e1d["box()"] 89ad4f5d_065c_e8f6_983b_d18b7460b090 -->|calls| 333b0598_86ec_e08e_fb9e_97bb5d448e1d 53579b11_7da2_b348_12e0_809b3a0e0405["text()"] 89ad4f5d_065c_e8f6_983b_d18b7460b090 -->|calls| 53579b11_7da2_b348_12e0_809b3a0e0405 ec70c8d9_4454_4221_ac85_d75695eb85fc["draw()"] 89ad4f5d_065c_e8f6_983b_d18b7460b090 -->|calls| ec70c8d9_4454_4221_ac85_d75695eb85fc style 89ad4f5d_065c_e8f6_983b_d18b7460b090 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/core/langchain_core/runnables/graph_ascii.py lines 247–366
def draw_ascii(vertices: Mapping[str, str], edges: Sequence[LangEdge]) -> str:
"""Build a DAG and draw it in ASCII.
Args:
vertices: list of graph vertices.
edges: list of graph edges.
Raises:
ValueError: if the canvas dimensions are invalid or if
edge coordinates are invalid.
Returns:
ASCII representation
Example:
```python
from langchain_core.runnables.graph_ascii import draw_ascii
vertices = {1: "1", 2: "2", 3: "3", 4: "4"}
edges = [
(source, target, None, None)
for source, target in [(1, 2), (2, 3), (2, 4), (1, 4)]
]
print(draw_ascii(vertices, edges))
```
```txt
+---+
| 1 |
+---+
* *
* *
* *
+---+ *
| 2 | *
+---+** *
* ** *
* ** *
* **
+---+ +---+
| 3 | | 4 |
+---+ +---+
```
"""
# NOTE: coordinates might me negative, so we need to shift
# everything to the positive plane before we actually draw it.
xlist: list[float] = []
ylist: list[float] = []
sug = _build_sugiyama_layout(vertices, edges)
for vertex in sug.g.sV:
# NOTE: moving boxes w/2 to the left
xlist.extend(
(
vertex.view.xy[0] - vertex.view.w / 2.0,
vertex.view.xy[0] + vertex.view.w / 2.0,
)
)
ylist.extend((vertex.view.xy[1], vertex.view.xy[1] + vertex.view.h))
for edge in sug.g.sE:
for x, y in edge.view.pts:
xlist.append(x)
ylist.append(y)
minx = min(xlist)
miny = min(ylist)
maxx = max(xlist)
maxy = max(ylist)
canvas_cols = math.ceil(math.ceil(maxx) - math.floor(minx)) + 1
canvas_lines = round(maxy - miny)
canvas = AsciiCanvas(canvas_cols, canvas_lines)
# NOTE: first draw edges so that node boxes could overwrite them
for edge in sug.g.sE:
Domain
Subdomains
Source
Frequently Asked Questions
What does draw_ascii() do?
draw_ascii() is a function in the langchain codebase, defined in libs/core/langchain_core/runnables/graph_ascii.py.
Where is draw_ascii() defined?
draw_ascii() is defined in libs/core/langchain_core/runnables/graph_ascii.py at line 247.
What does draw_ascii() call?
draw_ascii() calls 5 function(s): _build_sugiyama_layout, box, draw, line, text.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free