Comments (3)
Hello @ulemanstreaming,
the diagrams module supports pygraphviz
and graphviz
backends and get_graph
will return either pygraphviz
or graphviz
graph objects which you can edit to your liking. If you use pygraphviz
(default) you could edit pygraphviz.AGraph
attributes like this:
from transitions.extensions import GraphMachine
states = ["welcome", "industry", "usecase", "load_data"]
transitions = [
["next_step", "welcome", "industry"],
["next_step", "industry", "usecase"],
["next_step", "usecase", "load_data"],
["previous_step", "load_data", "usecase"],
["previous_step", "usecase", "industry"],
["previous_step", "industry", "welcome"],
]
m = GraphMachine(states=states, transitions=transitions, initial='welcome')
graph = m.get_combined_graph()
graph.graph_attr["fontname"] = "arial"
graph.edge_attr["fontname"] = "arial"
graph.node_attr["fontname"] = "arial"
graph.draw('graph_arial.png', prog='dot')
But there is also a mechanism for 'global' graph settings:
The dot/graphviz settings are defined as class members (Graphmachine.machine_attributes
, Graphmachine.style_attributes
). For global changes you can either 'monkey patch' those or use inheritance and create your own configuration. For a start, you can copy the above mentioned configurations.
GraphMachine
uses MarkupMachine
to get a dictionary representation of the current machine configuration (states, transitions, etc.). You could override get_markup_config
to intercept this process and add a capitalised label.
from transitions.extensions import GraphMachine
from copy import deepcopy
states = ["welcome", "industry", "usecase", "load_data"]
transitions = [
["next_step", "welcome", "industry"],
["next_step", "industry", "usecase"],
["next_step", "usecase", "load_data"],
["previous_step", "load_data", "usecase"],
["previous_step", "usecase", "industry"],
["previous_step", "industry", "welcome"],
]
machine_attributes = deepcopy(GraphMachine.machine_attributes)
style_attributes = deepcopy(GraphMachine.style_attributes)
machine_attributes["fontname"] = "arial"
style_attributes["node"]["default"]["fontname"] = "arial"
style_attributes["edge"]["default"]["fontname"] = "arial"
class ArialGraph(GraphMachine):
machine_attributes=machine_attributes
style_attributes=style_attributes
def get_markup_config(self):
config = super(ArialGraph, self).get_markup_config()
for state in config['states']:
state['label'] = state['name'].capitalize()
return config
ag = ArialGraph(states=states, transitions=transitions, initial='welcome')
ag.get_combined_graph().draw('graph_inherited.png', prog='dot')
So this is how you could achieve what you are looking for as of now. However, if you have some ideas about how to streamline this process and make it more user friendly let me know. Adding more parameters to the constructor is something I'd like to avoid since there are already so many of them.
from transitions.
Instead of
graph.graph_attr["fontname"] = "arial"
graph.edge_attr["fontname"] = "arial"
graph.node_attr["fontname"] = "arial"
you could also pass them as arguments to dot:
graph.draw('graph_args.png', prog='dot', args="-Gfontname=Arial -Efontname=Arial -Nfontname=Arial")
from transitions.
Thank you @aleneum . These are very helpful suggestions, and sufficient for my current purposes.
- Using command line arguments is effective, though it feels a bit hacky.
- I already subclass GraphMachine (using your suggested Alternative initialization pattern), so there's no need for me to create a separate subclass. I work in a Jupyter notebook, which can readily display a PNG if it's returned by a method named
_repr_png_
. So I simply implement that, along with your suggestions forget_markup_config()
and the*_attribute
data members. I also use a smaller font inside the graph and replace underscore with space in the state labels. This leads to:
class Wizard(GraphMachine):
machine_attributes = deepcopy(GraphMachine.machine_attributes)
style_attributes = deepcopy(GraphMachine.style_attributes)
machine_attributes['fontname'] = 'arial'
style_attributes['node']['default']['fontname'] = 'arial'
style_attributes['edge']['default']['fontname'] = 'arial'
style_attributes['node']['default']['fontsize'] = 9
style_attributes['edge']['default']['fontsize'] = 9
def __init(...)
...
def get_markup_config(self):
config = super(Wizard, self).get_markup_config()
for state in config['states']:
state['label'] = state['name'].capitalize().replace('_', ' ')
return config
def _repr_png_(self, **kwargs):
return self.get_graph(title=self._title, **kwargs).draw(None, prog='dot', format='png')
So, it works. I have no suggestions for making it easier beyond documenting these tricks, or maybe doing more with the kwargs
parameter in get_graph()
. I agree that loading up the constructor with more parameters is not great; if you really wanted to give callers more control, adding one or a few methods to set style attributes might be preferable.
This issue can be closed as far as I'm concerned.
from transitions.
Related Issues (20)
- Project dependencies may have API risk issues HOT 1
- Dynamically adding HSM to an existing state, results in child transitions to be absent.
- Mermaid diagrams HOT 4
- maximum recursion depth exceeded while calling a Python object HOT 1
- State transition during on_exception callback causes RecursionError
- "on_exception" is not invoked for "KeyboardInterrupt"
- Explicitly reexport package names
- AttributeError: 'state' does not exist HOT 3
- prepare func raises Exception altough on_exception is defined
- GraphMachine seems not working for parallel states HOT 1
- AsyncMachine transitions fail when a list of `State` objects are passed in to the FSM. HOT 2
- get_triggers does not work HOT 3
- Cannot use Pydantic `BaseModel` class for models HOT 2
- All possible transitions are initialized, instead of those provided as an argument. HOT 2
- Machine get into wrong state. HOT 1
- after_state_change not triggered by set_state HOT 2
- AsyncMachine with queued transitions breaks when transition is cancelled
- AsyncMachine transitions aren't atomic HOT 4
- The Event and Machine have cyclic dependency. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from transitions.