neo4j-contrib / py2neo

EOL! Py2neo is a comprehensive Neo4j driver library and toolkit for Python.
https://py2neo.org
Apache License 2.0
20 stars 8 forks source link

networkx export of the graph #893

Open geoHeil opened 3 years ago

geoHeil commented 3 years ago

how can I export the graph to networkx which is obtained by running some cypher query?

technige commented 3 years ago

Right now, there's no direct way to do this. You would have to graph.run(...).to_subgraph() and then manually iterate through the nodes and relationships to convert them into NetworkX format.

Out of interest, how would you expect to map the node labels and relationship types onto a NetworkX graph?

geoHeil commented 3 years ago

Well a simple solution such as https://neo4j.com/labs/apoc/4.1/export/graphml/ what is happening for t.graphml.all would do the job already i.e. having a Networkx MultiDiGraph with an edge attribute relationship_kin / node_kind to store the label /type and then all the attributer as key/value lookups.

priamai commented 3 years ago

This would be wonderful!

motey commented 3 years ago

hi @geoHeil , i am not familiar with NetworkX but if you just need a graphml export what about a tiny CALL apoc.export.graphml.all(null, {stream:true}) ?

from py2neo import Graph

g = Graph(name="test")

# Create testdata on db1
g.run("CREATE (n:MyNode) SET n.id = 1")
g.run("CREATE (n:MyNode2) SET n.id = 2")

# Extract data from db1
graphml = g.run(
    "CALL apoc.export.graphml.all(null, {stream:true}) yield file, nodes, relationships, properties, data RETURN file, nodes, relationships, properties, data"
).to_data_frame()
print(graphml)