Skip to content

Add workflow graph generation functionality #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/python-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ jobs:
- uses: "actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065" # v5.6.0
with:
python-version: '3.9'
- name: Install graphviz
run: |
sudo apt-get update
sudo apt-get install graphviz graphviz-dev
- name: Install dependencies
run: |
pip install pipenv
Expand Down
4 changes: 3 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ name = "pypi"
jsonschema = "==4.4.0"
pyyaml = "==6.0"
requests = "*"
pygraphviz = "==1.11"
transitions = "==0.9.2"

[dev-packages]
pytest = "==6.2.5"
pytest-runner = "==5.3.1"

[requires]
python_version = "3"
python_version = "3.9"
273 changes: 202 additions & 71 deletions Pipfile.lock

Large diffs are not rendered by default.

31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,33 @@ WorkflowValidator(Workflow(workflow)).validate()
```
The `validate` method will raise an exception if the provided workflow does not complaint specification.

You can see a full example in the [test_workflow_validator](tests/serverlessworkflow/sdk/test_workflow_validator.py) file
You can see a full example in the [test_workflow_validator](tests/serverlessworkflow/sdk/test_workflow_validator.py) file

## Generate workflow state machine and graph

To generate the workflow graph diagram:

```python
from serverlessworkflow.sdk.workflow import Workflow
from serverlessworkflow.sdk.state_machine_helper import StateMachineHelper

def main():
subflows = []
with open("tests/examples/graph.json") as f:
workflow = Workflow.from_source(f.read())
with open("tests/examples/advertise-listing.json") as f:
subflows.append(Workflow.from_source(f.read()))
with open("tests/examples/second-subgraph.json") as f:
subflows.append(Workflow.from_source(f.read()))
machine_helper = StateMachineHelper(workflow=workflow, get_actions=True, subflows=subflows)
machine_helper.draw('diagram.svg')


if __name__ == "__main__":
main()
```

The `StateMachineHelper` can be set with `get_actions` as `False` and the produced diagram will not represent the actions inside each state (it will only create a diagram with the states and their transitions). Moreover, the developer may not give any `subflows`, and they simply will not be generated.
As for the `draw` method, the developer can also specify `graph_engine='mermaid'`. In that case, the method will not generate a figure, but rather the Mermaid code that can be executed, for instance, in the [Mermaid Live Editor](https://mermaid.live).

It is also possible to only generate the workflow state machine. An example on how to do so can be analyzed in the [state_machine_helper](serverlessworkflow/sdk/state_machine_helper.py) source code.
Loading