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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
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
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
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
0f4846255b20 updated for ismi-dev.
casties
parents: 7
diff changeset
17 @app.route("/commentaries.html")
0f4846255b20 updated for ismi-dev.
casties
parents: 7
diff changeset
18 def get_commentaries():
0f4846255b20 updated for ismi-dev.
casties
parents: 7
diff changeset
19 return app.send_static_file('commentaries.html')
0f4846255b20 updated for ismi-dev.
casties
parents: 7
diff changeset
20
13
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
21 @app.route("/commentaries_authors.html")
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
22 def get_commentaries_authors():
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
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
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
27 query = ("match (a1:PERSON)<-[:was_created_by]-(t1:TEXT)-[r:is_commentary_on]->(t2:TEXT)-[:was_created_by]->(a2:PERSON)"
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
28 " return a1,t1,t2,a2"
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
29 " limit 100")
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
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
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
36 for author1, text1, text2, author2 in results:
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
37 # source text
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
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
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
40 n4j_nodes[id1] = text1
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
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
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
49 # target text
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
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
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
52 n4j_nodes[id2] = text2
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
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
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
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
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
64 # source author
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
65 id3 = author1['ismi_id']
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
66 if id3 not in n4j_nodes:
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
67 n4j_nodes[id3] = author1
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
68 nodes.append({"title": "%s [%s]"%(author1['label'],id3), "label": "PERSON"})
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
69 node_idx[id3] = i
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
70 s_author = i
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
71 i += 1
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
72
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
73 else:
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
74 s_author = node_idx[id3]
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
75
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
76 # relation was_created_by
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
77 rels.append({"source": source, "target": s_author})
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
78
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
79 # target author
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
80 id4 = author1['ismi_id']
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
81 if id4 not in n4j_nodes:
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
82 n4j_nodes[id4] = author2
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
83 nodes.append({"title": "%s [%s]"%(author2['label'],id4), "label": "PERSON"})
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
84 node_idx[id4] = i
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
85 t_author = i
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
86 i += 1
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
87
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
88 else:
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
89 t_author = node_idx[id4]
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
90
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
91 # relation was_created_by
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
92 rels.append({"source": source, "target": t_author})
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
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
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
105 query = ("MATCH (t:TEXT)-[:was_created_by]->(p:PERSON) "
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
106 "WHERE p.ismi_id = {id} "
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
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
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
110 returns=(Node,Node),
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
111 params={"id": int(q)}
6
aeef1fedd899 first version of ismi-python-neo4jrestclient. doesn't work yet.
casties
parents:
diff changeset
112 )
13
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
113 # {"name": "(?i).*" + q + ".*"}
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
114 print("search for %s returned %s results"%(repr(q),len(results)))
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
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
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
130 @app.route("/textandcommentaries/<text_id>")
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
131 def get_textandcommentaries(text_id):
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
132 query = ("match (t:TEXT {ismi_id:{text_id}})"
14
b33f35b57b93 added authors to details view.
casties
parents: 13
diff changeset
133 " optional match (t)-[:was_created_by]->(a:PERSON)"
b33f35b57b93 added authors to details view.
casties
parents: 13
diff changeset
134 " optional match (s:TEXT)<-[:is_commentary_on]-(t)"
b33f35b57b93 added authors to details view.
casties
parents: 13
diff changeset
135 " optional match (s)-[:was_created_by]->(sa:PERSON)"
b33f35b57b93 added authors to details view.
casties
parents: 13
diff changeset
136 " optional match (t)<-[:is_commentary_on]-(c:TEXT)"
b33f35b57b93 added authors to details view.
casties
parents: 13
diff changeset
137 " optional match (c)-[:was_created_by]->(ca:PERSON)"
b33f35b57b93 added authors to details view.
casties
parents: 13
diff changeset
138 " return t,a.label,s.label,s.ismi_id,sa.label,c.label,c.ismi_id,ca.label")
13
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
139 print("query:%s"%query)
14
b33f35b57b93 added authors to details view.
casties
parents: 13
diff changeset
140 results = gdb.query(query, returns=(Node,str,str,str,str,str,str,str),
13
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
141 params={"text_id": int(text_id)})
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
142
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
143 print("result:%s"%results)
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
144 text = None
14
b33f35b57b93 added authors to details view.
casties
parents: 13
diff changeset
145 author = None
13
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
146 scs = {}
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
147 cs = {}
14
b33f35b57b93 added authors to details view.
casties
parents: 13
diff changeset
148 for [t,a_label,s_label,s_id,sa_label,c_label,c_id,ca_label] in results:
13
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
149 text = t
14
b33f35b57b93 added authors to details view.
casties
parents: 13
diff changeset
150 author = a_label
13
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
151 if s_id is not None and s_id != "None":
14
b33f35b57b93 added authors to details view.
casties
parents: 13
diff changeset
152 scs[int(s_id)] = {"title": s_label, "author":sa_label}
13
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
153
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
154 if c_id is not None and c_id != "None":
14
b33f35b57b93 added authors to details view.
casties
parents: 13
diff changeset
155 cs[int(c_id)] = {"title": c_label, "author":ca_label}
13
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
156
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
157 print("text:%s scs:%s cs:%s"%(text, scs, cs))
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
158 return Response(dumps({"title": text['label'], "attrs": text.properties,
14
b33f35b57b93 added authors to details view.
casties
parents: 13
diff changeset
159 "author": author,
13
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
160 "commenting": scs, "commentaries": cs}),
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
161 mimetype="application/json")
394bd5cfc508 new commentaries_authors.html visualisation.
casties
parents: 9
diff changeset
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)