Mercurial > hg > NetworkVis
comparison ismi-python-neo4jrestclient/ismi-vis.py @ 6:aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
author | casties |
---|---|
date | Mon, 28 Sep 2015 18:15:35 +0200 |
parents | |
children | 45dad9e38c82 |
comparison
equal
deleted
inserted
replaced
5:fa1b4fa5b4f8 | 6:aeef1fedd899 |
---|---|
1 #!/usr/bin/env python | |
2 from json import dumps | |
3 | |
4 from flask import Flask, Response, request | |
5 | |
6 from neo4jrestclient.client import GraphDatabase, Node | |
7 | |
8 app = Flask(__name__, static_url_path='/static/') | |
9 gdb = GraphDatabase("http://localhost:7474", username="neo4j", password="neo5j") | |
10 | |
11 | |
12 @app.route("/") | |
13 def get_index(): | |
14 return app.send_static_file('index.html') | |
15 | |
16 | |
17 @app.route("/graph") | |
18 def get_graph(): | |
19 query = ("MATCH (m:Movie)<-[:ACTED_IN]-(a:Person) " | |
20 "RETURN m.title as movie, collect(a.name) as cast " | |
21 "LIMIT {limit}") | |
22 results = gdb.query(query, | |
23 params={"limit": request.args.get("limit", 100)}) | |
24 nodes = [] | |
25 rels = [] | |
26 i = 0 | |
27 for movie, cast in results: | |
28 nodes.append({"title": movie, "label": "movie"}) | |
29 target = i | |
30 i += 1 | |
31 for name in cast: | |
32 actor = {"title": name, "label": "actor"} | |
33 try: | |
34 source = nodes.index(actor) | |
35 except ValueError: | |
36 nodes.append(actor) | |
37 source = i | |
38 i += 1 | |
39 rels.append({"source": source, "target": target}) | |
40 return Response(dumps({"nodes": nodes, "links": rels}), | |
41 mimetype="application/json") | |
42 | |
43 | |
44 @app.route("/search") | |
45 def get_search(): | |
46 try: | |
47 q = request.args["q"] | |
48 except KeyError: | |
49 return [] | |
50 else: | |
51 query = ("MATCH (movie:Movie) " | |
52 "WHERE movie.title =~ {title} " | |
53 "RETURN movie") | |
54 results = gdb.query( | |
55 query, | |
56 returns=Node, | |
57 params={"title": "(?i).*" + q + ".*"} | |
58 ) | |
59 return Response(dumps([{"movie": row.properties} | |
60 for [row] in results]), | |
61 mimetype="application/json") | |
62 | |
63 | |
64 @app.route("/movie/<title>") | |
65 def get_movie(title): | |
66 query = ("MATCH (movie:Movie {title:{title}}) " | |
67 "OPTIONAL MATCH (movie)<-[r]-(person:Person) " | |
68 "RETURN movie.title as title," | |
69 "collect([person.name, " | |
70 " head(split(lower(type(r)), '_')), r.roles]) as cast " | |
71 "LIMIT 1") | |
72 results = gdb.query(query, params={"title": title}) | |
73 title, cast = results[0] | |
74 return Response(dumps({"title": title, | |
75 "cast": [dict(zip(("name", "job", "role"), member)) | |
76 for member in cast]}), | |
77 mimetype="application/json") | |
78 | |
79 | |
80 if __name__ == '__main__': | |
81 app.run(port=8080) |