Artificial-superintelligence
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -36,8 +36,25 @@ def execute_code():
|
|
36 |
return jsonify({"result": "Error: No command provided."})
|
37 |
|
38 |
try:
|
39 |
-
if command
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
@@ -45,26 +62,29 @@ def execute_code():
|
|
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
|
|
|
36 |
return jsonify({"result": "Error: No command provided."})
|
37 |
|
38 |
try:
|
39 |
+
if command == "show files":
|
40 |
+
files = os.listdir(current_dir)
|
41 |
+
return jsonify({"result": "Files in current directory:\n" + "\n".join(files)})
|
42 |
+
elif command == "hide files":
|
43 |
+
return jsonify({"result": "Files hidden."})
|
44 |
+
elif command.startswith("new file "):
|
45 |
+
filename = command[9:].strip()
|
46 |
+
filepath = os.path.join(current_dir, filename)
|
47 |
+
with open(filepath, 'w') as f:
|
48 |
+
pass # Create an empty file
|
49 |
+
return jsonify({"result": f"Created new file: {filename}"})
|
50 |
+
elif command.startswith("edit "):
|
51 |
+
filename = command[5:].strip()
|
52 |
+
filepath = os.path.join(current_dir, filename)
|
53 |
+
if os.path.exists(filepath):
|
54 |
+
return jsonify({"result": "Enter code:", "action": "edit", "filename": filename})
|
55 |
+
else:
|
56 |
+
return jsonify({"result": f"Error: File {filename} not found."})
|
57 |
+
elif command.startswith("cd "):
|
58 |
new_dir = os.path.join(current_dir, command[3:])
|
59 |
if os.path.isdir(new_dir):
|
60 |
current_dir = os.path.abspath(new_dir)
|
|
|
62 |
else:
|
63 |
return jsonify({"result": f"Error: Directory not found: {new_dir}"})
|
64 |
elif command.startswith("!"):
|
|
|
65 |
result = execute_command(command[1:])
|
66 |
elif command.startswith("pip install"):
|
|
|
67 |
result = execute_command(f"{sys.executable} -m {command}")
|
68 |
elif command.startswith("git "):
|
|
|
69 |
result = execute_command(command)
|
70 |
else:
|
|
|
71 |
if command.endswith(".py"):
|
|
|
72 |
result = execute_command(f"{sys.executable} {command}")
|
73 |
else:
|
|
|
74 |
result = execute_command(f"{sys.executable} -c \"{command}\"")
|
75 |
return jsonify({"result": result})
|
76 |
except Exception as e:
|
77 |
return jsonify({"result": f"Error: {str(e)}"})
|
78 |
|
79 |
+
@app.route("/save_file", methods=["POST"])
|
80 |
+
def save_file():
|
81 |
+
filename = request.json.get("filename")
|
82 |
+
content = request.json.get("content")
|
83 |
+
filepath = os.path.join(current_dir, filename)
|
84 |
+
with open(filepath, 'w') as f:
|
85 |
+
f.write(content)
|
86 |
+
return jsonify({"result": f"File {filename} saved successfully."})
|
87 |
+
|
88 |
@app.route("/cleanup", methods=["POST"])
|
89 |
def cleanup():
|
90 |
global temp_dir, current_dir
|