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.

36 lines
1.1KB

  1. import time, atexit, json
  2. import networkx as nx
  3. import numpy as np
  4. import websocket
  5. from socket import error as socket_error
  6. from . import clifford
  7. from . import util
  8. from . import nxgraphstate
  9. class VizClient(object):
  10. def __init__(self, uri = "ws://localhost:5000"):
  11. self.ws = websocket.create_connection(uri, timeout=0.1)
  12. atexit.register(self.shutdown)
  13. def shutdown(self):
  14. """ Close the connection to the websocket """
  15. self.ws.close()
  16. def update(self, graph, delay = 0.5):
  17. """ Call this function when you are ready to send data to the browser """
  18. g = nxgraphstate.NXGraphState(graph)
  19. # Automatically perform layout if position is not provided
  20. if not all(("position" in node) for node in list(g.node.values())):
  21. g.layout()
  22. # Send data to browser and rate-limit
  23. try:
  24. self.ws.send(json.dumps(g.to_json(stringify=True)))
  25. self.ws.recv()
  26. except websocket._exceptions.WebSocketTimeoutException:
  27. print("Timed out ... you might be pushing a bit hard")
  28. time.sleep(delay)