[Python] How to add a label to each node in Graphviz?

Discussão em 'Python' iniciado por Stack, Agosto 26, 2025.

  1. Stack

    Stack Membro Participativo

    [EDIT] as asked here is what ps does for me :

    PID TTY TIME CMD
    3796 pts/0 00:00:00 bash
    4811 pts/0 00:00:00 ps


    I'm beginning with Graphviz and I would like to display the name of the running process. I got a script that display their number and I'm trying to add a label to each node.

    The problem is that only the last write label is displayed to the root node, how can I manage to write a label to every node ?

    #!/usr/bin/env python3

    from subprocess import Popen, PIPE, call
    import re

    dot = open("psgraph.dot", "w")
    dot.write("digraph G {\n")

    p = Popen("ps -fe", shell=True, stdout=PIPE)
    psre = re.compile(r"\w+\s+(\d+)\s+(\d+)")

    p.stdout.readline() # ignore first line
    for line in p.stdout:
    match = psre.search(line.decode("utf-8"))
    if match:
    if int(match.group(2)) in (0, 2):
    continue
    dot.write (" {1} -> {0}\n".format(match.group(1), match.group(2)))

    for line in p.stdout:
    match = psre.search(line.decode("utf-8"))
    if match:
    if int(match.group(2)) in (0, 2):
    continue
    dot.write ("""1 [label="loop"]\n""")

    dot.write("""1 [label="laste write"]}\n""")
    dot.close()

    call("dot -Tpdf -O psgraph.dot", shell=True)

    Continue reading...

Compartilhe esta Página