Mercurial > hg > NetworkVis
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 7:45dad9e38c82 | 8:18ef6948d689 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 from json import dumps | |
| 3 | |
| 4 from flask import Flask, Response, request, send_from_directory | |
| 5 | |
| 6 from neo4jrestclient.client import GraphDatabase, Node, Relationship | |
| 7 | |
| 8 app = Flask(__name__, static_url_path='/static/') | |
| 9 #gdb = GraphDatabase("http://localhost:7474") | |
| 10 gdb = GraphDatabase("http://euler.mpiwg-berlin.mpg.de:7474") | |
| 11 | |
| 12 | |
| 13 @app.route('/json/<path:path>') | |
| 14 def send_json(path): | |
| 15 return send_from_directory('static/json', path) | |
| 16 | |
| 17 @app.route('/platin/<path:path>') | |
| 18 def send_platin(path): | |
| 19 return send_from_directory('static/platin', path) | |
| 20 | |
| 21 | |
| 22 | |
| 23 @app.route("/") | |
| 24 def get_index(): | |
| 25 return app.send_static_file('index.html') | |
| 26 | |
| 27 @app.route("/sphaera") | |
| 28 def get_sphaera(): | |
| 29 return app.send_static_file('index_sphaera.html') | |
| 30 | |
| 31 | |
| 32 @app.route("/graph") | |
| 33 def get_graph(): | |
| 34 | |
| 35 query = ("MATCH (m:Edition)<-[r]-(e:Edition) " | |
| 36 "RETURN m,r,e " | |
| 37 "LIMIT {limit}") | |
| 38 results = gdb.query(query, | |
| 39 params={"limit": request.args.get("limit", 10000)},returns=(Node,Relationship,Node)) | |
| 40 nodes = [] | |
| 41 rels = [] | |
| 42 nodesIdSet=set() | |
| 43 i = 0 | |
| 44 for ed1,r,ed2 in results: | |
| 45 ed1id = ed1.properties["Nid"] | |
| 46 | |
| 47 #lon = ed1.properties.get("lon","") | |
| 48 #lat = ed1.properties.get("lat","") | |
| 49 node = ed1.properties.copy() | |
| 50 node["title"]=ed1id | |
| 51 if ed1id not in nodesIdSet: | |
| 52 nodes.append(node) | |
| 53 nodesIdSet.add(ed1id) | |
| 54 | |
| 55 target = nodes.index(node) | |
| 56 | |
| 57 | |
| 58 #lon = ed2.properties.get("lon","") | |
| 59 #lat = ed2.properties.get("lat","") | |
| 60 ed2id = ed2.properties["Nid"] | |
| 61 node = ed2.properties.copy() | |
| 62 node["title"]=ed2id | |
| 63 | |
| 64 if ed2id not in nodesIdSet: | |
| 65 nodes.append(node) | |
| 66 nodesIdSet.add(ed2id) | |
| 67 | |
| 68 source =nodes.index(node) | |
| 69 | |
| 70 rels.append({"source": source, "target": target}) | |
| 71 | |
| 72 return Response(dumps({"nodes": nodes, "links": rels}), | |
| 73 mimetype="application/json") | |
| 74 | |
| 75 | |
| 76 @app.route("/search") | |
| 77 def get_search(): | |
| 78 try: | |
| 79 q = request.args["q"] | |
| 80 except KeyError: | |
| 81 return [] | |
| 82 else: | |
| 83 query = ("MATCH (ed:Edition) " | |
| 84 "WHERE ed.name =~ {title} " | |
| 85 "RETURN ed") | |
| 86 results = gdb.query( | |
| 87 query, | |
| 88 returns=Node, | |
| 89 params={"title": "(?i).*" + q + ".*"} | |
| 90 ) | |
| 91 | |
| 92 return Response(dumps([{"edition": row.properties} | |
| 93 for [row] in results]), | |
| 94 mimetype="application/json") | |
| 95 | |
| 96 | |
| 97 @app.route("/edition/<nid>") | |
| 98 def get_movie(nid): | |
| 99 query = ("MATCH (ed:Edition {Nid:{Nid}}) " | |
| 100 "RETURN ed.Nid as Nid," | |
| 101 "ed.Title as Title " | |
| 102 "LIMIT 1") | |
| 103 results = gdb.query(query, params={"Nid": nid}) | |
| 104 nid, title = results[0] | |
| 105 return Response(dumps({"title": nid, | |
| 106 "name": title}), | |
| 107 mimetype="application/json") | |
| 108 | |
| 109 | |
| 110 if __name__ == '__main__': | |
| 111 app.debug = True | |
| 112 app.run(port=8080) |
