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.

server.py 2.1KB

8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
8 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from flask import Flask, request, render_template, jsonify
  2. from werkzeug.contrib.cache import SimpleCache
  3. import json
  4. import abp
  5. #TODO: only send deltas
  6. #graphstate = abp.GraphState()
  7. cache = SimpleCache(default_timeout = 10000)
  8. cache.set("state", abp.GraphState())
  9. app = Flask(__name__)
  10. @app.route("/")
  11. def index():
  12. return render_template("index.html")
  13. @app.route("/state", methods = ["GET", "POST"])
  14. def state():
  15. if request.method == "GET":
  16. state = cache.get("state")
  17. output = state.to_json()
  18. output["update_required"] = cache.get("update")
  19. cache.set("update", False)
  20. return jsonify(output)
  21. elif request.method == "POST":
  22. cache.set("update", True)
  23. graphstate = abp.GraphState()
  24. graphstate.from_json(json.loads(request.data))
  25. cache.set("state", graphstate)
  26. return jsonify({"update": "ok"})
  27. @app.route("/add_node/<int:node>")
  28. def add_node(node):
  29. """ Add a node to the graph """
  30. graphstate = cache.get("state")
  31. graphstate.add_node(node)
  32. graphstate.layout()
  33. cache.set("update", True)
  34. cache.set("state", graphstate)
  35. return jsonify({"add_node": "okay"})
  36. @app.route("/act_local_rotation/<int:node>/<int:operation>")
  37. def act_local_rotation(node, operation):
  38. """ Add a node to the graph """
  39. # TODO: try to lookup the operation first
  40. graphstate = cache.get("state")
  41. graphstate.act_local_rotation(node, operation)
  42. cache.set("update", True)
  43. cache.set("state", graphstate)
  44. return jsonify({"act_local_rotation": "okay"})
  45. @app.route("/act_cz/<int:a>/<int:b>")
  46. def act_cz(a, b):
  47. """ Add a node to the graph """
  48. graphstate = cache.get("state")
  49. graphstate.act_cz(a, b)
  50. graphstate.layout()
  51. cache.set("update", True)
  52. cache.set("state", graphstate)
  53. return jsonify({"act_cz": "okay"})
  54. @app.route("/clear")
  55. def clear():
  56. """ Clear the current state """
  57. graphstate = abp.GraphState()
  58. cache.set("update", True)
  59. cache.set("state", graphstate)
  60. return jsonify({"clear": "okay"})
  61. if __name__ == "__main__":
  62. app.debug = True
  63. app.run(host="0.0.0.0")