-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
62 lines (43 loc) · 1.43 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from flask import Flask, session, redirect, url_for, request
from flask import render_template
import random
import importlib
from dolo import state
import lit
import time
app = Flask(__name__)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
run_id = str(time.time())
@app.route("/")
def index():
if "id" not in session:
# first time person has opened this
session["id"] = random.randint(1, 1000000)
session["run_id"] = run_id
elif "id" in session and session["run_id"] != run_id:
# its the same person but using a different instance of the app
session["run_id"] = run_id
# destroy old state
state[session["id"]] = {}
# Session id needs to be set before reloading module
importlib.reload(lit)
return render_template("index.html")
@app.route("/refresh")
def refresh():
if "id" in session:
# initialize state dictionary
state[session["id"]] = {}
return redirect(url_for("index"))
@app.route("/event/<widget_id>", methods=["POST"])
def update_state(widget_id):
data = request.get_json()
new_state = data["widget_state"]
# If not change in state
if new_state == state[session["id"]][widget_id]:
return "", 200
# widget state has to be json serializable
state[session["id"]][widget_id] = data["widget_state"]
return "", 205
if app.debug:
app.jinja_env.auto_reload = True
app.config["TEMPLATES_AUTO_RELOAD"] = True