Last active
June 7, 2020 10:15
-
-
Save madagra/dca8b28d2130b9946bcf4c19a765583e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from anytree.exporter import DotExporter | |
| COLOR_SCHEME = ["aliceblue", "antiquewhite", "azure", "coral", "palegreen"] | |
| def export(self): | |
| """ | |
| Export tree into graphviz format in both *.dot file and image *.png file | |
| """ | |
| def nodeattr_fn(node): | |
| return f'style=filled color={COLOR_SCHEME[node.depth]}' | |
| def nodename_fn(node): | |
| name = node.name | |
| return name | |
| root = self.nodes[self.root_key] | |
| dot = DotExporter( | |
| root, | |
| nodenamefunc=nodename_fn, | |
| nodeattrfunc=nodeattr_fn, | |
| options=[ | |
| 'graph [layout = dot, ranksep="1.5", nodesep="0.7"]', | |
| 'rankdir ="LR"', | |
| 'node [fontname="Arial"]' | |
| ] | |
| ) | |
| dot.to_dotfile(f"./{self.root_key}.dot") | |
| dot.to_picture(f"./{self.root_key}.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment