|
from flask import Flask, request, jsonify |
|
import sys |
|
import io |
|
import contextlib |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
def execute_python_code(code): |
|
|
|
output = io.StringIO() |
|
sys.stdout = output |
|
sys.stderr = output |
|
|
|
try: |
|
|
|
exec(code) |
|
except Exception as e: |
|
output.write(f"Error: {e}") |
|
|
|
|
|
return output.getvalue() |
|
|
|
@app.route('/') |
|
def index(): |
|
return app.send_static_file('index.html') |
|
|
|
@app.route('/execute', methods=['POST']) |
|
def execute(): |
|
data = request.get_json() |
|
code = data.get('code', '') |
|
|
|
|
|
result = execute_python_code(code) |
|
|
|
|
|
return jsonify({'result': result}) |
|
|
|
if __name__ == '__main__': |
|
|