diff ismi-python-neo4jrestclient/ismi-vis.py @ 16:ad3eefa2cb80

pimped commentaries_authors.html visualisation.
author casties
date Tue, 06 Oct 2015 19:26:46 +0200
parents b33f35b57b93
children 09c0a9ceb778
line wrap: on
line diff
--- a/ismi-python-neo4jrestclient/ismi-vis.py	Tue Oct 06 11:01:15 2015 +0200
+++ b/ismi-python-neo4jrestclient/ismi-vis.py	Tue Oct 06 19:26:46 2015 +0200
@@ -1,7 +1,7 @@
 #!/usr/bin/env python
 from json import dumps
 
-from flask import Flask, Response, request
+from flask import Flask, Response, request, send_from_directory
 
 from neo4jrestclient.client import GraphDatabase, Node, Relationship
 
@@ -20,10 +20,59 @@
 
 @app.route("/commentaries_authors.html")
 def get_commentaries_authors():
-    return app.send_static_file('commentaries_authors.html')
+    return app.send_static_file('commentaries_authors2.html')
+
+@app.route('/static/<filename>')
+def get_file(filename):
+    return send_from_directory('static', filename)
 
 @app.route("/graph")
-def get_graph():
+def get_graph_commentaries():
+    query = ("match (t1:TEXT)-[r:is_commentary_on]->(t2:TEXT)"
+             " return t1,t2"
+             " limit {limit}")
+    results = gdb.query(query, returns=(Node,Node),
+                        params={"limit": int(request.args.get("limit", 100))})
+
+    n4j_nodes = {}
+    node_idx = {}
+    nodes = []
+    rels = []
+    i = 0
+    for text1, text2 in results:
+        # source text
+        id1 = text1['ismi_id']
+        if id1 not in n4j_nodes:
+            n4j_nodes[id1] = text1
+            nodes.append({"title": "%s [%s]"%(text1['label'],id1), "label": "TEXT", "ismi_id": id1})
+            node_idx[id1] = i
+            source = i
+            i += 1
+
+        else:
+            source = node_idx[id1]
+
+        # target text
+        id2 = text2['ismi_id']
+        if id2 not in n4j_nodes:
+            n4j_nodes[id2] = text2
+            nodes.append({"title": "%s [%s]"%(text2['label'],id2), "label": "TEXT", "ismi_id": id2})
+            node_idx[id2] = i
+            target = i
+            i += 1
+
+        else:
+            target = node_idx[id2]
+
+        # relation is_commentary_on
+        rels.append({"source": source, "target": target})
+
+    return Response(dumps({"nodes": nodes, "links": rels}),
+                    mimetype="application/json")
+
+
+@app.route("/graph_commentaries_authors")
+def get_graph_commentaries_authors():
     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")