Search code examples
pythonnetworkxgremlinjanusgraphgremlinpython

Is there a way to cast a gremlinpython Graph to a networkx Graph


I want to cast a gremlinpython Graph (gremlin_python.structure.graph.Graph) into a networkx graph.

I found that with package ipython-gremlin (https://github.com/davebshow/ipython-gremlin) one can convert vertices to pandas dataframes and edges to networkx.classes.multidigraph.MultiDiGraph but the package is outdated and I can't find the code location on which they do the conversion. Is there a way to do the conversion?

Thanks!


Solution

  • You will probably have to write a little code to do this. I have done it before using the subgraph step to return the part of the graph I am interested in and then iterating over that creating the NetworkX vertices and edges. It's fairly straightforward. The only issue to be aware of is that the current Gremlin Python client uses the default Tornado maximum result frame size which I believe is 10mb so if your subgraph result exceeds that you will hit an exception. Here is a simple example that finds the star graph of routes from the SAF airport in the air routes data set and creates a NetworkX DiGraph from it

    import networkx as nx
    G = nx.DiGraph()
    sg = g.V().has('code','SAF').outE().subgraph('sg').cap('sg').next()
    for e in sg['@value']['edges']:
        G.add_edge(e.outV.id,e.inV.id,elabel=e.label)
    
    print(G.nodes())
    print(G.edges())
    

    When run the results are

    ['44', '13', '20', '31', '8']
    [('44', '13'), ('44', '20'), ('44', '31'), ('44', '8')]