Skip to content

Commit 3b35995

Browse files
committed
docs: replace inline diagram generation with statemachine-diagram directive
Convert tutorial.md and transitions.md to use the directive instead of doctest write_png + image references. Extract CoffeeOrder, OrderWorkflow, and OrderWorkflowCompound to tests/machines/ for importability. Add tip about Graphviz requirement and seealso linking to diagram.md for deeper coverage (Sphinx directive, Jupyter, QuickChart, etc.). Delete the 3 PNG files that are no longer referenced.
1 parent 96ccb6d commit 3b35995

7 files changed

Lines changed: 66 additions & 35 deletions

File tree

-36 KB
Binary file not shown.
-30.2 KB
Binary file not shown.
-13.9 KB
Binary file not shown.

docs/transitions.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,14 @@ True
171171
Compare the diagrams — both model the same behavior, but the compound version
172172
makes the "cancellable" grouping explicit in the hierarchy:
173173

174-
```py
175-
>>> getfixture("requires_dot_installed")
176-
>>> OrderWorkflow()._graph().write_png("docs/images/transition_from_any.png")
177-
>>> OrderWorkflowCompound()._graph().write_png("docs/images/transition_compound_cancel.png")
178-
174+
```{statemachine-diagram} tests.machines.transition_from_any.OrderWorkflow
175+
:caption: from_.any()
179176
```
180177

181-
| `from_.any()` | Compound |
182-
|---|---|
183-
| ![from_.any()](images/transition_from_any.png) | ![Compound](images/transition_compound_cancel.png) |
178+
```{statemachine-diagram} tests.machines.transition_from_any.OrderWorkflowCompound
179+
:caption: Compound
180+
:target:
181+
```
184182

185183
The compound approach scales better as you add more states — no need to
186184
remember to include each new state in a `from_()` list.

docs/tutorial.md

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -345,46 +345,36 @@ factories, and the full list of listener callbacks.
345345

346346
## Generating diagrams
347347

348-
Visualize any state machine as a diagram. Install the `diagrams` extra
349-
first:
348+
Visualize any state machine as a diagram:
350349

350+
```{statemachine-diagram} tests.machines.tutorial_coffee_order.CoffeeOrder
351+
:alt: CoffeeOrder diagram
351352
```
352-
pip install python-statemachine[diagrams]
353-
```
354-
355-
Then generate an image at runtime:
356-
357-
```py
358-
>>> getfixture("requires_dot_installed")
359-
360-
>>> from statemachine import StateChart, State
361-
362-
>>> class CoffeeOrder(StateChart):
363-
... pending = State(initial=True)
364-
... preparing = State()
365-
... ready = State()
366-
... picked_up = State(final=True)
367-
...
368-
... start = pending.to(preparing)
369-
... finish = preparing.to(ready)
370-
... pick_up = ready.to(picked_up)
371353

372-
>>> order = CoffeeOrder()
373-
>>> order._graph().write_png("docs/images/tutorial_coffeeorder.png")
354+
Generate diagrams programmatically with `_graph()`:
374355

356+
```python
357+
order = CoffeeOrder()
358+
order._graph().write_png("order.png")
375359
```
376360

377-
![CoffeeOrder](images/tutorial_coffeeorder.png)
378-
379361
Or from the command line:
380362

381363
```bash
382364
python -m statemachine.contrib.diagram my_module.CoffeeOrder order.png
383365
```
384366

367+
```{tip}
368+
Diagram generation requires [Graphviz](https://graphviz.org/) (`dot` command)
369+
and the `diagrams` extra:
370+
371+
pip install python-statemachine[diagrams]
372+
```
373+
385374
```{seealso}
386-
See [](diagram.md) for Jupyter integration, SVG output, DPI settings, and
387-
the `quickchart_write_svg` alternative that doesn't require Graphviz.
375+
See [](diagram.md) for highlighting active states, Jupyter integration,
376+
SVG output, DPI settings, Sphinx directive, and the `quickchart_write_svg`
377+
alternative that doesn't require Graphviz.
388378
```
389379

390380

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from statemachine import State
2+
from statemachine import StateChart
3+
4+
5+
class OrderWorkflow(StateChart):
6+
pending = State(initial=True)
7+
processing = State()
8+
done = State()
9+
completed = State(final=True)
10+
cancelled = State(final=True)
11+
12+
process = pending.to(processing)
13+
complete = processing.to(done)
14+
finish = done.to(completed)
15+
cancel = cancelled.from_.any()
16+
17+
18+
class OrderWorkflowCompound(StateChart):
19+
class active(State.Compound):
20+
pending = State(initial=True)
21+
processing = State()
22+
done = State(final=True)
23+
24+
process = pending.to(processing)
25+
complete = processing.to(done)
26+
27+
completed = State(final=True)
28+
cancelled = State(final=True)
29+
done_state_active = active.to(completed)
30+
cancel = active.to(cancelled)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from statemachine import State
2+
from statemachine import StateChart
3+
4+
5+
class CoffeeOrder(StateChart):
6+
pending = State(initial=True)
7+
preparing = State()
8+
ready = State()
9+
picked_up = State(final=True)
10+
11+
start = pending.to(preparing)
12+
finish = preparing.to(ready)
13+
pick_up = ready.to(picked_up)

0 commit comments

Comments
 (0)