Skip to content

viz

This module provides utilities for converting an Abstract Syntax Tree (AST) to Mermaid for inline display in Jupyter (Lab ≥4.1 / NB ≥7.1) and to ASCII via the mermaid-ascii CLI.

Functions:

ast_to_mermaid

ast_to_mermaid(
    ast: ReprStruct, direction: Direction = "TD"
) -> str
Source code in packages/astx/src/astx/viz.py
151
152
153
154
155
156
157
158
159
160
161
162
def ast_to_mermaid(ast: ReprStruct, direction: Direction = "TD") -> str:
    """
    title: Mermaid for Jupyter/image (uses named items).
    parameters:
      ast:
        type: ReprStruct
      direction:
        type: Direction
    returns:
      type: str
    """
    return _traverse_ast_to_mermaid(ast, direction=direction, named_items=True)

ast_to_mermaid_ascii

ast_to_mermaid_ascii(
    ast: ReprStruct, direction: Direction = "TD"
) -> str

-

Mermaid tailored for mermaid-ascii, no named items, pipe edge labels.

Source code in packages/astx/src/astx/viz.py
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
def ast_to_mermaid_ascii(ast: ReprStruct, direction: Direction = "TD") -> str:
    """
    title: >-
      Mermaid tailored for mermaid-ascii, no named items, pipe edge labels.
    parameters:
      ast:
        type: ReprStruct
      direction:
        type: Direction
    returns:
      type: str
    """
    return _traverse_ast_to_mermaid(
        ast, direction=direction, named_items=False
    )

visualize_ascii

visualize_ascii(
    ast: ReprStruct,
    timeout: int = 10,
    direction: Direction = "TD",
    width: Optional[int] = None,
    border_padding: Optional[int] = 0,
    padding_x: Optional[int] = 3,
    padding_y: Optional[int] = 3,
    ascii_only: bool = False,
) -> str

For maximum compatibility, this uses an ASCII-friendly Mermaid form: - no named items (node text is the identifier) - pipe-labeled edges (--> |label| ...) parameters: ast: type: ReprStruct timeout: type: int direction: type: Direction width: type: Optional[int] border_padding: type: Optional[int] padding_x: type: Optional[int] padding_y: type: Optional[int] ascii_only: type: bool returns: type: str

Source code in packages/astx/src/astx/viz.py
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
def visualize_ascii(
    ast: ReprStruct,
    timeout: int = 10,
    direction: Direction = "TD",
    width: Optional[int] = None,
    border_padding: Optional[int] = 0,
    padding_x: Optional[int] = 3,
    padding_y: Optional[int] = 3,
    ascii_only: bool = False,
) -> str:
    """
    title: Render the AST to ASCII using the `mermaid-ascii` CLI.
    summary: |-

      For maximum compatibility, this uses an ASCII-friendly Mermaid form:
      - no named items (node text is the identifier)
      - pipe-labeled edges (--> |label| ...)
    parameters:
      ast:
        type: ReprStruct
      timeout:
        type: int
      direction:
        type: Direction
      width:
        type: Optional[int]
      border_padding:
        type: Optional[int]
      padding_x:
        type: Optional[int]
      padding_y:
        type: Optional[int]
      ascii_only:
        type: bool
    returns:
      type: str
    """
    exe = _find_mermaid_ascii()
    if exe is None:
        import yaml

        return str(yaml.dump(ast, sort_keys=False))

    src = ast_to_mermaid_ascii(ast, direction=direction)

    cmd = [exe]
    if width is not None:
        cmd += ["-x", str(width)]
    if border_padding is not None:
        cmd += ["--borderPadding", str(border_padding)]
    if padding_x is not None:
        cmd += ["--paddingX", str(padding_x)]
    if padding_y is not None:
        cmd += ["--paddingY", str(padding_y)]
    if ascii_only:
        cmd += ["--ascii"]

    proc = subprocess.run(
        cmd,
        input=src,
        text=True,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        timeout=timeout,
        check=False,
    )

    if proc.returncode != 0:
        first = src.splitlines()[0].strip() if src else ""
        hint = ""
        if "first line should define the graph" in (proc.stderr or ""):
            hint = f" (first line is {first!r}; try 'graph TD' or 'graph LR')"
        raise RuntimeError((proc.stderr or "mermaid-ascii failed.") + hint)

    if proc.stderr:
        raise RuntimeError(proc.stderr.strip())

    return proc.stdout

visualize_image

visualize_image(
    ast: ReprStruct, direction: Direction = "TD"
) -> None
Source code in packages/astx/src/astx/viz.py
182
183
184
185
186
187
188
189
190
191
192
193
194
def visualize_image(ast: ReprStruct, direction: Direction = "TD") -> None:
    """
    title: Display the AST as Mermaid inline in Jupyter (Lab ≥4.1 / NB ≥7.1).
    parameters:
      ast:
        type: ReprStruct
      direction:
        type: Direction
    """
    _display(  # type: ignore
        {"text/vnd.mermaid": ast_to_mermaid(ast, direction=direction)},
        raw=True,
    )