|
from flask import Flask, request, jsonify, render_template |
|
import io |
|
import sys |
|
|
|
app = Flask(__name__) |
|
|
|
@app.route("/") |
|
def index(): |
|
|
|
return render_template("index.html") |
|
|
|
@app.route("/execute", methods=["POST"]) |
|
def execute_code(): |
|
|
|
code = request.json.get("code", "") |
|
|
|
|
|
output = io.StringIO() |
|
sys.stdout = output |
|
sys.stderr = output |
|
|
|
try: |
|
|
|
exec(code) |
|
result = output.getvalue() |
|
except Exception as e: |
|
result = f"Error: {e}" |
|
finally: |
|
|
|
sys.stdout = sys.__stdout__ |
|
sys.stderr = sys.__stderr__ |
|
|
|
return jsonify({"result": result}) |
|
|
|
if __name__ == "__main__": |
|
|
|
app.run(host="0.0.0.0", port=7860) |
|
|