MLDeveloper commited on
Commit
84de4cf
·
verified ·
1 Parent(s): 9b2c4f1

Update backend.py

Browse files
Files changed (1) hide show
  1. backend.py +35 -29
backend.py CHANGED
@@ -1,29 +1,35 @@
1
- from flask import Flask, request, jsonify
2
- import subprocess
3
-
4
- app = Flask(__name__)
5
-
6
- @app.route("/run", methods=["POST"])
7
- def run_code():
8
- try:
9
- code = request.json.get("code", "")
10
-
11
- if not code:
12
- return jsonify({"output": "No code provided!"}), 400
13
-
14
- # Run Python code in a subprocess
15
- process = subprocess.run(
16
- ["python", "-c", code],
17
- capture_output=True,
18
- text=True
19
- )
20
-
21
- output = process.stdout if process.stdout else process.stderr
22
- return jsonify({"output": output})
23
-
24
- except Exception as e:
25
- return jsonify({"output": f"Error: {str(e)}"}), 500
26
-
27
-
28
- if __name__ == "__main__":
29
- app.run(debug=True, port=5000)
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import subprocess
3
+
4
+ app = Flask(__name__)
5
+
6
+ @app.route("/run", methods=["POST"])
7
+ def run_code():
8
+ try:
9
+ # Ensure request contains JSON data
10
+ if not request.is_json:
11
+ return jsonify({"output": "Invalid request. Expecting JSON format."}), 400
12
+
13
+ data = request.get_json()
14
+ code = data.get("code", "")
15
+
16
+ if not code.strip():
17
+ return jsonify({"output": "No code provided!"}), 400
18
+
19
+ # Run Python code in a subprocess
20
+ process = subprocess.run(
21
+ ["python", "-c", code],
22
+ capture_output=True,
23
+ text=True
24
+ )
25
+
26
+ output = process.stdout.strip() if process.stdout else process.stderr.strip()
27
+
28
+ return jsonify({"output": output})
29
+
30
+ except Exception as e:
31
+ return jsonify({"output": f"Error: {str(e)}"}), 500
32
+
33
+
34
+ if __name__ == "__main__":
35
+ app.run(host="0.0.0.0", port=5000, debug=True)