convert the python dictionary output into a flowchart convert the python dictionary output into a flowchart tkinter tkinter

convert the python dictionary output into a flowchart


Focusing just on converting your weirdly-formatted status string into a dict is hard enough couldn't you have it in a more sensible, popular format like JSON?

import redef Status2dict(status):    result = {}    current = {}    lines = status.splitlines()    for line in lines:        line = line.strip()        if not line:             continue        mo = re.match(r'Object (\w+) {', line)        if mo:            curk = mo.group(1)            current = {curk: {}}        elif re.match('}', line):            result.update(current)            current = {}        else:            mo = re.match(r'(\w+)\s+([\w\s]+);', line)            if not mo:                raise ValueError('cannot match {!r}'.format(line))            current[curk][mo.group(1)] = mo.group(2)    if current:        result.update(current)    return resultimport pprint    pprint.pprint(Status2dict(status))

This code tries to be marginally robust on small variations from the inferred syntax, you may want to dial that up or down, depending. But, it should be better than nothing.


If you already got the code converting to dot file, you can use the tk export of graphviz to visualise your graph on a tkinter canvas. You could give a look at this other question for other libraries.

dot -Ttk output tcl code like this

# a$c create oval 5.33 53.33 77.33 5.33 -fill white -width 1 -outline black -tags {1node1}$c create text 41.33 30.3 -text {a} -fill black -font {"Times" 14} -tags {0node1}# c$c create oval 53.33 149.33 125.33 101.33 -fill white -width 1 -outline black -tags {1node2}$c create text 89.33 126.3 -text {c} -fill black -font {"Times" 14} -tags {0node2}#(...)

You can display it on a tkinter canvas by calling the tcl interpreter as in this example:

from Tkinter import *import subprocessgraph = "digraph g { a-> c ; b -> c ; c -> d }"ps = subprocess.Popen(('dot', '-Ttk'),stdin=subprocess.PIPE, stdout=subprocess.PIPE)out, err = ps.communicate(input=graph)canvas = Canvas()canvas.pack(expand=YES, fill=BOTH)#the output of graphiz assume thazt the canvas in variable $cout = "set c .{}\n".format(canvas._name) + out#tk.eval expect command one by onemap(canvas.tk.eval, out.split("\n"))canvas.mainloop()