-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-v1.py
64 lines (54 loc) · 1.67 KB
/
api-v1.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
from flask import Flask, jsonify, request
from flask import Response
from flask_cors import CORS
import sys
from io import StringIO
import contextlib
app = Flask(__name__)
cors = CORS(app, resources={r"/tester": {"origins": "*"}})
app.config['CORS_HEADERS'] = 'Content-Type'
def execute_code(code):
@contextlib.contextmanager
def stdoutIO(stdout=None):
old = sys.stdout
if stdout is None:
stdout = StringIO()
sys.stdout = stdout
yield stdout
sys.stdout = old
with stdoutIO() as s:
try:
exec(code)
except:
print("Something wrong with the code")
#print(code)
return s.getvalue()
#Testing Route
@app.route('/', methods=['GET'])
def getDefault():
return jsonify({'response': 'Hello to my users api!'})
#Testing Route
@app.route('/tester', methods=['POST']) # can be distinguished request.method == 'POST'
def tester():
"""
Example:
{
"inputs": [0,1,2,3],
"solutions": [0,1,2,3],
"code": "ls = inputs\n output = []\n for i in ls:\n output.append(i)"
}
"""
inputs = request.json['inputs']
solutions = request.json['solutions']
code = request.json['code']
code2 = code.splitlines()
program_parsed = ""
for i in code2:
program_parsed += " " + i + "\n"
program = "def main(inputs):\n" + program_parsed + " return outputs\nprint(main(inputs="+ str(inputs) +"))"
var = execute_code(program)
print(var[:-1] == str(solutions))
return jsonify({'result': var[:-1], 'equal':var[:-1] == str(solutions)})
if __name__ == '__main__':
#print(sys.path)
app.run(debug=True, port=8000)