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