Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,87 @@
|
|
1 |
-
from flask import Flask, request, jsonify, render_template
|
2 |
import os
|
3 |
import subprocess
|
4 |
import tempfile
|
5 |
import shutil
|
|
|
6 |
|
7 |
app = Flask(__name__)
|
8 |
-
BASE_DIR = tempfile.mkdtemp() # Temporary directory for executing user scripts
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def index():
|
21 |
-
"
|
22 |
-
return render_template('index.html')
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
@app.route('/run', methods=['POST'])
|
26 |
-
def run_command():
|
27 |
-
"""Executes Python scripts or commands."""
|
28 |
-
user_input = request.json.get('code', '')
|
29 |
-
response = {"output": "", "error": ""}
|
30 |
-
|
31 |
-
# Save the user's Python script to a temporary file
|
32 |
-
temp_file_path = os.path.join(BASE_DIR, "user_script.py")
|
33 |
-
with open(temp_file_path, 'w') as script_file:
|
34 |
-
script_file.write(user_input)
|
35 |
-
|
36 |
-
# Execute the script and capture output
|
37 |
try:
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
except Exception as e:
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
@app.
|
65 |
-
def
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
@app.teardown_appcontext
|
72 |
-
def teardown(exception):
|
73 |
-
"""Clean up after the app shuts down."""
|
74 |
-
cleanup_temp_dir()
|
75 |
-
|
76 |
-
|
77 |
-
if __name__ == '__main__':
|
78 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
1 |
+
from flask import Flask, request, jsonify, render_template, send_from_directory
|
2 |
import os
|
3 |
import subprocess
|
4 |
import tempfile
|
5 |
import shutil
|
6 |
+
import sys
|
7 |
|
8 |
app = Flask(__name__)
|
|
|
9 |
|
10 |
+
# Create a temporary directory for operations
|
11 |
+
temp_dir = tempfile.mkdtemp()
|
12 |
+
current_dir = temp_dir
|
13 |
+
|
14 |
+
def execute_command(command, cwd=None):
|
15 |
+
"""Executes a command and returns the output."""
|
16 |
+
process = subprocess.Popen(
|
17 |
+
command,
|
18 |
+
shell=True,
|
19 |
+
stdout=subprocess.PIPE,
|
20 |
+
stderr=subprocess.PIPE,
|
21 |
+
text=True,
|
22 |
+
cwd=cwd or current_dir
|
23 |
+
)
|
24 |
+
stdout, stderr = process.communicate()
|
25 |
+
return stdout + stderr
|
26 |
+
|
27 |
+
@app.route("/")
|
28 |
def index():
|
29 |
+
return render_template("index.html")
|
|
|
30 |
|
31 |
+
@app.route("/execute", methods=["POST"])
|
32 |
+
def execute_code():
|
33 |
+
global current_dir
|
34 |
+
command = request.json.get("code", "").strip()
|
35 |
+
if not command:
|
36 |
+
return jsonify({"result": "Error: No command provided."})
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
try:
|
39 |
+
if command.startswith("cd "):
|
40 |
+
# Change directory
|
41 |
+
new_dir = os.path.join(current_dir, command[3:])
|
42 |
+
if os.path.isdir(new_dir):
|
43 |
+
current_dir = os.path.abspath(new_dir)
|
44 |
+
return jsonify({"result": f"Changed directory to: {current_dir}"})
|
45 |
+
else:
|
46 |
+
return jsonify({"result": f"Error: Directory not found: {new_dir}"})
|
47 |
+
elif command.startswith("!"):
|
48 |
+
# Execute shell command
|
49 |
+
result = execute_command(command[1:])
|
50 |
+
elif command.startswith("pip install"):
|
51 |
+
# Install Python package
|
52 |
+
result = execute_command(f"{sys.executable} -m {command}")
|
53 |
+
elif command.startswith("git "):
|
54 |
+
# Execute git command
|
55 |
+
result = execute_command(command)
|
56 |
+
else:
|
57 |
+
# Execute Python code
|
58 |
+
if command.endswith(".py"):
|
59 |
+
# Run Python script
|
60 |
+
result = execute_command(f"{sys.executable} {command}")
|
61 |
+
else:
|
62 |
+
# Execute Python code
|
63 |
+
result = execute_command(f"{sys.executable} -c \"{command}\"")
|
64 |
+
return jsonify({"result": result})
|
65 |
except Exception as e:
|
66 |
+
return jsonify({"result": f"Error: {str(e)}"})
|
67 |
+
|
68 |
+
@app.route("/cleanup", methods=["POST"])
|
69 |
+
def cleanup():
|
70 |
+
global temp_dir, current_dir
|
71 |
+
if os.path.exists(temp_dir):
|
72 |
+
shutil.rmtree(temp_dir)
|
73 |
+
temp_dir = tempfile.mkdtemp()
|
74 |
+
current_dir = temp_dir
|
75 |
+
return jsonify({"result": "Temporary files cleaned up."})
|
76 |
+
|
77 |
+
@app.route("/list_files", methods=["GET"])
|
78 |
+
def list_files():
|
79 |
+
files = os.listdir(current_dir)
|
80 |
+
return jsonify({"files": files})
|
81 |
+
|
82 |
+
@app.route("/download/<path:filename>", methods=["GET"])
|
83 |
+
def download_file(filename):
|
84 |
+
return send_from_directory(current_dir, filename, as_attachment=True)
|
85 |
+
|
86 |
+
if __name__ == "__main__":
|
87 |
+
app.run(host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|