Skip to content

Instantly share code, notes, and snippets.

@madagra
Last active June 7, 2020 10:15
Show Gist options
  • Select an option

  • Save madagra/dca8b28d2130b9946bcf4c19a765583e to your computer and use it in GitHub Desktop.

Select an option

Save madagra/dca8b28d2130b9946bcf4c19a765583e to your computer and use it in GitHub Desktop.
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