-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
176 lines (149 loc) · 4.5 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from flask import Flask, request
import json
import base64
import sympy
from sympy import factor, expand, apart, expand_trig
import math
from TypstCalculatorServer import TypstCalculatorServer, VERSION
app = Flask(__name__)
server = TypstCalculatorServer()
calc = server.calculator
typst = server.typst
typst2sympy = server.sympy
exec_code = server.exec
subs, simplify, evalf, solve = server.subs, server.simplify, server.evalf, server.solve
set_variance, unset_variance, clear_variance = server.set_variance, server.unset_variance, server.clear_variance
operator, relation_op, additive_op, mp_op, postfix_op, reduce_op, func, func_mat, constant = calc.get_decorators()
var = server.var
def base64_decode(s: str):
return base64.b64decode(s).decode('utf-8')
@app.route('/')
def main():
return 'Typst Sympy Calculator Server'
@app.route('/version', methods=['GET'])
def get_version():
return {
'data': VERSION,
'error': ''
}
@app.route('/init', methods=['POST'])
def post_init():
try:
return {
'data': server.init(base64_decode(request.json['typst_file'])),
'error': ''
}
except Exception as e:
return {
'data': '',
'error': str(e)
}
@app.route('/simplify', methods=['POST'])
def post_simplify():
try:
return {
'data': typst(simplify(base64_decode(request.json['typst_math']), base64_decode(request.json['typst_file']))),
'error': ''
}
except Exception as e:
return {
'data': '',
'error': str(e)
}
@app.route('/evalf', methods=['POST'])
def post_evalf():
try:
return {
'data': typst(evalf(base64_decode(request.json['typst_math']), base64_decode(request.json['typst_file']))),
'error': ''
}
except Exception as e:
return {
'data': '',
'error': str(e)
}
@app.route('/solve', methods=['POST'])
def post_solve():
try:
return {
'data': typst(solve(base64_decode(request.json['typst_math']), base64_decode(request.json['typst_file']))),
'error': ''
}
except Exception as e:
return {
'data': '',
'error': str(e)
}
@app.route('/factor', methods=['POST'])
def post_factor():
try:
return {
'data': typst(factor(subs(base64_decode(request.json['typst_math']), base64_decode(request.json['typst_file'])))),
'error': ''
}
except Exception as e:
return {
'data': '',
'error': str(e)
}
@app.route('/expand', methods=['POST'])
def post_expand():
try:
return {
'data': typst(expand(apart(expand_trig(subs(base64_decode(request.json['typst_math']), base64_decode(request.json['typst_file'])))))),
'error': ''
}
except Exception as _:
try:
return {
'data': typst(expand(expand_trig(subs(base64_decode(request.json['typst_math']), base64_decode(request.json['typst_file']))))),
'error': ''
}
except Exception as e:
return {
'data': '',
'error': str(e)
}
@app.route('/variances', methods=['GET'])
def get_variances():
try:
result = {}
for key in var:
result[key] = typst(var[key])
return {
'data': result,
'error': ''
}
except Exception as e:
return {
'data': '',
'error': str(e)
}
@app.route('/python', methods=['POST'])
def run_python():
try:
rv = None
try:
rv = eval(base64_decode(request.json['code']))
except SyntaxError:
# replace all \t with 4 spaces
python_code = base64_decode(request.json['code']).replace('\t', ' ')
# remove leading indent from python code
lines = python_code.split('\n')
indent = len(lines[0]) - len(lines[0].lstrip())
new_python_code = ''
for line in lines:
assert set(' ' + line[:indent]) == set(' '), 'IndentationError'
new_python_code += line[indent:] + '\n'
exec(new_python_code)
return {
'data': str(rv),
'error': ''
}
except Exception as e:
return {
'data': '',
'error': str(e)
}
if __name__ == '__main__':
app.run(host='127.0.0.1', port=7396)