Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a block diagram from a python class hierarchy using blockdiag
"""
Dependencies: pip install blockdiag
Run: python visualize.py
"""
from blockdiag import parser, builder, drawer
TEMPLATE = """
blockdiag {{ orientation = portrait
{}
}}"""
def hierarchy(cls):
parents = set(cls.__bases__) - {object}
if parents:
edges = zip(parents, [cls]*len(parents))
for parent in parents:
return edges + hierarchy(parent)
return []
def generate_blockdiag_string(cls):
return TEMPLATE.format('\n'.join(
'{} -> {};'.format(s.__name__, t.__name__) for s, t in hierarchy(cls)))
def build_png(source):
tree = parser.parse_string(source)
diagram = builder.ScreenNodeBuilder.build(tree)
draw = drawer.DiagramDraw('PNG', diagram, filename="foo.png")
draw.draw()
draw.save()
if __name__ == '__main__':
class Closeable(object):
pass
class Clickable(object):
pass
class Widget(Closeable, Clickable):
pass
class Panel(Widget):
pass
build_png(generate_blockdiag_string(Panel))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment