diff d3s_examples/python-neo4jrestclient/sphaera.py @ 8:18ef6948d689

new d3s examples
author Dirk Wintergruen <dwinter@mpiwg-berlin.mpg.de>
date Thu, 01 Oct 2015 17:17:27 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/d3s_examples/python-neo4jrestclient/sphaera.py	Thu Oct 01 17:17:27 2015 +0200
@@ -0,0 +1,112 @@
+#!/usr/bin/env python
+from json import dumps
+
+from flask import Flask, Response, request, send_from_directory
+
+from neo4jrestclient.client import GraphDatabase, Node, Relationship
+
+app = Flask(__name__, static_url_path='/static/')
+#gdb = GraphDatabase("http://localhost:7474")
+gdb = GraphDatabase("http://euler.mpiwg-berlin.mpg.de:7474")
+
+
+@app.route('/json/<path:path>')
+def send_json(path):
+    return send_from_directory('static/json', path)
+
+@app.route('/platin/<path:path>')
+def send_platin(path):
+    return send_from_directory('static/platin', path)
+
+
+
+@app.route("/")
+def get_index():
+    return app.send_static_file('index.html')
+
+@app.route("/sphaera")
+def get_sphaera():
+    return app.send_static_file('index_sphaera.html')
+
+
+@app.route("/graph")
+def get_graph():
+   
+    query = ("MATCH (m:Edition)<-[r]-(e:Edition) "
+             "RETURN m,r,e "
+             "LIMIT {limit}")
+    results = gdb.query(query,
+                        params={"limit": request.args.get("limit", 10000)},returns=(Node,Relationship,Node))
+    nodes = []
+    rels = []
+    nodesIdSet=set()
+    i = 0
+    for ed1,r,ed2   in results:
+        ed1id = ed1.properties["Nid"]
+      
+        #lon = ed1.properties.get("lon","")
+        #lat = ed1.properties.get("lat","")
+        node = ed1.properties.copy()
+        node["title"]=ed1id
+        if ed1id not in nodesIdSet:    
+            nodes.append(node)
+            nodesIdSet.add(ed1id)
+            
+        target = nodes.index(node)
+          
+        
+        #lon = ed2.properties.get("lon","")
+        #lat = ed2.properties.get("lat","")
+        ed2id = ed2.properties["Nid"]
+        node = ed2.properties.copy()
+        node["title"]=ed2id
+          
+        if ed2id not in nodesIdSet:
+            nodes.append(node)
+            nodesIdSet.add(ed2id)
+            
+        source =nodes.index(node)
+        
+        rels.append({"source": source, "target": target})
+        
+    return Response(dumps({"nodes": nodes, "links": rels}),
+                    mimetype="application/json")
+
+
+@app.route("/search")
+def get_search():
+    try:
+        q = request.args["q"]
+    except KeyError:
+        return []
+    else:
+        query = ("MATCH (ed:Edition) "
+                 "WHERE ed.name =~ {title} "
+                 "RETURN ed")
+        results = gdb.query(
+            query,
+            returns=Node,
+            params={"title": "(?i).*" + q + ".*"}
+        )
+       
+        return Response(dumps([{"edition": row.properties}
+                               for [row] in results]),
+                        mimetype="application/json")
+
+
+@app.route("/edition/<nid>")
+def get_movie(nid):
+    query = ("MATCH (ed:Edition {Nid:{Nid}}) "
+             "RETURN ed.Nid as Nid,"
+             "ed.Title as Title "
+             "LIMIT 1")
+    results = gdb.query(query, params={"Nid": nid})
+    nid, title = results[0]
+    return Response(dumps({"title": nid,
+                           "name":  title}),
+                    mimetype="application/json")
+
+
+if __name__ == '__main__':
+    app.debug = True
+    app.run(port=8080)