Mercurial > hg > NetworkVis
annotate ismi-python-neo4jrestclient/ismi-vis.py @ 39:88c5232f9d48 default tip
fix click on author name.
author | casties |
---|---|
date | Thu, 04 Feb 2016 18:56:48 +0100 |
parents | 7b5dcd3238d2 |
children |
rev | line source |
---|---|
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
1 #!/usr/bin/env python |
29 | 2 from json import dumps, loads |
31
a6b2a09ea413
fix commentary chain visualisation author name display and search by clicking author name.
casties
parents:
29
diff
changeset
|
3 import urllib.request, urllib.parse |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
4 |
16 | 5 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
|
6 |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
7 from neo4jrestclient.client import GraphDatabase, Node, Relationship |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
8 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
9 app = Flask(__name__, static_url_path='/static/') |
13 | 10 app.debug = True |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
11 gdb = GraphDatabase("http://localhost:7474", username="neo4j", password="neo5j") |
29 | 12 ismi_db_baseurl = "http://localhost:18080/ismi-richfaces/" |
13 | |
14 def loadJSON(url): | |
15 #print("JSON loading %s"%url) | |
16 wsh=urllib.request.urlopen(url) | |
17 txt = wsh.read() | |
18 return loads(txt.decode("utf-8")) | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
19 |
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("/") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
22 def get_index(): |
31
a6b2a09ea413
fix commentary chain visualisation author name display and search by clicking author name.
casties
parents:
29
diff
changeset
|
23 return app.send_static_file('commentaries.html') |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
24 |
9 | 25 @app.route("/commentaries.html") |
26 def get_commentaries(): | |
27 return app.send_static_file('commentaries.html') | |
28 | |
16 | 29 @app.route('/static/<filename>') |
30 def get_file(filename): | |
31 return send_from_directory('static', filename) | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
32 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
33 @app.route("/graph") |
16 | 34 def get_graph_commentaries(): |
35 query = ("match (t1:TEXT)-[r:is_commentary_on]->(t2:TEXT)" | |
36 " return t1,t2" | |
37 " limit {limit}") | |
38 results = gdb.query(query, returns=(Node,Node), | |
39 params={"limit": int(request.args.get("limit", 100))}) | |
40 | |
41 n4j_nodes = {} | |
42 node_idx = {} | |
43 nodes = [] | |
44 rels = [] | |
45 i = 0 | |
46 for text1, text2 in results: | |
47 # source text | |
48 id1 = text1['ismi_id'] | |
49 if id1 not in n4j_nodes: | |
50 n4j_nodes[id1] = text1 | |
51 nodes.append({"title": "%s [%s]"%(text1['label'],id1), "label": "TEXT", "ismi_id": id1}) | |
52 node_idx[id1] = i | |
53 source = i | |
54 i += 1 | |
55 | |
56 else: | |
57 source = node_idx[id1] | |
58 | |
59 # target text | |
60 id2 = text2['ismi_id'] | |
61 if id2 not in n4j_nodes: | |
62 n4j_nodes[id2] = text2 | |
63 nodes.append({"title": "%s [%s]"%(text2['label'],id2), "label": "TEXT", "ismi_id": id2}) | |
64 node_idx[id2] = i | |
65 target = i | |
66 i += 1 | |
67 | |
68 else: | |
69 target = node_idx[id2] | |
70 | |
71 # relation is_commentary_on | |
72 rels.append({"source": source, "target": target}) | |
73 | |
74 return Response(dumps({"nodes": nodes, "links": rels}), | |
75 mimetype="application/json") | |
76 | |
77 | |
78 @app.route("/graph_commentaries_authors") | |
79 def get_graph_commentaries_authors(): | |
13 | 80 query = ("match (a1:PERSON)<-[:was_created_by]-(t1:TEXT)-[r:is_commentary_on]->(t2:TEXT)-[:was_created_by]->(a2:PERSON)" |
81 " return a1,t1,t2,a2" | |
82 " limit 100") | |
83 results = gdb.query(query, returns=(Node,Node,Node,Node)) | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
84 n4j_nodes = {} |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
85 node_idx = {} |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
86 nodes = [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
87 rels = [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
88 i = 0 |
13 | 89 for author1, text1, text2, author2 in results: |
90 # source text | |
91 id1 = text1['ismi_id'] | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
92 if id1 not in n4j_nodes: |
13 | 93 n4j_nodes[id1] = text1 |
94 nodes.append({"title": "%s [%s]"%(text1['label'],id1), "label": "TEXT"}) | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
95 node_idx[id1] = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
96 source = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
97 i += 1 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
98 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
99 else: |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
100 source = node_idx[id1] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
101 |
13 | 102 # target text |
103 id2 = text2['ismi_id'] | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
104 if id2 not in n4j_nodes: |
13 | 105 n4j_nodes[id2] = text2 |
106 nodes.append({"title": "%s [%s]"%(text2['label'],id2), "label": "TEXT"}) | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
107 node_idx[id2] = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
108 target = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
109 i += 1 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
110 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
111 else: |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
112 target = node_idx[id2] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
113 |
13 | 114 # relation is_commentary_on |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
115 rels.append({"source": source, "target": target}) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
116 |
13 | 117 # source author |
118 id3 = author1['ismi_id'] | |
119 if id3 not in n4j_nodes: | |
120 n4j_nodes[id3] = author1 | |
121 nodes.append({"title": "%s [%s]"%(author1['label'],id3), "label": "PERSON"}) | |
122 node_idx[id3] = i | |
123 s_author = i | |
124 i += 1 | |
125 | |
126 else: | |
127 s_author = node_idx[id3] | |
128 | |
129 # relation was_created_by | |
130 rels.append({"source": source, "target": s_author}) | |
131 | |
132 # target author | |
133 id4 = author1['ismi_id'] | |
134 if id4 not in n4j_nodes: | |
135 n4j_nodes[id4] = author2 | |
136 nodes.append({"title": "%s [%s]"%(author2['label'],id4), "label": "PERSON"}) | |
137 node_idx[id4] = i | |
138 t_author = i | |
139 i += 1 | |
140 | |
141 else: | |
142 t_author = node_idx[id4] | |
143 | |
144 # relation was_created_by | |
145 rels.append({"source": source, "target": t_author}) | |
146 | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
147 return Response(dumps({"nodes": nodes, "links": rels}), |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
148 mimetype="application/json") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
149 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
150 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
151 @app.route("/search") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
152 def get_search(): |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
153 try: |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
154 q = request.args["q"] |
31
a6b2a09ea413
fix commentary chain visualisation author name display and search by clicking author name.
casties
parents:
29
diff
changeset
|
155 qs = urllib.parse.quote(q) |
37
7b5dcd3238d2
search field also accepts ismi_ids. display list of authors in head. display author name in title list.
casties
parents:
35
diff
changeset
|
156 |
31
a6b2a09ea413
fix commentary chain visualisation author name display and search by clicking author name.
casties
parents:
29
diff
changeset
|
157 norm_js = loadJSON(ismi_db_baseurl + "jsonInterface?method=normalize_string&type=arabic_translit&text=%s"%qs) |
29 | 158 nq = norm_js.get("normalized_text") |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
159 except KeyError: |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
160 return [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
161 else: |
37
7b5dcd3238d2
search field also accepts ismi_ids. display list of authors in head. display author name in title list.
casties
parents:
35
diff
changeset
|
162 if qs.isdigit(): |
7b5dcd3238d2
search field also accepts ismi_ids. display list of authors in head. display author name in title list.
casties
parents:
35
diff
changeset
|
163 query = ("MATCH (t:TEXT)-[:was_created_by]->(p:PERSON {ismi_id: {id}})" |
7b5dcd3238d2
search field also accepts ismi_ids. display list of authors in head. display author name in title list.
casties
parents:
35
diff
changeset
|
164 " RETURN t,p,exists((t)-[:is_commentary_on]->()),exists(()-[:is_commentary_on]->(t))") |
7b5dcd3238d2
search field also accepts ismi_ids. display list of authors in head. display author name in title list.
casties
parents:
35
diff
changeset
|
165 param = {"id": int(qs)} |
7b5dcd3238d2
search field also accepts ismi_ids. display list of authors in head. display author name in title list.
casties
parents:
35
diff
changeset
|
166 |
7b5dcd3238d2
search field also accepts ismi_ids. display list of authors in head. display author name in title list.
casties
parents:
35
diff
changeset
|
167 else: |
7b5dcd3238d2
search field also accepts ismi_ids. display list of authors in head. display author name in title list.
casties
parents:
35
diff
changeset
|
168 query = ("MATCH (t:TEXT)-[:was_created_by]->(p:PERSON)" |
7b5dcd3238d2
search field also accepts ismi_ids. display list of authors in head. display author name in title list.
casties
parents:
35
diff
changeset
|
169 " WHERE p._n_label =~ {name}" |
7b5dcd3238d2
search field also accepts ismi_ids. display list of authors in head. display author name in title list.
casties
parents:
35
diff
changeset
|
170 " RETURN t,p,exists((t)-[:is_commentary_on]->()),exists(()-[:is_commentary_on]->(t))") |
7b5dcd3238d2
search field also accepts ismi_ids. display list of authors in head. display author name in title list.
casties
parents:
35
diff
changeset
|
171 param = {"name": ".*%s.*"%nq.lower()} |
7b5dcd3238d2
search field also accepts ismi_ids. display list of authors in head. display author name in title list.
casties
parents:
35
diff
changeset
|
172 |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
173 results = gdb.query( |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
174 query, |
17 | 175 returns=(Node,Node,bool,bool), |
37
7b5dcd3238d2
search field also accepts ismi_ids. display list of authors in head. display author name in title list.
casties
parents:
35
diff
changeset
|
176 params=param |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
177 ) |
13 | 178 # {"name": "(?i).*" + q + ".*"} |
179 print("search for %s returned %s results"%(repr(q),len(results))) | |
17 | 180 data = [{"text": t.properties, |
181 "author": a.properties, | |
182 "is_commentary": is_com, | |
183 "has_commentaries": has_com} | |
184 for [t,a,is_com,has_com] in results] | |
185 return Response(dumps(data), | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
186 mimetype="application/json") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
187 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
188 |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
189 @app.route("/text/<text_id>") |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
190 def get_text(text_id): |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
191 query = ("MATCH (text:TEXT {ismi_id:{text_id}}) " |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
192 " RETURN text" |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
193 " LIMIT 1") |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
194 results = gdb.query(query, returns=Node, params={"text_id": int(text_id)}) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
195 node = results[0][0] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
196 print("node:%s"%repr(node)) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
197 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
|
198 mimetype="application/json") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
199 |
13 | 200 @app.route("/textandcommentaries/<text_id>") |
201 def get_textandcommentaries(text_id): | |
202 query = ("match (t:TEXT {ismi_id:{text_id}})" | |
14 | 203 " optional match (t)-[:was_created_by]->(a:PERSON)" |
204 " optional match (s:TEXT)<-[:is_commentary_on]-(t)" | |
205 " optional match (s)-[:was_created_by]->(sa:PERSON)" | |
206 " optional match (t)<-[:is_commentary_on]-(c:TEXT)" | |
207 " optional match (c)-[:was_created_by]->(ca:PERSON)" | |
21 | 208 " return t,a.label,a.ismi_id,s.label,s.ismi_id,sa.label,c.label,c.ismi_id,ca.label") |
13 | 209 print("query:%s"%query) |
21 | 210 results = gdb.query(query, returns=(Node,str,str,str,str,str,str,str,str), |
13 | 211 params={"text_id": int(text_id)}) |
212 | |
213 print("result:%s"%results) | |
214 text = None | |
14 | 215 author = None |
13 | 216 scs = {} |
217 cs = {} | |
21 | 218 for [t,a_label,a_id,s_label,s_id,sa_label,c_label,c_id,ca_label] in results: |
13 | 219 text = t |
14 | 220 author = a_label |
21 | 221 author_id = a_id |
13 | 222 if s_id is not None and s_id != "None": |
14 | 223 scs[int(s_id)] = {"title": s_label, "author":sa_label} |
13 | 224 |
225 if c_id is not None and c_id != "None": | |
14 | 226 cs[int(c_id)] = {"title": c_label, "author":ca_label} |
13 | 227 |
228 print("text:%s scs:%s cs:%s"%(text, scs, cs)) | |
229 return Response(dumps({"title": text['label'], "attrs": text.properties, | |
21 | 230 "author": {"label": author, "ismi_id": author_id}, |
13 | 231 "commenting": scs, "commentaries": cs}), |
232 mimetype="application/json") | |
233 | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
234 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
235 if __name__ == '__main__': |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
236 app.run(port=8080) |