Mercurial > hg > NetworkVis
annotate ismi-python-neo4jrestclient/ismi-vis.py @ 10:04b69bcf4be5
merge
author | casties |
---|---|
date | Thu, 01 Oct 2015 17:37:09 +0200 |
parents | 0f4846255b20 |
children | 394bd5cfc508 |
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 |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
6 from neo4jrestclient.client import GraphDatabase, Node, Relationship |
6
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 |
9 | 16 @app.route("/commentaries.html") |
17 def get_commentaries(): | |
18 return app.send_static_file('commentaries.html') | |
19 | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
20 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
21 @app.route("/graph") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
22 def get_graph(): |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
23 query = ("match (t1:TEXT)-[r:is_commentary_on]->(t2:TEXT) return t1,r,t2") |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
24 results = gdb.query(query, returns=(Node,Relationship,Node)) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
25 n4j_nodes = {} |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
26 node_idx = {} |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
27 nodes = [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
28 rels = [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
29 i = 0 |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
30 for node1, rel, node2 in results: |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
31 # source node |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
32 id1 = node1['ismi_id'] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
33 if id1 not in n4j_nodes: |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
34 n4j_nodes[id1] = node1 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
35 nodes.append({"title": node1['label'], "label": "TEXT"}) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
36 node_idx[id1] = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
37 source = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
38 i += 1 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
39 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
40 else: |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
41 source = node_idx[id1] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
42 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
43 # target node |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
44 id2 = node2['ismi_id'] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
45 if id2 not in n4j_nodes: |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
46 n4j_nodes[id2] = node2 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
47 nodes.append({"title": node2['label'], "label": "TEXT"}) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
48 node_idx[id2] = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
49 target = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
50 i += 1 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
51 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
52 else: |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
53 target = node_idx[id2] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
54 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
55 # relation |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
56 rels.append({"source": source, "target": target}) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
57 |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
58 return Response(dumps({"nodes": nodes, "links": rels}), |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
59 mimetype="application/json") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
60 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
61 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
62 @app.route("/search") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
63 def get_search(): |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
64 try: |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
65 q = request.args["q"] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
66 except KeyError: |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
67 return [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
68 else: |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
69 query = ("MATCH (text:TEXT) " |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
70 "WHERE text.label =~ {title} " |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
71 "RETURN text") |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
72 results = gdb.query( |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
73 query, |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
74 returns=Node, |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
75 params={"title": "(?i).*" + q + ".*"} |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
76 ) |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
77 return Response(dumps([{"text": row.properties} for [row] in results]), |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
78 mimetype="application/json") |
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 |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
81 @app.route("/text/<text_id>") |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
82 def get_text(text_id): |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
83 query = ("MATCH (text:TEXT {ismi_id:{text_id}}) " |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
84 " RETURN text" |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
85 " LIMIT 1") |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
86 results = gdb.query(query, returns=Node, params={"text_id": int(text_id)}) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
87 node = results[0][0] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
88 print("node:%s"%repr(node)) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
89 return Response(dumps({"title": node['label'], "attrs": node.properties}), |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
90 mimetype="application/json") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
91 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
92 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
93 if __name__ == '__main__': |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
94 app.run(port=8080) |