-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
77 lines (53 loc) · 1.62 KB
/
server.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from flask import Flask
from flask import Response
from flask import render_template
from database import ImgnetDatabase
import datastore
import json
app = Flask(__name__)
db = ImgnetDatabase()
@app.route("/")
def index():
return render_template("index.html")
@app.route("/list/photos/allsub/<int:label>")
def list_photos_allsub(label):
p = db.get_photos_in_label_and_sublabels(label)
return Response(json.dumps(p), mimetype="application/json")
def build_label_tree(label):
subs = []
for l in db.get_sub_labels(label):
lbl = {}
lbl["name"] = str(l[1])
lbl["id"] = l[0]
lbl["sub"] = build_label_tree(l[0])
subs.append(lbl)
return subs
@app.route("/tree/labels/<int:label>")
def tree_labels(label):
tree = []
lbl = {}
lbl["name"] = db.get_label_name(label)
lbl["id"] = label
lbl["sub"] = build_label_tree(label)
tree.append(lbl)
return Response(json.dumps(tree), mimetype="application/json")
@app.route("/applylabel/<int:label>/<int:photo>")
def apply_label(label, photo):
db.label_photo(photo, label)
db.commit()
return ""
@app.route("/labels/photo/<int:photo>")
def get_labels_on_photo(photo):
return json.dumps(db.get_labels_on_photo(photo))
@app.route("/add/label/<int:parent>/<name>")
def add_label(parent, name):
db.create_label(name, parent)
db.commit()
return ""
@app.route("/img/<int:id_>")
def img(id_):
with open(datastore.get(id_), 'rb') as f:
resp = f.read()
return Response(resp, mimetype="image/jpeg")
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')