Mercurial > hg > NetworkVis
annotate ismi-python-neo4jrestclient/ismi-vis.py @ 14:b33f35b57b93
added authors to details view.
author | casties |
---|---|
date | Mon, 05 Oct 2015 19:30:28 +0200 |
parents | 394bd5cfc508 |
children | ad3eefa2cb80 |
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/') |
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 return app.send_static_file('commentaries_authors.html') | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
24 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
25 @app.route("/graph") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
26 def get_graph(): |
13 | 27 query = ("match (a1:PERSON)<-[:was_created_by]-(t1:TEXT)-[r:is_commentary_on]->(t2:TEXT)-[:was_created_by]->(a2:PERSON)" |
28 " return a1,t1,t2,a2" | |
29 " limit 100") | |
30 results = gdb.query(query, returns=(Node,Node,Node,Node)) | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
31 n4j_nodes = {} |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
32 node_idx = {} |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
33 nodes = [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
34 rels = [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
35 i = 0 |
13 | 36 for author1, text1, text2, author2 in results: |
37 # source text | |
38 id1 = text1['ismi_id'] | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
39 if id1 not in n4j_nodes: |
13 | 40 n4j_nodes[id1] = text1 |
41 nodes.append({"title": "%s [%s]"%(text1['label'],id1), "label": "TEXT"}) | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
42 node_idx[id1] = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
43 source = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
44 i += 1 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
45 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
46 else: |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
47 source = node_idx[id1] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
48 |
13 | 49 # target text |
50 id2 = text2['ismi_id'] | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
51 if id2 not in n4j_nodes: |
13 | 52 n4j_nodes[id2] = text2 |
53 nodes.append({"title": "%s [%s]"%(text2['label'],id2), "label": "TEXT"}) | |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
54 node_idx[id2] = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
55 target = i |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
56 i += 1 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
57 |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
58 else: |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
59 target = node_idx[id2] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
60 |
13 | 61 # relation is_commentary_on |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
62 rels.append({"source": source, "target": target}) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
63 |
13 | 64 # source author |
65 id3 = author1['ismi_id'] | |
66 if id3 not in n4j_nodes: | |
67 n4j_nodes[id3] = author1 | |
68 nodes.append({"title": "%s [%s]"%(author1['label'],id3), "label": "PERSON"}) | |
69 node_idx[id3] = i | |
70 s_author = i | |
71 i += 1 | |
72 | |
73 else: | |
74 s_author = node_idx[id3] | |
75 | |
76 # relation was_created_by | |
77 rels.append({"source": source, "target": s_author}) | |
78 | |
79 # target author | |
80 id4 = author1['ismi_id'] | |
81 if id4 not in n4j_nodes: | |
82 n4j_nodes[id4] = author2 | |
83 nodes.append({"title": "%s [%s]"%(author2['label'],id4), "label": "PERSON"}) | |
84 node_idx[id4] = i | |
85 t_author = i | |
86 i += 1 | |
87 | |
88 else: | |
89 t_author = node_idx[id4] | |
90 | |
91 # relation was_created_by | |
92 rels.append({"source": source, "target": t_author}) | |
93 | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
94 return Response(dumps({"nodes": nodes, "links": rels}), |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
95 mimetype="application/json") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
96 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
97 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
98 @app.route("/search") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
99 def get_search(): |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
100 try: |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
101 q = request.args["q"] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
102 except KeyError: |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
103 return [] |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
104 else: |
13 | 105 query = ("MATCH (t:TEXT)-[:was_created_by]->(p:PERSON) " |
106 "WHERE p.ismi_id = {id} " | |
107 "RETURN t,p") | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
108 results = gdb.query( |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
109 query, |
13 | 110 returns=(Node,Node), |
111 params={"id": int(q)} | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
112 ) |
13 | 113 # {"name": "(?i).*" + q + ".*"} |
114 print("search for %s returned %s results"%(repr(q),len(results))) | |
115 return Response(dumps([{"text": text.properties, "author": author.properties} for [text,author] in results]), | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
116 mimetype="application/json") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
117 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
118 |
7
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
119 @app.route("/text/<text_id>") |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
120 def get_text(text_id): |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
121 query = ("MATCH (text:TEXT {ismi_id:{text_id}}) " |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
122 " RETURN text" |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
123 " LIMIT 1") |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
124 results = gdb.query(query, returns=Node, params={"text_id": int(text_id)}) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
125 node = results[0][0] |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
126 print("node:%s"%repr(node)) |
45dad9e38c82
first functional version of commentary visualisation.
casties
parents:
6
diff
changeset
|
127 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
|
128 mimetype="application/json") |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
129 |
13 | 130 @app.route("/textandcommentaries/<text_id>") |
131 def get_textandcommentaries(text_id): | |
132 query = ("match (t:TEXT {ismi_id:{text_id}})" | |
14 | 133 " optional match (t)-[:was_created_by]->(a:PERSON)" |
134 " optional match (s:TEXT)<-[:is_commentary_on]-(t)" | |
135 " optional match (s)-[:was_created_by]->(sa:PERSON)" | |
136 " optional match (t)<-[:is_commentary_on]-(c:TEXT)" | |
137 " optional match (c)-[:was_created_by]->(ca:PERSON)" | |
138 " return t,a.label,s.label,s.ismi_id,sa.label,c.label,c.ismi_id,ca.label") | |
13 | 139 print("query:%s"%query) |
14 | 140 results = gdb.query(query, returns=(Node,str,str,str,str,str,str,str), |
13 | 141 params={"text_id": int(text_id)}) |
142 | |
143 print("result:%s"%results) | |
144 text = None | |
14 | 145 author = None |
13 | 146 scs = {} |
147 cs = {} | |
14 | 148 for [t,a_label,s_label,s_id,sa_label,c_label,c_id,ca_label] in results: |
13 | 149 text = t |
14 | 150 author = a_label |
13 | 151 if s_id is not None and s_id != "None": |
14 | 152 scs[int(s_id)] = {"title": s_label, "author":sa_label} |
13 | 153 |
154 if c_id is not None and c_id != "None": | |
14 | 155 cs[int(c_id)] = {"title": c_label, "author":ca_label} |
13 | 156 |
157 print("text:%s scs:%s cs:%s"%(text, scs, cs)) | |
158 return Response(dumps({"title": text['label'], "attrs": text.properties, | |
14 | 159 "author": author, |
13 | 160 "commenting": scs, "commentaries": cs}), |
161 mimetype="application/json") | |
162 | |
6
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
163 |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
164 if __name__ == '__main__': |
aeef1fedd899
first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff
changeset
|
165 app.run(port=8080) |