Mercurial > hg > NetworkVis
annotate ismi-python-neo4jrestclient/ismi-vis.py @ 25:f82512502b31
Initial commit for query builder, still need to tailor for ISMI purposes
author | alistair |
---|---|
date | Mon, 23 Nov 2015 02:03:51 -0500 |
parents | 80010fb9dbce |
children | 2564732249b3 |
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 |
16 | 4 from flask import Flask, Response, request, send_from_directory |
6
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/') |
13 | 9 app.debug = True |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
10 gdb = GraphDatabase("http://localhost:7474", username="neo4j", password="neo5j") |
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 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
13 @app.route("/") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
14 def get_index(): |
13 | 15 return app.send_static_file('commentaries.html') |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
16 |
9 | 17 @app.route("/commentaries.html") |
18 def get_commentaries(): | |
19 return app.send_static_file('commentaries.html') | |
20 | |
13 | 21 @app.route("/commentaries_authors.html") |
22 def get_commentaries_authors(): | |
23 | 23 return app.send_static_file('commentaries_authors2.html') |
16 | 24 |
25 @app.route('/static/<filename>') | |
26 def get_file(filename): | |
27 return send_from_directory('static', filename) | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
28 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
29 @app.route("/graph") |
16 | 30 def get_graph_commentaries(): |
31 query = ("match (t1:TEXT)-[r:is_commentary_on]->(t2:TEXT)" | |
32 " return t1,t2" | |
33 " limit {limit}") | |
34 results = gdb.query(query, returns=(Node,Node), | |
35 params={"limit": int(request.args.get("limit", 100))}) | |
36 | |
37 n4j_nodes = {} | |
38 node_idx = {} | |
39 nodes = [] | |
40 rels = [] | |
41 i = 0 | |
42 for text1, text2 in results: | |
43 # source text | |
44 id1 = text1['ismi_id'] | |
45 if id1 not in n4j_nodes: | |
46 n4j_nodes[id1] = text1 | |
47 nodes.append({"title": "%s [%s]"%(text1['label'],id1), "label": "TEXT", "ismi_id": id1}) | |
48 node_idx[id1] = i | |
49 source = i | |
50 i += 1 | |
51 | |
52 else: | |
53 source = node_idx[id1] | |
54 | |
55 # target text | |
56 id2 = text2['ismi_id'] | |
57 if id2 not in n4j_nodes: | |
58 n4j_nodes[id2] = text2 | |
59 nodes.append({"title": "%s [%s]"%(text2['label'],id2), "label": "TEXT", "ismi_id": id2}) | |
60 node_idx[id2] = i | |
61 target = i | |
62 i += 1 | |
63 | |
64 else: | |
65 target = node_idx[id2] | |
66 | |
67 # relation is_commentary_on | |
68 rels.append({"source": source, "target": target}) | |
69 | |
70 return Response(dumps({"nodes": nodes, "links": rels}), | |
71 mimetype="application/json") | |
72 | |
73 | |
74 @app.route("/graph_commentaries_authors") | |
75 def get_graph_commentaries_authors(): | |
13 | 76 query = ("match (a1:PERSON)<-[:was_created_by]-(t1:TEXT)-[r:is_commentary_on]->(t2:TEXT)-[:was_created_by]->(a2:PERSON)" |
77 " return a1,t1,t2,a2" | |
78 " limit 100") | |
79 results = gdb.query(query, returns=(Node,Node,Node,Node)) | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
80 n4j_nodes = {} |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
81 node_idx = {} |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
82 nodes = [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
83 rels = [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
84 i = 0 |
13 | 85 for author1, text1, text2, author2 in results: |
86 # source text | |
87 id1 = text1['ismi_id'] | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
88 if id1 not in n4j_nodes: |
13 | 89 n4j_nodes[id1] = text1 |
90 nodes.append({"title": "%s [%s]"%(text1['label'],id1), "label": "TEXT"}) | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
91 node_idx[id1] = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
92 source = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
93 i += 1 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
94 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
95 else: |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
96 source = node_idx[id1] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
97 |
13 | 98 # target text |
99 id2 = text2['ismi_id'] | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
100 if id2 not in n4j_nodes: |
13 | 101 n4j_nodes[id2] = text2 |
102 nodes.append({"title": "%s [%s]"%(text2['label'],id2), "label": "TEXT"}) | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
103 node_idx[id2] = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
104 target = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
105 i += 1 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
106 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
107 else: |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
108 target = node_idx[id2] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
109 |
13 | 110 # relation is_commentary_on |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
111 rels.append({"source": source, "target": target}) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
112 |
13 | 113 # source author |
114 id3 = author1['ismi_id'] | |
115 if id3 not in n4j_nodes: | |
116 n4j_nodes[id3] = author1 | |
117 nodes.append({"title": "%s [%s]"%(author1['label'],id3), "label": "PERSON"}) | |
118 node_idx[id3] = i | |
119 s_author = i | |
120 i += 1 | |
121 | |
122 else: | |
123 s_author = node_idx[id3] | |
124 | |
125 # relation was_created_by | |
126 rels.append({"source": source, "target": s_author}) | |
127 | |
128 # target author | |
129 id4 = author1['ismi_id'] | |
130 if id4 not in n4j_nodes: | |
131 n4j_nodes[id4] = author2 | |
132 nodes.append({"title": "%s [%s]"%(author2['label'],id4), "label": "PERSON"}) | |
133 node_idx[id4] = i | |
134 t_author = i | |
135 i += 1 | |
136 | |
137 else: | |
138 t_author = node_idx[id4] | |
139 | |
140 # relation was_created_by | |
141 rels.append({"source": source, "target": t_author}) | |
142 | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
143 return Response(dumps({"nodes": nodes, "links": rels}), |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
144 mimetype="application/json") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
145 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
146 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
147 @app.route("/search") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
148 def get_search(): |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
149 try: |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
150 q = request.args["q"] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
151 except KeyError: |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
152 return [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
153 else: |
17 | 154 query = ("MATCH (t:TEXT)-[:was_created_by]->(p:PERSON {ismi_id: {id}})" |
155 " RETURN t,p,exists((t)-[:is_commentary_on]->()),exists(()-[:is_commentary_on]->(t))") | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
156 results = gdb.query( |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
157 query, |
17 | 158 returns=(Node,Node,bool,bool), |
13 | 159 params={"id": int(q)} |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
160 ) |
13 | 161 # {"name": "(?i).*" + q + ".*"} |
162 print("search for %s returned %s results"%(repr(q),len(results))) | |
17 | 163 data = [{"text": t.properties, |
164 "author": a.properties, | |
165 "is_commentary": is_com, | |
166 "has_commentaries": has_com} | |
167 for [t,a,is_com,has_com] in results] | |
168 return Response(dumps(data), | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
169 mimetype="application/json") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
170 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
171 |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
172 @app.route("/text/<text_id>") |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
173 def get_text(text_id): |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
174 query = ("MATCH (text:TEXT {ismi_id:{text_id}}) " |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
175 " RETURN text" |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
176 " LIMIT 1") |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
177 results = gdb.query(query, returns=Node, params={"text_id": int(text_id)}) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
178 node = results[0][0] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
179 print("node:%s"%repr(node)) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
180 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
|
181 mimetype="application/json") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
182 |
13 | 183 @app.route("/textandcommentaries/<text_id>") |
184 def get_textandcommentaries(text_id): | |
185 query = ("match (t:TEXT {ismi_id:{text_id}})" | |
14 | 186 " optional match (t)-[:was_created_by]->(a:PERSON)" |
187 " optional match (s:TEXT)<-[:is_commentary_on]-(t)" | |
188 " optional match (s)-[:was_created_by]->(sa:PERSON)" | |
189 " optional match (t)<-[:is_commentary_on]-(c:TEXT)" | |
190 " optional match (c)-[:was_created_by]->(ca:PERSON)" | |
21 | 191 " return t,a.label,a.ismi_id,s.label,s.ismi_id,sa.label,c.label,c.ismi_id,ca.label") |
13 | 192 print("query:%s"%query) |
21 | 193 results = gdb.query(query, returns=(Node,str,str,str,str,str,str,str,str), |
13 | 194 params={"text_id": int(text_id)}) |
195 | |
196 print("result:%s"%results) | |
197 text = None | |
14 | 198 author = None |
13 | 199 scs = {} |
200 cs = {} | |
21 | 201 for [t,a_label,a_id,s_label,s_id,sa_label,c_label,c_id,ca_label] in results: |
13 | 202 text = t |
14 | 203 author = a_label |
21 | 204 author_id = a_id |
13 | 205 if s_id is not None and s_id != "None": |
14 | 206 scs[int(s_id)] = {"title": s_label, "author":sa_label} |
13 | 207 |
208 if c_id is not None and c_id != "None": | |
14 | 209 cs[int(c_id)] = {"title": c_label, "author":ca_label} |
13 | 210 |
211 print("text:%s scs:%s cs:%s"%(text, scs, cs)) | |
212 return Response(dumps({"title": text['label'], "attrs": text.properties, | |
21 | 213 "author": {"label": author, "ismi_id": author_id}, |
13 | 214 "commenting": scs, "commentaries": cs}), |
215 mimetype="application/json") | |
216 | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
217 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
218 if __name__ == '__main__': |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
219 app.run(port=8080) |