public
Created

HydraGraph script for exposing GraphDatabase in Neo4j using Python and Flask by Coolgarif Tech

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
from neo4jrestclient.client import GraphDatabase
from neo4jrestclient.constants import RAW
from neo4jrestclient.client import Node
 
from urlparse import urlparse
import re, json
 
from flask import Flask, Response, json, jsonify, request, Blueprint, render_template
 
app = Flask(__name__)
 
@app.route('/_api/hydraGraph')
def api_response():
 
callbackArguments = request.args['callback']
 
# create DB connection
db = GraphDatabase("http://localhost:7475/db/data")
 
nodes = getNodes(db)
rels = getRels(db)
 
result = {
'nodes' : nodes,
'rels' : rels
}
 
result = json.dumps(result)
 
# GET Request Response
callbackWrapper = callbackArguments + "(" + result + ")"
resp = Response(callbackWrapper, status = 200, mimetype = 'application/json')
return resp
 
 
def createNodeJSON(name, uid, description):
JSONObject = {
'name': name,
'id' : uid,
'description' : description
}
return JSONObject
 
 
def createRelsJSON(startNode, endNode):
JSONObject = {
'startNode' : startNode,
'endNode' : endNode
}
return JSONObject
 
 
def doRegEX(urlString):
regex = re.compile("([^/]*)$")
stripedURLComponent = regex.search(urlString.path)
return stripedURLComponent.group(0)
 
 
def getNodes(db):
q = "START n=node(*) RETURN n"
params = {}
querySquenceObject = db.query(q, params=params, returns=RAW)
#Blank list to hold the JSON
nodeJSON = []
 
# Iterating over the resposes from the graph db
# NOTE:Excluding the ROOT NODE from RETURN!!!!
for node in querySquenceObject[1:]:
n = node.pop()
data = n.get('data')
name = data.get('name')
description = data.get('description')
 
self = n.get('self')
print self
self = urlparse(self)
uid = doRegEX(self)
 
nodeJSON.append(createNodeJSON(name, uid, description))
return nodeJSON
 
def getRels(db):
q = "START n=node(*) MATCH (n)-[r]->() RETURN r"
params = {}
querySquenceObject = db.query(q, params=params, returns=RAW)
 
#Blank list to hold the JSON
relsJSON = []
for rel in querySquenceObject:
r = rel.pop()
start = r.get('start')
end = r.get('end')
start = urlparse(start)
end = urlparse(end)
startNode = doRegEX(start)
endNode = doRegEX(end)
 
relsJSON.append(createRelsJSON(startNode, endNode))
return relsJSON
 
 
if __name__ == '__main__':
app.debug = True
app.run()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.