22(diagrams)=
33# Diagrams
44
5- You can generate diagrams from your {ref}` StateChart ` .
5+ You can generate visual diagrams from any
6+ {class}` ~statemachine.statemachine.StateChart ` — useful for documentation,
7+ debugging, or sharing your machine's structure with teammates.
68
7- ``` {note}
8- This functionality depends on [pydot](https://github.com/pydot/pydot), it means that you need to
9- have `pydot` installed on your system. pydot is a Python library that allows you to create and
10- manipulate graphs in [Graphviz](https://graphviz.org/)'s
11- [dot language](https://graphviz.org/doc/info/lang.html).
12-
13- In order to use [pydot](https://github.com/pydot/pydot), we also need to have
14- [Graphviz](https://graphviz.org/) installed on your system.
15-
16- You can install this library already with pydot dependency using the `extras` install option:
9+ ## Installation
1710
18- pip install python-statemachine[diagrams]
11+ Diagram generation requires [ pydot] ( https://github.com/pydot/pydot ) and
12+ [ Graphviz] ( https://graphviz.org/ ) :
1913
14+ ``` bash
15+ pip install python-statemachine[diagrams] # installs pydot
16+ ```
2017
21- Or to install `pydot` manually, you can use pip by running the following command:
22-
23- pip install pydot
24-
18+ You also need the ` dot ` command-line tool from Graphviz. On Debian/Ubuntu:
2519
26- To install Graphviz, you can visit the [Graphviz website](https://graphviz.org/) and follow the
27- instructions for your operating system. Alternatively, you can use a package manager to install
28- Graphviz. For example, on Debian-based systems (such as Ubuntu), you can use the following command:
20+ ``` bash
21+ sudo apt install graphviz
22+ ```
2923
30- sudo apt install graphviz
24+ For other systems, see the [ Graphviz downloads page ] ( https:// graphviz.org/download/ ) .
3125
32- ```
3326
34- ## How to generate a diagram at runtime
27+ ## Generating diagrams
3528
29+ Use ` DotGraphMachine ` to create a diagram from a class or an instance:
3630
3731``` py
3832>> > from statemachine.contrib.diagram import DotGraphMachine
@@ -48,20 +42,16 @@ Graphviz. For example, on Debian-based systems (such as Ubuntu), you can use the
4842
4943```
5044
51- With a ` dot ` graph instance, you can also generate images :
45+ Export to an image file :
5246
5347``` py
5448>> > dot.write_png(" docs/images/order_control_machine_initial.png" )
5549
5650```
5751
58- As this one:
59-
60-
6152![ OrderControl] ( images/order_control_machine_initial.png )
6253
63-
64- If you find the resolution of the image lacking, you can
54+ For higher resolution, set the DPI before exporting:
6555
6656``` py
6757>> > dot.set_dpi(300 )
@@ -70,10 +60,12 @@ If you find the resolution of the image lacking, you can
7060
7161```
7262
73- ![ OrderControl] ( images/order_control_machine_initial_300dpi.png )
63+ ![ OrderControl (300 DPI) ] ( images/order_control_machine_initial_300dpi.png )
7464
65+ ### Highlighting the current state
7566
76- The current {ref}` state ` is also highlighted:
67+ When you pass a machine ** instance** (not a class), the diagram highlights
68+ the current state:
7769
7870``` py
7971>> > # This example will only run on automated tests if dot is present
@@ -96,58 +88,52 @@ The current {ref}`state` is also highlighted:
9688
9789![ OrderControl] ( images/order_control_machine_processing.png )
9890
99-
100- ``` {hint}
101-
102- A handy shortcut to have the graph representation:
91+ ``` {tip}
92+ Every state machine instance exposes a `_graph()` shortcut that returns
93+ the `pydot.Dot` object directly.
94+ ```
10395
10496``` py
105- >>> machine._graph()
97+ >> > machine._graph() # doctest: +ELLIPSIS
10698< pydot.core.Dot ...
10799
108100```
109101
110- ## Generate from the command line
111102
112- You can also generate a diagram from the command line using the ` statemachine.contrib.diagram ` as a module.
103+ ## Command line
113104
114- ``` bash
115- ❯ python -m statemachine.contrib.diagram --help
116- usage: diagram.py [OPTION] < classpath> < out>
117-
118- Generate diagrams for StateMachine classes.
119-
120- positional arguments:
121- classpath A fully-qualified dotted path to the StateChart class.
122- out File to generate the image using extension as the output format.
105+ You can generate diagrams without writing Python code:
123106
124- optional arguments:
125- -h, --help show this help message and exit
107+ ``` bash
108+ python -m statemachine.contrib.diagram < classpath > < output_file >
126109```
127110
128- Example :
111+ The output format is inferred from the file extension :
129112
130113``` bash
131- python -m statemachine.contrib.diagram tests.examples.traffic_light_machine.TrafficLightMachine m .png
114+ python -m statemachine.contrib.diagram tests.examples.traffic_light_machine.TrafficLightMachine diagram .png
132115```
133116
134117``` {note}
135- Supported formats include: `dia`, `dot`, `fig`, `gif`, `jpg`, `pdf`, `png`, `ps`, `svg` and many others.
136- Please see [pydot](https://github.com/pydot/pydot) and [Graphviz](https://graphviz.org/) for a
118+ Supported formats include `dia`, `dot`, `fig`, `gif`, `jpg`, `pdf`,
119+ `png`, `ps`, `svg`, and many others. See
120+ [Graphviz output formats](https://graphviz.org/docs/outputs/) for the
137121complete list.
138122```
139123
140124
141- ## JupyterLab / Jupyter integration
142-
143- Machines instances are automatically displayed as a diagram when used on JupyterLab cells:
125+ ## Jupyter integration
144126
127+ State machine instances are automatically rendered as diagrams in
128+ JupyterLab cells — no extra code needed:
145129
146130![ Approval machine on JupyterLab] ( images/lab_approval_machine_accepted.png )
147131
148132
149- ## Don't want to install Graphviz
133+ ## Online generation (QuickChart)
150134
135+ If you prefer not to install Graphviz locally, you can generate diagrams
136+ using the [ QuickChart] ( https://quickchart.io/ ) online service:
151137
152138``` {eval-rst}
153139.. autofunction:: statemachine.contrib.diagram.quickchart_write_svg
0 commit comments