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 1.6KB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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()
  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. return jsonify(cache.get("state").to_json())
  17. elif request.method == "POST":
  18. graphstate = abp.GraphState()
  19. graphstate.from_json(json.loads(request.data))
  20. cache.set("state", graphstate)
  21. return jsonify({"update": "ok"})
  22. @app.route("/add_node/<int:node>")
  23. def add_node(node):
  24. """ Add a node to the graph """
  25. graphstate = cache.get("state")
  26. graphstate.add_node(node)
  27. cache.set("state", graphstate)
  28. return jsonify({"add_node": "okay"})
  29. @app.route("/act_local_rotation/<int:node>/<int:operation>")
  30. def act_local_rotation(node):
  31. """ Add a node to the graph """
  32. # TODO: try to lookup the operation first
  33. graphstate.act_local_rotation(node, operation)
  34. return jsonify({"act_local_rotation": "okay"})
  35. @app.route("/act_cz/<int:a>/<int:b>")
  36. def act_cz(a, b):
  37. """ Add a node to the graph """
  38. graphstate.act_cz(a, b)
  39. return jsonify({"act_cz": "okay"})
  40. @app.route("/clear")
  41. def clear():
  42. """ Clear the current state """
  43. graphstate = abp.GraphState()
  44. cache.set("state", graphstate)
  45. return jsonify({"clear": "okay"})
  46. if __name__ == "__main__":
  47. app.debug = False
  48. app.run(host="0.0.0.0")