5

How to check if a particular node is already present in the graph.

Here is what I have tried.

>>>from graphviz import Digraph
>>>dot = Digraph()
>>>dot.node('solid',xlabel='0')
>>>dot.node('liquid',xlabel='0')
>>>dot.edge('solid','liquid','melt')
>>>print (dot)

digraph {
    solid [xlabel=0]
    liquid [xlabel=0]
    solid -> liquid [label=melt]
}

>>>check = 'solid' in dot
>>>print (solid)

False

As we see here I can't directly check for the node.

I am looking for a way to check if the node has been visited/created before and if that node is visited then increment it's xlabel by 1.

Is there a way to traverse and visit each node in graphviz or do I have to write a separate code to check if a specific node is present?

2 Answers 2

1

Apparently the .body attribute contains a list with nodes prefixed with tab. Should you could do:

>>>print('\tsolid' in dot.body)

True
Sign up to request clarification or add additional context in comments.

Comments

0

The method worked for me in 2020 is:

if '\tsolid' in dot.source:
    # Here you need to get the line number and know the real xlabel
    dot.body[line_number] = '\tsolid [xlabel='+str(real_xlabel)+']'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.