Artificial-superintelligence
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
import sys
|
3 |
+
import io
|
4 |
+
import contextlib
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
# Function to execute Python code safely
|
9 |
+
def execute_python_code(code):
|
10 |
+
# Capturing the output of Python execution
|
11 |
+
output = io.StringIO()
|
12 |
+
sys.stdout = output
|
13 |
+
sys.stderr = output
|
14 |
+
|
15 |
+
try:
|
16 |
+
# Executing the code
|
17 |
+
exec(code)
|
18 |
+
except Exception as e:
|
19 |
+
output.write(f"Error: {e}")
|
20 |
+
|
21 |
+
# Return the captured output
|
22 |
+
return output.getvalue()
|
23 |
+
|
24 |
+
@app.route('/')
|
25 |
+
def index():
|
26 |
+
return app.send_static_file('index.html')
|
27 |
+
|
28 |
+
@app.route('/execute', methods=['POST'])
|
29 |
+
def execute():
|
30 |
+
data = request.get_json()
|
31 |
+
code = data.get('code', '')
|
32 |
+
|
33 |
+
# Execute the Python code and capture the result
|
34 |
+
result = execute_python_code(code)
|
35 |
+
|
36 |
+
# Send the result back to the frontend
|
37 |
+
return jsonify({'result': result})
|
38 |
+
|
39 |
+
if __name__ == '__main__':
|
40 |
+
app.run(debug=True)
|