Visualization API¶
Generate Mermaid, Graphviz DOT, or SCXML diagrams from a StateMachine definition.
See Visualization guide for the full guide with examples.
Functions¶
to_mermaid
¶
to_mermaid(machine: StateMachine, title: str | None = None, show_guards: bool = True, show_actions: bool = False, show_timeouts: bool = True, direction: str = 'TB') -> str
Generate a Mermaid stateDiagram-v2 from a state machine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
machine
|
StateMachine
|
The state machine to visualize. |
required |
title
|
str | None
|
Optional title for the diagram. |
None
|
show_guards
|
bool
|
If True, show guard names on transitions. |
True
|
show_actions
|
bool
|
If True, show action names on transitions. |
False
|
show_timeouts
|
bool
|
If True, show timeout transitions. |
True
|
direction
|
str
|
Diagram direction: "TB" (top-bottom), "LR" (left-right), "BT" (bottom-top), "RL" (right-left). |
'TB'
|
Returns:
| Type | Description |
|---|---|
str
|
Mermaid diagram string. |
Example
machine = StateMachine.from_yaml("order_fsm.yaml") diagram = to_mermaid(machine, title="Order Lifecycle") print(diagram)
title: Order Lifecycle¶
stateDiagram-v2 direction TB [*] → PENDING_NEW PENDING_NEW → OPEN : broker_ack ...
Source code in src/pystator/visualization.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
to_mermaid_flowchart
¶
to_mermaid_flowchart(machine: StateMachine, title: str | None = None, show_guards: bool = True, direction: str = 'TB') -> str
Generate a Mermaid flowchart from a state machine.
Alternative visualization using flowchart syntax.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
machine
|
StateMachine
|
The state machine to visualize. |
required |
title
|
str | None
|
Optional title for the diagram. |
None
|
show_guards
|
bool
|
If True, show guard names on transitions. |
True
|
direction
|
str
|
Diagram direction. |
'TB'
|
Returns:
| Type | Description |
|---|---|
str
|
Mermaid flowchart string. |
Source code in src/pystator/visualization.py
to_dot
¶
to_dot(machine: StateMachine, title: str | None = None, show_guards: bool = True, show_actions: bool = False) -> str
Generate a Graphviz DOT diagram from a state machine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
machine
|
StateMachine
|
The state machine to visualize. |
required |
title
|
str | None
|
Optional title for the diagram. |
None
|
show_guards
|
bool
|
If True, show guard names on transitions. |
True
|
show_actions
|
bool
|
If True, show action names on transitions. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
DOT diagram string. |
Source code in src/pystator/visualization.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 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 | |
to_scxml
¶
to_scxml(machine: StateMachine, name: str | None = None) -> str
Generate an SCXML representation of the state machine.
Produces a valid SCXML document using only the Python stdlib
xml.etree.ElementTree module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
machine
|
StateMachine
|
The state machine to export. |
required |
name
|
str | None
|
Optional machine name (defaults to |
None
|
Returns:
| Type | Description |
|---|---|
str
|
SCXML XML string. |
Source code in src/pystator/visualization.py
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
get_statistics
¶
get_statistics(machine: StateMachine) -> dict
Get statistics about a state machine.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
machine
|
StateMachine
|
The state machine to analyze. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with statistics. |
Source code in src/pystator/visualization.py
Import¶
from pystator.visualization import (
to_mermaid,
to_mermaid_flowchart,
to_dot,
to_scxml,
get_statistics,
)