Anders and Briegel in Python
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

76 行
2.1KB

  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")