Python-igraph: Creating a graph object with a list of nodes

Hi all!

I am new to Python and even newer to iGraph.

I have a project which I have been stuck on for a while (please bear with me…)

I wish to generate a tree structure to visualize the structure of an XML file with Plotly-igraph.

I am using this as reference:
reference link

Trouble is, I am unsure how to do so.

My current implementation entails the following steps:

  1. Getting all the elements of the XML file and putting it in to a list.
    (elements include 3 strings: tag, attribute, text)

  2. Using the list to generate the tree in igraph.

However, I am unsure how to do the 2nd part…
igraph does not seem to accept objects as arguments when I try to do a:

G = Graph()
for x in elementlist:
G.add_vertices(x)

giving the following error:

an integer is required (got type treenode)

As such, my 2 main questions are:
1. How do I use a list to generate a graph object?
2. I also have no idea how to get the edges between each node in an XML.

Any help with the first question will be greatly appreciated and it would be nice if anyone has suggestions for the 2nd question! (I’m open to changing my current implementation)

Hi graphie,

So add_vertices takes an integer number because it wants to add that number of vertices to the graph. What you might want is the method called add_vertex. This accepts another parameter name which can then be used to find the vertex and add data to it.

G = Graph()
for x in elementlist:
    G.add_vertex(name="some_name")
    G.vs.find(name="some_name")["x"] = x

See https://igraph.org/python/doc/igraph.Graph-class.html#add_vertex for more information.
As for your second question, maybe someone more knowledgeable in XML parsing could point you in the right direction?