Skip to content

Commit 4edee56

Browse files
committed
docs: mention f-string/format() text representations in README and tutorial
1 parent 1e4ba19 commit 4edee56

2 files changed

Lines changed: 54 additions & 5 deletions

File tree

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,17 @@ True
7777

7878
```
7979

80-
Generate a diagram:
80+
Generate a diagram or get a text representation with f-strings:
81+
82+
```py
83+
>>> print(f"{sm:md}")
84+
| State | Event | Guard | Target |
85+
| ------ | ----- | ----- | ------ |
86+
| Green | cycle | | Yellow |
87+
| Yellow | cycle | | Red |
88+
| Red | cycle | | Green |
89+
90+
```
8191

8292
```python
8393
sm._graph().write_png("traffic_light.png")
@@ -341,7 +351,7 @@ There's a lot more to explore:
341351
- **`prepare_event`** callback — inject custom data into all callbacks
342352
- **Observer pattern** — register external listeners to watch events and state changes
343353
- **Django integration** — auto-discover state machines in Django apps with `MachineMixin`
344-
- **Diagram generation**from the CLI, at runtime, or in Jupyter notebooks
354+
- **Diagram generation**via f-strings (`f"{sm:mermaid}"`), CLI, Sphinx directive, or Jupyter
345355
- **Dictionary-based definitions** — create state machines from data structures
346356
- **Internationalization** — error messages in multiple languages
347357

docs/tutorial.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,16 +364,55 @@ Or from the command line:
364364
python -m statemachine.contrib.diagram my_module.CoffeeOrder order.png
365365
```
366366

367+
### Text representations with `format()`
368+
369+
You can also get text representations of any state machine using Python's built-in
370+
`format()` or f-strings — no Graphviz needed:
371+
372+
```py
373+
>>> from tests.machines.tutorial_coffee_order import CoffeeOrder
374+
375+
>>> print(f"{CoffeeOrder:md}")
376+
| State | Event | Guard | Target |
377+
| --------- | ------- | ----- | --------- |
378+
| Pending | start | | Preparing |
379+
| Preparing | finish | | Ready |
380+
| Ready | pick_up | | Picked up |
381+
382+
```
383+
384+
Supported formats include `mermaid`, `md` (markdown table), `rst`, `dot`, and `svg`.
385+
Works on both classes and instances:
386+
387+
```py
388+
>>> print(f"{CoffeeOrder:mermaid}")
389+
stateDiagram-v2
390+
direction LR
391+
state "Pending" as pending
392+
state "Preparing" as preparing
393+
state "Ready" as ready
394+
state "Picked up" as picked_up
395+
[*] --> pending
396+
picked_up --> [*]
397+
pending --> preparing : start
398+
preparing --> ready : finish
399+
ready --> picked_up : pick_up
400+
<BLANKLINE>
401+
402+
```
403+
367404
```{tip}
368-
Diagram generation requires [Graphviz](https://graphviz.org/) (`dot` command)
405+
Graphviz diagram generation requires [Graphviz](https://graphviz.org/) (`dot` command)
369406
and the `diagrams` extra:
370407
371408
pip install python-statemachine[diagrams]
409+
410+
Text formats (`md`, `rst`, `mermaid`) work without any extra dependencies.
372411
```
373412

374413
```{seealso}
375-
See [](diagram.md) for highlighting active states, Jupyter integration,
376-
SVG output, DPI settings, Sphinx directive, and the `quickchart_write_svg`
414+
See [](diagram.md) for all formats, highlighting active states, auto-expanding
415+
docstrings, Jupyter integration, Sphinx directive, and the `quickchart_write_svg`
377416
alternative that doesn't require Graphviz.
378417
```
379418

0 commit comments

Comments
 (0)