diff --git a/abp/__init__.py b/abp/__init__.py index 48827db..93593a4 100644 --- a/abp/__init__.py +++ b/abp/__init__.py @@ -1,6 +1,7 @@ # Alias some stuff to make imports cleaner from abp.graphstate import GraphState from abp.nxgraphstate import NXGraphState +from abp.vizclient import VizClient from abp.qi import CircuitModel DETERMINISTIC = False diff --git a/abp/nxgraphstate.py b/abp/nxgraphstate.py index 253fd30..2c8d957 100644 --- a/abp/nxgraphstate.py +++ b/abp/nxgraphstate.py @@ -1,6 +1,8 @@ import networkx as nx +import numpy as np import graphstate import clifford +import util class NXGraphState(graphstate.GraphState, nx.Graph): """ This is GraphState with NetworkX-like abilities """ @@ -15,10 +17,3 @@ class NXGraphState(graphstate.GraphState, nx.Graph): for key, (x, y, z) in pos.items(): self.node[key]["position"] = util.xyz(x, y, z) - def add_vops(self): - """ Automatically add vops if they're not present """ - for key in self.node: - if not "vop" in self.node[key]: - self.node[key]["vop"] = clifford.identity - - diff --git a/abp/vizclient.py b/abp/vizclient.py new file mode 100644 index 0000000..0a89c11 --- /dev/null +++ b/abp/vizclient.py @@ -0,0 +1,35 @@ +import time, atexit, json +import networkx as nx +import numpy as np +import websocket +from socket import error as socket_error +import clifford +import util +import nxgraphstate + +class VizClient(object): + def __init__(self, uri = "ws://localhost:5000"): + self.ws = websocket.create_connection(uri, timeout=0.1) + atexit.register(self.shutdown) + + def shutdown(self): + """ Close the connection to the websocket """ + self.ws.close() + + def update(self, graph, delay = 0.5): + """ Call this function when you are ready to send data to the browser """ + g = nxgraphstate.NXGraphState(graph) + + # Automatically perform layout if position is not provided + if not all(("position" in node) for node in g.node.values()): + g.layout() + + # Send data to browser and rate-limit + try: + self.ws.send(json.dumps(g.to_json(stringify=True))) + self.ws.recv() + except websocket._exceptions.WebSocketTimeoutException: + print "Timed out ... you might be pushing a bit hard" + time.sleep(delay) + + diff --git a/examples/visualization/new.py b/examples/visualization/new.py new file mode 100644 index 0000000..5e4fa72 --- /dev/null +++ b/examples/visualization/new.py @@ -0,0 +1,8 @@ +from abp import GraphState, VizClient +from abp.util import xyz + +v = VizClient() +g = GraphState(5) +for i in range(5): + g.node[i]["position"] = xyz(i, 0, 0) +v.update(g)