-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (63 loc) · 1.84 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
from flask import Flask, request, render_template, jsonify, make_response
from .modules.control import toLean
from html import escape
from .interpreter.interpret import interpret
import requests
import json
import random
from cairosvg import svg2png
with open("./data/credentials.txt") as f:
creds = f.read().splitlines()
ID = creds[0]
KEY = creds[1]
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route('/create', methods=['POST'])
def Process():
req = request.get_json()
khl = req['khl']
lean = toLean(khl)
inter = interpret(lean)
repl = inter[0]
errors = inter[1]
sorries=inter[2]
res = make_response(jsonify({"lean": lean, "repl": repl, "errors": errors, "sorries": sorries}))
return res
@app.route("/tablet")
def tablet():
return render_template("tablet.html")
@app.route("/save/", methods=['POST'])
def binary_saver():
svg_code = request.form['image']
svg2png(bytestring=svg_code,write_to='./data/out.png')
r = requests.post("https://api.mathpix.com/v3/text",
files={"file": open("./data/out.png","rb")},
data={
"options_json": json.dumps({
"math_inline_delimiters": ["$", "$"],
"rm_spaces": True
})
},
headers={
"app_id": ID,
"app_key": KEY
}
)
with open("./data/req.json", "w") as f:
json.dump(r.json(), f)
return jsonify({})
@app.route("/ocr", methods=['POST'])
def read_tablet():
with open("./data/req.json", "r") as f:
r = json.load(f)
with open("./data/old_req.json", "r") as f:
rold = json.load(f)
if r==rold:
return make_response(jsonify({}))
else:
with open("./data/old_req.json", "w") as f:
json.dump(r, f)
res = make_response(jsonify(r))
return res