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.

30 lines
860B

  1. """
  2. Allows us to visualize the state in a browser
  3. """
  4. import atexit, json, time
  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 update(self, delay = 0.5):
  19. """ Call this function when you are ready to send data to the browser """
  20. data = json.dumps(self.to_json())
  21. self.ws.send(data)
  22. time.sleep(delay)