|
@@ -9,25 +9,29 @@ from websocket import create_connection |
|
|
from graphstate import GraphState |
|
|
from graphstate import GraphState |
|
|
import json |
|
|
import json |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class VisibleGraphState(GraphState): |
|
|
class VisibleGraphState(GraphState): |
|
|
|
|
|
|
|
|
|
|
|
""" Overloads the graph state with methods for sending to the browser over a websocket """ |
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs): |
|
|
def __init__(self, *args, **kwargs): |
|
|
|
|
|
""" Constructor """ |
|
|
GraphState.__init__(self, *args, **kwargs) |
|
|
GraphState.__init__(self, *args, **kwargs) |
|
|
self.ws = create_connection("ws://localhost:5001") |
|
|
|
|
|
|
|
|
self.ws = create_connection("ws://localhost:5000") |
|
|
atexit.register(self.shutdown) |
|
|
atexit.register(self.shutdown) |
|
|
#self.ws.send(json.dumps({"method":"clear"})) |
|
|
|
|
|
|
|
|
|
|
|
def shutdown(self): |
|
|
def shutdown(self): |
|
|
|
|
|
""" The client should shut down automatically on close """ |
|
|
self.update() |
|
|
self.update() |
|
|
self.ws.close() |
|
|
self.ws.close() |
|
|
|
|
|
|
|
|
def to_json(self): |
|
|
def to_json(self): |
|
|
ngbh = {a: {b : True for b in self.ngbh[a]} |
|
|
|
|
|
|
|
|
""" We override to_json() so that we send the whole `ngbh` structure in JS-friendly form """ |
|
|
|
|
|
ngbh = {a: {b: True for b in self.ngbh[a]} |
|
|
for a in self.ngbh} |
|
|
for a in self.ngbh} |
|
|
return {"vops": self.vops, "ngbh": ngbh, "meta": self.meta} |
|
|
return {"vops": self.vops, "ngbh": ngbh, "meta": self.meta} |
|
|
|
|
|
|
|
|
def update(self): |
|
|
def update(self): |
|
|
|
|
|
""" Call this function when you are ready to send data to the browser """ |
|
|
data = json.dumps(self.to_json()) |
|
|
data = json.dumps(self.to_json()) |
|
|
self.ws.send(data) |
|
|
self.ws.send(data) |
|
|
|
|
|
|
|
|
|
|
|
|