-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathapp.py
80 lines (60 loc) · 2.07 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import glob
import io
import os
import uuid
import numpy as np
from flask import Flask, jsonify, make_response, render_template, request
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
app = Flask(__name__)
app.secret_key = "s3cr3t"
app.debug = False
app._static_folder = os.path.abspath("templates/static/")
@app.route("/", methods=["GET"])
def index():
title = "Create the input image"
return render_template("layouts/index.html", title=title)
@app.route("/results/", methods=["GET"])
def results():
title = "Results"
datalist = []
for csv in glob.iglob("images/*.csv"):
datalist.append(get_file_content(csv))
return render_template("layouts/results.html", title=title, datalist=datalist)
@app.route("/results/<unique_id>", methods=["GET"])
def result_for_uuid(unique_id):
title = "Result"
data = get_file_content(get_file_name(unique_id))
return render_template("layouts/result.html", title=title, data=data)
@app.route("/postmethod", methods=["POST"])
def post_javascript_data():
jsdata = request.form["canvas_data"]
unique_id = create_csv(jsdata)
params = {"unique_id": unique_id}
return jsonify(params)
@app.route("/plot/<imgdata>")
def plot(imgdata):
data = [float(i) for i in imgdata.strip("[]").split(",")]
data = np.reshape(data, (200, 200))
fig = Figure()
axis = fig.add_subplot(1, 1, 1)
axis.axis("off")
axis.imshow(data, interpolation="nearest")
canvas = FigureCanvas(fig)
output = io.BytesIO()
canvas.print_png(output)
response = make_response(output.getvalue())
response.mimetype = "image/png"
return response
def create_csv(text):
unique_id = str(uuid.uuid4())
with open(get_file_name(unique_id), "a") as file:
file.write(text[1:-1] + "\n")
return unique_id
def get_file_name(unique_id):
return f"images/{unique_id}.csv"
def get_file_content(filename):
with open(filename, "r") as file:
return file.read()
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)