from flask import Flask, request, redirect, url_for, make_response, render_template, Markup, send_from_directory, send_file from flask_redis import FlaskRedis import json, abp, markdown from pprint import pprint app = Flask(__name__) redis = FlaskRedis(app) @app.route("/") def index(): return render_template("index.html") @app.route("/graph", methods=["GET", "POST"]) def graph(): if request.method == 'POST': # Convert the data to a graph state g = abp.GraphState() g.from_json(json.loads(request.data)) # Convert it back to JSON just for fun data = json.dumps(g.to_json(stringify=True)) # Insert into the database redis.execute_command("SET", "graph", data) # Return success return "Posted {} bytes OK".format(len(data)) else: # Get from the database return redis.get("graph") @app.route("/edit", methods=["POST"]) def edit(): # Load the graph from the database g = abp.GraphState() g.from_json(json.loads(redis.get("graph"))) # Apply the edit to the graph edit = json.loads(request.data) action = edit["action"] pprint(edit, indent=2) if action == "create": g.add_qubit(edit["name"], position=edit["position"], vop=0) elif action == "cz": g.act_cz(edit["start"], edit["end"]) elif action == "hadamard": g.act_hadamard(edit["node"]) elif action == "phase": g.act_local_rotation(edit["node"], "phase") elif action == "delete": g._del_node(edit["node"]) elif action == "localcomplementation": g.local_complementation(edit["node"]) elif action == "measure": g.measure(edit["node"], "p"+edit["basis"]) else: pass # New state into JSON and database data = json.dumps(g.to_json(stringify=True)) redis.execute_command("SET", "graph", data) return data @app.route("/doc") def doc(): body = Markup(markdown.markdown(render_template("doc.md"), extensions=["markdown.extensions.codehilite"])) return render_template("boilerplate.html", body=body)