| @@ -1,6 +1,7 @@ | |||||
| # Alias some stuff to make imports cleaner | # Alias some stuff to make imports cleaner | ||||
| from abp.graphstate import GraphState | from abp.graphstate import GraphState | ||||
| from abp.nxgraphstate import NXGraphState | from abp.nxgraphstate import NXGraphState | ||||
| from abp.vizclient import VizClient | |||||
| from abp.qi import CircuitModel | from abp.qi import CircuitModel | ||||
| DETERMINISTIC = False | DETERMINISTIC = False | ||||
| @@ -1,6 +1,8 @@ | |||||
| import networkx as nx | import networkx as nx | ||||
| import numpy as np | |||||
| import graphstate | import graphstate | ||||
| import clifford | import clifford | ||||
| import util | |||||
| class NXGraphState(graphstate.GraphState, nx.Graph): | class NXGraphState(graphstate.GraphState, nx.Graph): | ||||
| """ This is GraphState with NetworkX-like abilities """ | """ 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(): | for key, (x, y, z) in pos.items(): | ||||
| self.node[key]["position"] = util.xyz(x, y, z) | 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 | |||||
| @@ -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) | |||||
| @@ -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) | |||||