Python: Workflow visualization. (#372)

* Workflow visualization.

* fix typing

* address comments

* update all samples

* Update fan in edge group visual

* fix quality check

* Update python/samples/getting_started/workflow/step_06_map_reduce.py

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* address comment

* Catch up with changes in workflow; add to_mermaid method

* Update examples

* fix test

* add installation guide to error messages

* Remove visualization for samples except for one.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
This commit is contained in:
Eric Zhu
2025-08-19 17:07:49 -07:00
committed by GitHub
Unverified
parent baaa9c0aee
commit 6900d3456c
11 changed files with 617 additions and 4 deletions
@@ -0,0 +1,11 @@
# Workflow Getting Started Samples
## Installation
You can install the workflow package with visualization dependency:
```bash
pip install agent-framework-workflow[viz]
```
To export visualization images you also need to [install GraphViz](https://graphviz.org/download/).
@@ -2,7 +2,13 @@
import asyncio
from agent_framework.workflow import Executor, WorkflowBuilder, WorkflowCompletedEvent, WorkflowContext, handler
from agent_framework.workflow import (
Executor,
WorkflowBuilder,
WorkflowCompletedEvent,
WorkflowContext,
handler,
)
"""
The following sample demonstrates a basic workflow with two executors
@@ -2,7 +2,13 @@
import asyncio
from agent_framework.workflow import Executor, WorkflowBuilder, WorkflowCompletedEvent, WorkflowContext, handler
from agent_framework.workflow import (
Executor,
WorkflowBuilder,
WorkflowCompletedEvent,
WorkflowContext,
handler,
)
"""
The following sample demonstrates a basic workflow with two executors
@@ -12,6 +12,7 @@ from agent_framework.workflow import (
WorkflowBuilder,
WorkflowCompletedEvent,
WorkflowContext,
WorkflowViz,
handler,
)
@@ -23,6 +24,8 @@ to a final count per word.
Intermediate results are stored in a temporary directory, and the
final results are written to a file in the same directory.
This sample also shows how you can visualize a workflow using `WorkflowViz`.
"""
# Define the temporary directory for storing intermediate results
@@ -286,6 +289,24 @@ async def main():
.build()
)
# Step 2.5: Visualize the workflow (optional)
print("🎨 Generating workflow visualization...")
viz = WorkflowViz(workflow)
# Print out the mermaid string.
print("🧜 Mermaid string: \n=======")
print(viz.to_mermaid())
print("=======")
# Print out the DiGraph string.
print("📊 DiGraph string: \n=======")
print(viz.to_digraph())
print("=======")
try:
# Export the DiGraph visualization as SVG.
svg_file = viz.export(format="svg")
print(f"🖼️ SVG file saved to: {svg_file}")
except ImportError:
print("💡 Tip: Install 'viz' extra to export workflow visualization: pip install agent-framework-workflow[viz]")
# Step 3: Open the text file and read its content.
async with aiofiles.open(os.path.join(DIR, "resources", "long_text.txt"), "r") as f:
raw_text = await f.read()