Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.1KB

  1. """
  2. Allows us to visualize the state in a browser
  3. """
  4. import atexit, json
  5. from graphstate import GraphState
  6. from websocket import create_connection
  7. class VisibleGraphState(GraphState):
  8. """ Overloads the graph state with methods for sending to the browser over a websocket """
  9. def __init__(self, *args, **kwargs):
  10. """ Constructor """
  11. GraphState.__init__(self, *args, **kwargs)
  12. self.ws = create_connection("ws://localhost:5000")
  13. atexit.register(self.shutdown)
  14. def shutdown(self):
  15. """ The client should shut down automatically on close """
  16. self.update()
  17. self.ws.close()
  18. def to_json(self):
  19. """ We override to_json() so that we send the whole `ngbh` structure in JS-friendly form """
  20. ngbh = {a: {b: True for b in self.ngbh[a]}
  21. for a in self.ngbh}
  22. return {"vops": self.vops, "ngbh": ngbh, "meta": self.meta}
  23. def update(self):
  24. """ Call this function when you are ready to send data to the browser """
  25. data = json.dumps(self.to_json())
  26. self.ws.send(data)