Anders and Briegel in Python
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

81 lignes
2.2KB

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