-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
93 lines (62 loc) · 2.72 KB
/
Copy pathmain.py
File metadata and controls
93 lines (62 loc) · 2.72 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from flask import Flask, render_template, request
import json
import os
app = Flask(__name__)
def read_protein(difficulty, num):
with open(f"./data/{difficulty}/{num}.json") as f:
data = json.load(f)
return data
def read_scores(difficulty):
score_path = f"./data/{difficulty}/scores.json"
if not os.path.isfile(score_path):
return []
with open(score_path) as f:
data = json.load(f)
return data
def write_scores(scores, difficulty):
score_path = f"./data/{difficulty}/scores.json"
with open(score_path, "w") as f:
f.writelines(json.dumps(scores))
def read_leaderboard(difficulty):
scores = read_scores(difficulty)
if len(scores) <= 1:
return scores
sorted_list = sorted(scores, key=lambda x: (x["minutes"], x["seconds"]))
return sorted_list[:3] if len(sorted_list) > 3 else sorted_list
@app.route("/")
def index():
return render_template("index.html")
@app.route("/play/<difficulty>/<int:num>")
def play(difficulty, num):
protein = read_protein(difficulty, num)
return render_template("play.html", sequence=protein["sequence"], difficulty=difficulty, num=num)
@app.route("/protein/<difficulty>/<int:num>")
def protein(difficulty, num):
protein = read_protein(difficulty, num)
structure_id = protein["structure"]
structure_provider = "pdb" if len(protein["structure"]) == 4 else "afdb"
structure = f"https://molstar.org/viewer/?{structure_provider}={structure_id}&hide-controls=1&collapse-left-panel=1&pdb-provider=rcsb"
return render_template("protein.html",
name=protein["name"],
gene=protein["gene"],
structure=structure,
function_summary=protein["function_summary"],
function_description=protein["function_description"],
function_citation=protein["function_citation"],
structure_citation=protein["structure_citation"])
@app.route("/score/<difficulty>", methods=["POST"])
def score(difficulty):
name = request.json["name"]
minutes = request.json["minutes"]
seconds = request.json["seconds"]
text = request.json["text"]
scores = read_scores(difficulty)
scores.append({"name": name, "minutes": minutes, "seconds": seconds, "text": text})
write_scores(scores, difficulty)
return ('', 204)
@app.route("/leaderboard")
def leaderboard():
easy_leaderboard = read_leaderboard("easy")
medium_leaderboard = read_leaderboard("medium")
hard_leaderboard = read_leaderboard("hard")
return render_template("leaderboard.html", easy=easy_leaderboard, medium=medium_leaderboard, hard=hard_leaderboard)