Commits

Michele Orrù committed f4665d1

Adding js graph - needs review.

  • Participants
  • Parent commits b0eaf5d

Comments (0)

Files changed (1)

         # override Edge._weight
         self._weight = list()
 
-    def __repr__(self):
-        """
-        Print the current node in a pretty way. Useful for debugging and testing
-        :)
-        """
-        return '<graph.Edge object FOOOOO XXX FOOOO>'
-
     def get_weight(self):
         """
-        Return the mean of the list self._weights.
+        Return the mean of the list self._weights if it contains any element, 0
+        otherwise.
         """
-        return sum(self._weight) / len(self._weight)
+        if self._weight:
+            return sum(self._weight) / len(self._weight)
+        else:
+            logging.debug('Edge %s contains no weight!')
+            return 0
 
     def set_weight(self, value):
         """
-        Append BLABLABLA
+        Append a new weight to self._weight
         """
         self._weight.append(value)
 
     weight = property(get_weight, set_weight)
 
 
-class Graph(graph.Graph):
-
-    def add_edge(self, id1, id2, *args, **kwargs):
-         """
-         Appends a new Edge to the graph, slightly modifying pattern's
-         implementation to allow multiple linking.
-         """
-         # Create nodes that are not yet part of the graph.
-         n1 = self.add_node(id1)
-         n2 = self.add_node(id2)
-         e2 = kwargs.pop("base", graph.Edge)
-         e2 = e2(n1, n2, *args, **kwargs)
-         self.edges.append(e2)
-         # XXX: HACK *avoid* syncronization between node links:
-         # A.links.edge(B) yields edge A->B
-         # B.links.edge(A) yields edge B->A
-         n1.links.append(n2, edge=e2)
-         # Clear adjacency cache.
-         self._adjacency, self._paths = None, {}
-         return e2
-
 # --------------------------------------------------------------
 import flask
-
 app = flask.Flask(__name__)
 
-@app.route('/')
-def plot(txtcsv='foo.csv'):
+@app.route('/graph/js/graph.js')
+def foo():
+    return open('wiki-polarization/js/graph.js').read()
+
+@app.route('/graph/<txtcsv>')
+def plot(txtcsv):
     """
     Import the CSV file created with TalkProcessor, and populate a graph where:
      - each node is identified by a single person.
     The graph is supposed to show people with strong accordance more near than
     people in opposition.
     """
-    talk_graph = Graph()
+    talk_graph = graph.Graph()
 
     with open(txtcsv) as txt:
         csv_reader = csv.DictReader(txt,
             else:
                 # Add a new node using the Edge class declared above
                 talk_graph.add_edge(author, target,
-                                    weight=sentiment(msg)[0]) # , base=Edge)
+                                    weight=sentiment(msg)[0],
+                                    base=Edge, type='is-related-to')
 
-    return graph.HTMLCanvasRenderer(talk_graph).render()
+    return graph.render(talk_graph, frames=1, ipf=0)
 
 
 if __name__ == '__main__':