Skip to content

Commit 65f9944

Browse files
committed
docs(extensions): add documentation about mermaid
1 parent 97da710 commit 65f9944

File tree

1 file changed

+80
-5
lines changed

1 file changed

+80
-5
lines changed

README.md

+80-5
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,80 @@ Additional Keywords:
14151415
- `show_auto_transitions` (default False): Shows auto transitions in graph
14161416
- `show_state_attributes` (default False): Show callbacks (enter, exit), tags and timeouts in graph
14171417

1418-
Transitions can generate basic state diagrams displaying all valid transitions between states. To use the graphing functionality, you'll need to have `graphviz` and/or `pygraphviz` installed:
1418+
Transitions can generate basic state diagrams displaying all valid transitions between states.
1419+
The basic diagram support generates a [mermaid](https://mermaid.js.org) state machine definition which can be used with mermaid's [live editor](https://mermaid.live), in markdown files in GitLab or GitHub and other web services.
1420+
For instance, this code:
1421+
```python
1422+
from transitions.extensions.diagrams import HierarchicalGraphMachine
1423+
import pyperclip
1424+
1425+
states = ['A', 'B', {'name': 'C',
1426+
'final': True,
1427+
'parallel': [{'name': '1', 'children': ['a', {"name": "b", "final": True}],
1428+
'initial': 'a',
1429+
'transitions': [['go', 'a', 'b']]},
1430+
{'name': '2', 'children': ['a', {"name": "b", "final": True}],
1431+
'initial': 'a',
1432+
'transitions': [['go', 'a', 'b']]}]}]
1433+
transitions = [['reset', 'C', 'A'], ["init", "A", "B"], ["do", "B", "C"]]
1434+
1435+
1436+
m = HierarchicalGraphMachine(states=states, transitions=transitions, initial="A", show_conditions=True,
1437+
title="Mermaid", graph_engine="mermaid", auto_transitions=False)
1438+
m.init()
1439+
1440+
pyperclip.copy(m.get_graph().draw(None)) # using pyperclip for convenience
1441+
print("Graph copied to clipboard!")
1442+
```
1443+
1444+
Produces this diagram (check the document source to see the markdown notation):
1445+
1446+
```mermaid
1447+
---
1448+
Mermaid Graph
1449+
---
1450+
stateDiagram-v2
1451+
direction LR
1452+
classDef s_default fill:white,color:black
1453+
classDef s_inactive fill:white,color:black
1454+
classDef s_parallel color:black,fill:white
1455+
classDef s_active color:red,fill:darksalmon
1456+
classDef s_previous color:blue,fill:azure
1457+
1458+
state "A" as A
1459+
Class A s_previous
1460+
state "B" as B
1461+
Class B s_active
1462+
state "C" as C
1463+
C --> [*]
1464+
Class C s_default
1465+
state C {
1466+
state "1" as C_1
1467+
state C_1 {
1468+
[*] --> C_1_a
1469+
state "a" as C_1_a
1470+
state "b" as C_1_b
1471+
C_1_b --> [*]
1472+
}
1473+
--
1474+
state "2" as C_2
1475+
state C_2 {
1476+
[*] --> C_2_a
1477+
state "a" as C_2_a
1478+
state "b" as C_2_b
1479+
C_2_b --> [*]
1480+
}
1481+
}
1482+
1483+
C --> A: reset
1484+
A --> B: init
1485+
B --> C: do
1486+
C_1_a --> C_1_b: go
1487+
C_2_a --> C_2_b: go
1488+
[*] --> A
1489+
```
1490+
1491+
To use more sophisticated graphing functionality, you'll need to have `graphviz` and/or `pygraphviz` installed.
14191492
To generate graphs with the package `graphviz`, you need to install [Graphviz](https://graphviz.org/) manually or via a package manager.
14201493

14211494
sudo apt-get install graphviz graphviz-dev # Ubuntu and Debian
@@ -1424,12 +1497,14 @@ To generate graphs with the package `graphviz`, you need to install [Graphviz](h
14241497

14251498
Now you can install the actual Python packages
14261499

1427-
pip install graphviz pygraphviz # install graphviz and/or pygraphviz manually...
1500+
pip install graphviz pygraphviz # install graphviz and/or pygraphviz manually...
14281501
pip install transitions[diagrams] # ... or install transitions with 'diagrams' extras which currently depends on pygraphviz
14291502

14301503
Currently, `GraphMachine` will use `pygraphviz` when available and fall back to `graphviz` when `pygraphviz` cannot be
1431-
found. This can be overridden by passing `use_pygraphviz=False` to the constructor. Note that this default might change
1432-
in the future and `pygraphviz` support may be dropped.
1504+
found.
1505+
If `graphviz` is not available either, `mermaid` will be used.
1506+
This can be overridden by passing `graph_engine="graphviz"` (or `"mermaid"`) to the constructor.
1507+
Note that this default might change in the future and `pygraphviz` support may be dropped.
14331508
With `Model.get_graph()` you can get the current graph or the region of interest (roi) and draw it like this:
14341509

14351510
```python
@@ -1440,7 +1515,7 @@ m = Model()
14401515
# without further arguments pygraphviz will be used
14411516
machine = GraphMachine(model=m, ...)
14421517
# when you want to use graphviz explicitly
1443-
machine = GraphMachine(model=m, use_pygraphviz=False, ...)
1518+
machine = GraphMachine(model=m, graph_engine="graphviz", ...)
14441519
# in cases where auto transitions should be visible
14451520
machine = GraphMachine(model=m, show_auto_transitions=True, ...)
14461521

0 commit comments

Comments
 (0)