File size: 952 Bytes
84de4cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, jsonify
import subprocess

app = Flask(__name__)

@app.route("/run", methods=["POST"])
def run_code():
    try:
        # Ensure request contains JSON data
        if not request.is_json:
            return jsonify({"output": "Invalid request. Expecting JSON format."}), 400

        data = request.get_json()
        code = data.get("code", "")

        if not code.strip():
            return jsonify({"output": "No code provided!"}), 400

        # Run Python code in a subprocess
        process = subprocess.run(
            ["python", "-c", code],
            capture_output=True,
            text=True
        )

        output = process.stdout.strip() if process.stdout else process.stderr.strip()

        return jsonify({"output": output})

    except Exception as e:
        return jsonify({"output": f"Error: {str(e)}"}), 500


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)