Visualization: Generating Mermaid Diagrams
Chorey's visualization utility converts the internal, linked structure of your pipeline (Step
, Branch
, Route
objects) into a text-based diagram format called Mermaid. This output can be rendered by any tool or platform that supports Mermaid (like GitHub, GitLab, and the official Mermaid Live Editor).
How it Works
The visualization works by recursively traversing the pipeline structure backward, from the final step back to the initial input.
- Traversal: The
mermaid(step)
function starts at the last step and follows the previous link backward, collecting all pipeline components - Node Conversion: Each component is mapped to a specialized Mermaid Node representation
- Input/Output: A dedicated Input node is placed at the start, and an Output node is placed at the end.
- Type Labels: Crucially, the edges (arrows) connecting the nodes are labeled with the data type being passed between the steps.
- Control Flow Logic:
- Steps are rendered as rectangles with the function or custom name and an optional description.
- Branches are rendered by connecting the node before the branch to all the first steps within the branches, and all the last steps within the branches to the node after the merge.
- Routes are rendered as diamond shapes labeled with the
decision_label
, with labeled arrows (e.g., "Choice 0") leading to the different conditional paths.
Using the mermaid()
Function
The utility is accessed by calling the mermaid()
function directly with the final Step
object of your pipeline.
Example
Assuming you have defined a complex pipeline named analysis_pipeline
:
from chorey import mermaid, step
# (Assume the complex pipeline definition from the Route guide)
# ...
# 1. Define the pipeline
analysis_pipeline = (
step(fetch_document)
.route(
step(summarize_document),
step(analyze_document),
selector=selector,
decision_label="Document Size Check"
)
.next(store_processing_result)
)
# 2. Generate the Mermaid code
mermaid_code = mermaid(analysis_pipeline)
# 3. Print the result
print(mermaid_code)
Output Format
The output is a string formatted in the Mermaid syntax, ready to be copied and pasted:
flowchart TD
id-7807c6369b204d1a957d2cc64d781c4b -->|"None"| output["Output"]
id-a6c3bc85d6624d95bfd0721858370760 -->|"Summary"| id-7807c6369b204d1a957d2cc64d781c4b["<b>store_processing_result</>"]
id-ee0dec5fee4e4d38acd2473c22ee1343 -->|"FullAnalysis"| id-7807c6369b204d1a957d2cc64d781c4b["<b>store_processing_result</>"]
id-3aa2c4f22fdb4269afdd21406da63fe3{"Document Length Check"}
id-b1b7dda764394941a31756511d97a76d -->|"Document"| id-3aa2c4f22fdb4269afdd21406da63fe3
id-3aa2c4f22fdb4269afdd21406da63fe3 -->|"Choice 0 (Document)"| id-a6c3bc85d6624d95bfd0721858370760["<b>summarize_document</>"]
id-3aa2c4f22fdb4269afdd21406da63fe3 -->|"Choice 1 (Document)"| id-ee0dec5fee4e4d38acd2473c22ee1343["<b>analyze_document</>"]
input -->|"int"| id-b1b7dda764394941a31756511d97a76d["<b>fetch_document</>"]
input["Input"]
Note: The IDs are generated only for uniqueness and will differ each time.