-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.py
135 lines (116 loc) · 3.14 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
from flask import Flask, request
import json
import latex2sympy2
import sympy
from sympy.abc import *
from sympy import *
from latex2sympy2 import latex2latex, latex2sympy, var, variances, set_variances, set_real, latex
app = Flask(__name__)
is_real = False
@app.route('/')
def main():
return 'Latex Sympy Calculator Server'
@app.route('/latex', methods=['POST'])
def get_latex():
try:
return {
'data': latex2latex(request.json['data']),
'error': ''
}
except Exception as e:
return {
'data': '',
'error': str(e)
}
@app.route('/matrix-raw-echelon-form', methods=['POST'])
def get_matrix_raw_echelon_form():
try:
return {
'data': latex(latex2sympy(request.json['data']).subs(variances).rref()[0]),
'error': ''
}
except Exception as e:
return {
'data': '',
'error': str(e)
}
@app.route('/numerical', methods=['POST'])
def get_numerical():
try:
return {
'data': latex(simplify(latex2sympy(request.json['data']).subs(variances).doit().doit()).evalf(subs=variances)),
'error': ''
}
except Exception as e:
return {
'data': '',
'error': str(e)
}
@app.route('/factor', methods=['POST'])
def get_factor():
try:
return {
'data': latex(factor(latex2sympy(request.json['data']).subs(variances))),
'error': ''
}
except Exception as e:
return {
'data': '',
'error': str(e)
}
@app.route('/expand', methods=['POST'])
def get_expand():
try:
return {
'data': latex(expand(apart(expand_trig(latex2sympy(request.json['data']).subs(variances))))),
'error': ''
}
except Exception as _:
try:
return {
'data': latex(expand(expand_trig(latex2sympy(request.json['data']).subs(variances)))),
'error': ''
}
except Exception as e:
return {
'data': '',
'error': str(e)
}
@app.route('/variances', methods=['GET'])
def get_variances():
result = {}
for key in var:
result[key] = str(var[key])
return json.dumps(result)
@app.route('/reset', methods=['GET'])
def reset():
set_variances({})
global var
var = latex2sympy2.var
return {
'success' : True
}
@app.route('/complex', methods=['GET'])
def complex():
global is_real
is_real = not is_real
set_real(True if is_real else None)
return {
'success' : True,
'value' : is_real
}
@app.route('/python', methods=['POST'])
def run_python():
try:
rv = eval(request.json['data'])
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=7395)