Skip to content

Commit

Permalink
added draw.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuf Baktir committed Aug 8, 2020
1 parent 9fbf1f2 commit 74eb533
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 4 deletions.
3 changes: 2 additions & 1 deletion nxneo4j/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 2,5 @@
from nxneo4j.community import *
from nxneo4j.path_finding import *
from nxneo4j.graph import Graph
from nxneo4j.di_graph import DiGraph
from nxneo4j.di_graph import DiGraph
from nxneo4j.draw import *
108 changes: 108 additions & 0 deletions nxneo4j/draw.py
Original file line number Diff line number Diff line change
@@ -0,0 1,108 @@
from IPython.display import IFrame

def draw(G, limit=100):
query = f"""
MATCH (n)
WITH n, rand() AS random
ORDER BY random
LIMIT {limit}
OPTIONAL MATCH (n)-[r]->(m)
RETURN n.{G.identifier_property} AS source_node,
id(n) AS source_id,
type(r) as label,
m.{G.identifier_property} AS target_node,
id(m) AS target_id
"""
result = G.driver.session().run(query)
nodes = []
edges = []
for row in result:
node1 = {'id':row['source_id'],'label':str(row['source_node'])}
node2 = {'id':row['target_id'],'label':str(row['target_node'])}
edge = {'from':row['source_id'],'to':row['target_id'],'label':row['label']}
if (node1 not in nodes) & (node2['id'] != None):
nodes.append(node1)
if (node2 not in nodes) & (node2['id'] != None):
nodes.append(node2)
if (edge not in edges) & (edge['to'] != None):
edges.append(edge)
html = f"""
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>neo4j display</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis.css" type="text/css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis-network.min.js"> </script>
<style type="text/css">
#mynetwork {{
width: 100%;
height: 500px;
}}
</style>
</head>
<body data-gr-c-s-loaded="true">
<div id="mynetwork"><div class="vis-network" tabindex="900" style="position: relative; overflow: hidden; touch-action: pan-y; user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); width: 100%; height: 100%;"><canvas width="1200" height="800" style="position: relative; touch-action: none; user-select: none; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); width: 100%; height: 100%;"></canvas></div></div>
<script type="text/javascript">
// create an array with nodes
var nodes = new vis.DataSet(
{nodes}
);
// create an array with edges
var edges = new vis.DataSet(
{edges}
);
// create a network
var container = document.getElementById('mynetwork');
var data = {{
nodes: nodes,
edges: edges
}};
var options = {{
nodes: {{
font: {{ color: 'white',size: 14 }},
color: '#F9A6C1',
size: 25,
shape: 'circle',
widthConstraint: 60
}},
edges : {{
arrows: {{
to: {{enabled: true, scaleFactor: 0.5}}
}},
"color": {{
"inherit": false
}},
font: {{size: 14, align: 'middle'}},
"smooth": {{
"enabled": true,
"type": "dynamic",
}},
"length": 200
}},
"interaction": {{
"dragNodes": true,
"hideEdgesOnDrag": false,
"hideNodesOnDrag": false
}},
"physics": {{
"enabled": true,
"stabilization": {{
"enabled": true,
"fit": true,
}}
}}
}};
var network = new vis.Network(container, data, options);
</script>
</body></html>
"""
file = open("vis.html", "w")
file.write(html)
file.close()
return IFrame("vis.html", width="100%", height="500")
8 changes: 5 additions & 3 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,12 1,11 @@
from neo4j import GraphDatabase

import nxneo4j as nx
import networkx as nx
nx.__file__

driver = GraphDatabase.driver(uri="bolt://localhost:11003",auth=("neo4j","neo"))
G = nx.Graph(driver)
G = nx.Graph()


G.delete_all()
G.add_node(1)
G.add_nodes_from([2,3,4])
Expand Down Expand Up @@ -36,3 35,6 @@

>>> nx.shortest_path(G, source=1, target=4)
[1, 2, 3, 4]

import pandas as pd
pd.__version__

0 comments on commit 74eb533

Please sign in to comment.