Mercurial > hg > NetworkVis
diff ismi-python-neo4jrestclient/ismi-vis.py @ 13:394bd5cfc508
new commentaries_authors.html visualisation.
author | casties |
---|---|
date | Mon, 05 Oct 2015 18:56:19 +0200 |
parents | 0f4846255b20 |
children | b33f35b57b93 |
line wrap: on
line diff
--- a/ismi-python-neo4jrestclient/ismi-vis.py Fri Oct 02 01:08:46 2015 -0400 +++ b/ismi-python-neo4jrestclient/ismi-vis.py Mon Oct 05 18:56:19 2015 +0200 @@ -6,33 +6,39 @@ from neo4jrestclient.client import GraphDatabase, Node, Relationship app = Flask(__name__, static_url_path='/static/') +app.debug = True gdb = GraphDatabase("http://localhost:7474", username="neo4j", password="neo5j") @app.route("/") def get_index(): - return app.send_static_file('index.html') + return app.send_static_file('commentaries.html') @app.route("/commentaries.html") def get_commentaries(): return app.send_static_file('commentaries.html') +@app.route("/commentaries_authors.html") +def get_commentaries_authors(): + return app.send_static_file('commentaries_authors.html') @app.route("/graph") def get_graph(): - query = ("match (t1:TEXT)-[r:is_commentary_on]->(t2:TEXT) return t1,r,t2") - results = gdb.query(query, returns=(Node,Relationship,Node)) + query = ("match (a1:PERSON)<-[:was_created_by]-(t1:TEXT)-[r:is_commentary_on]->(t2:TEXT)-[:was_created_by]->(a2:PERSON)" + " return a1,t1,t2,a2" + " limit 100") + results = gdb.query(query, returns=(Node,Node,Node,Node)) n4j_nodes = {} node_idx = {} nodes = [] rels = [] i = 0 - for node1, rel, node2 in results: - # source node - id1 = node1['ismi_id'] + for author1, text1, text2, author2 in results: + # source text + id1 = text1['ismi_id'] if id1 not in n4j_nodes: - n4j_nodes[id1] = node1 - nodes.append({"title": node1['label'], "label": "TEXT"}) + n4j_nodes[id1] = text1 + nodes.append({"title": "%s [%s]"%(text1['label'],id1), "label": "TEXT"}) node_idx[id1] = i source = i i += 1 @@ -40,11 +46,11 @@ else: source = node_idx[id1] - # target node - id2 = node2['ismi_id'] + # target text + id2 = text2['ismi_id'] if id2 not in n4j_nodes: - n4j_nodes[id2] = node2 - nodes.append({"title": node2['label'], "label": "TEXT"}) + n4j_nodes[id2] = text2 + nodes.append({"title": "%s [%s]"%(text2['label'],id2), "label": "TEXT"}) node_idx[id2] = i target = i i += 1 @@ -52,9 +58,39 @@ else: target = node_idx[id2] - # relation + # relation is_commentary_on rels.append({"source": source, "target": target}) + # source author + id3 = author1['ismi_id'] + if id3 not in n4j_nodes: + n4j_nodes[id3] = author1 + nodes.append({"title": "%s [%s]"%(author1['label'],id3), "label": "PERSON"}) + node_idx[id3] = i + s_author = i + i += 1 + + else: + s_author = node_idx[id3] + + # relation was_created_by + rels.append({"source": source, "target": s_author}) + + # target author + id4 = author1['ismi_id'] + if id4 not in n4j_nodes: + n4j_nodes[id4] = author2 + nodes.append({"title": "%s [%s]"%(author2['label'],id4), "label": "PERSON"}) + node_idx[id4] = i + t_author = i + i += 1 + + else: + t_author = node_idx[id4] + + # relation was_created_by + rels.append({"source": source, "target": t_author}) + return Response(dumps({"nodes": nodes, "links": rels}), mimetype="application/json") @@ -66,15 +102,17 @@ except KeyError: return [] else: - query = ("MATCH (text:TEXT) " - "WHERE text.label =~ {title} " - "RETURN text") + query = ("MATCH (t:TEXT)-[:was_created_by]->(p:PERSON) " + "WHERE p.ismi_id = {id} " + "RETURN t,p") results = gdb.query( query, - returns=Node, - params={"title": "(?i).*" + q + ".*"} + returns=(Node,Node), + params={"id": int(q)} ) - return Response(dumps([{"text": row.properties} for [row] in results]), + # {"name": "(?i).*" + q + ".*"} + print("search for %s returned %s results"%(repr(q),len(results))) + return Response(dumps([{"text": text.properties, "author": author.properties} for [text,author] in results]), mimetype="application/json") @@ -89,6 +127,33 @@ return Response(dumps({"title": node['label'], "attrs": node.properties}), mimetype="application/json") +@app.route("/textandcommentaries/<text_id>") +def get_textandcommentaries(text_id): + query = ("match (t:TEXT {ismi_id:{text_id}})" + " optional match (s:TEXT)<-[sr:is_commentary_on]-(t)" + " optional match (t)<-[cr:is_commentary_on]-(c:TEXT)" + " return t,s.label, s.ismi_id,c.label,c.ismi_id") + print("query:%s"%query) + results = gdb.query(query, returns=(Node,str,str,str,str), + params={"text_id": int(text_id)}) + + print("result:%s"%results) + text = None + scs = {} + cs = {} + for [t,s_label,s_id,c_label,c_id] in results: + text = t + if s_id is not None and s_id != "None": + scs[int(s_id)] = s_label + + if c_id is not None and c_id != "None": + cs[int(c_id)] = c_label + + print("text:%s scs:%s cs:%s"%(text, scs, cs)) + return Response(dumps({"title": text['label'], "attrs": text.properties, + "commenting": scs, "commentaries": cs}), + mimetype="application/json") + if __name__ == '__main__': app.run(port=8080)