yWorks / yfiles-jupyter-graphs

The home of the Jupyter notebook graph visualization widget powered by yFiles for HTML
https://www.yworks.com/products/yfiles-graphs-for-jupyter
Other
173 stars 18 forks source link

Show how to load Neo4j Graph Data into the plugin #2

Closed yGuy closed 2 years ago

yGuy commented 2 years ago

It should be easy to provide support for neo4j graphs. This should be documented or built right into the extension.

Loading neo4j graph data results is pretty trivial already, actually:

%pip install neo4j

from neo4j import GraphDatabase
uri      = "bolt://uritodatabase" 
user     = "username"          # your user name 
                              # default is always "neo4j" 
                              # unless you have changed it. 
password = "putyoursecretpassword-here"

driver = GraphDatabase.driver(uri=uri,auth=(user,password))
session = driver.session()

result = session.run("MATCH (s)-[r]->(t) RETURN s,r,t LIMIT 20")
g = result.graph()

from yfiles_jupyter_graphs import GraphWidget
w = GraphWidget()

w.nodes = list(map(lambda neoNode: ({"id": neoNode.id, "properties":{**dict(neoNode.items()), "label":list(neoNode.labels)[0]}}), g.nodes))
w.edges = list(map(lambda relationship: ({"id": relationship.id, "start": relationship.start_node.id, "end": relationship.end_node.id, "properties": {**dict(relationship.items()), "label": relationship.type}}), g.relationships))

w.show()