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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import sys
|
| 4 |
+
import os
|
| 5 |
+
import tempfile
|
| 6 |
+
import shutil
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
# Create a working directory for user files
|
| 10 |
+
WORKSPACE = Path("user_workspace")
|
| 11 |
+
WORKSPACE.mkdir(exist_ok=True)
|
| 12 |
+
|
| 13 |
+
def run_python_code(code, files_state):
|
| 14 |
+
"""Execute Python code and return output"""
|
| 15 |
+
try:
|
| 16 |
+
# Create a temporary file to run the code
|
| 17 |
+
with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False, dir=WORKSPACE) as f:
|
| 18 |
+
f.write(code)
|
| 19 |
+
temp_file = f.name
|
| 20 |
+
|
| 21 |
+
# Run the code
|
| 22 |
+
result = subprocess.run(
|
| 23 |
+
[sys.executable, temp_file],
|
| 24 |
+
capture_output=True,
|
| 25 |
+
text=True,
|
| 26 |
+
timeout=30,
|
| 27 |
+
cwd=WORKSPACE
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# Clean up temp file
|
| 31 |
+
os.unlink(temp_file)
|
| 32 |
+
|
| 33 |
+
# Combine stdout and stderr
|
| 34 |
+
output = result.stdout
|
| 35 |
+
if result.stderr:
|
| 36 |
+
output += f"\n--- Errors ---\n{result.stderr}"
|
| 37 |
+
|
| 38 |
+
return output if output else "Code executed successfully (no output)"
|
| 39 |
+
|
| 40 |
+
except subprocess.TimeoutExpired:
|
| 41 |
+
return "Error: Code execution timed out (30 second limit)"
|
| 42 |
+
except Exception as e:
|
| 43 |
+
return f"Error: {str(e)}"
|
| 44 |
+
|
| 45 |
+
def install_package(package_name):
|
| 46 |
+
"""Install a Python package using pip"""
|
| 47 |
+
if not package_name.strip():
|
| 48 |
+
return "Please enter a package name"
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
result = subprocess.run(
|
| 52 |
+
[sys.executable, "-m", "pip", "install", package_name],
|
| 53 |
+
capture_output=True,
|
| 54 |
+
text=True,
|
| 55 |
+
timeout=120
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
output = result.stdout
|
| 59 |
+
if result.stderr:
|
| 60 |
+
output += f"\n{result.stderr}"
|
| 61 |
+
|
| 62 |
+
return output if output else f"Successfully installed {package_name}"
|
| 63 |
+
|
| 64 |
+
except subprocess.TimeoutExpired:
|
| 65 |
+
return "Error: Installation timed out"
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return f"Error installing package: {str(e)}"
|
| 68 |
+
|
| 69 |
+
def save_file(filename, content):
|
| 70 |
+
"""Save file to workspace"""
|
| 71 |
+
if not filename:
|
| 72 |
+
return "Please enter a filename", None
|
| 73 |
+
|
| 74 |
+
try:
|
| 75 |
+
filepath = WORKSPACE / filename
|
| 76 |
+
filepath.write_text(content)
|
| 77 |
+
return f"File '{filename}' saved successfully", filepath
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return f"Error saving file: {str(e)}", None
|
| 80 |
+
|
| 81 |
+
def load_file(file):
|
| 82 |
+
"""Load uploaded file"""
|
| 83 |
+
if file is None:
|
| 84 |
+
return "", "No file uploaded"
|
| 85 |
+
|
| 86 |
+
try:
|
| 87 |
+
content = Path(file.name).read_text()
|
| 88 |
+
filename = Path(file.name).name
|
| 89 |
+
return content, f"Loaded: {filename}"
|
| 90 |
+
except Exception as e:
|
| 91 |
+
return "", f"Error loading file: {str(e)}"
|
| 92 |
+
|
| 93 |
+
def list_workspace_files():
|
| 94 |
+
"""List all files in workspace"""
|
| 95 |
+
try:
|
| 96 |
+
files = [f.name for f in WORKSPACE.iterdir() if f.is_file()]
|
| 97 |
+
if files:
|
| 98 |
+
return "Files in workspace:\n" + "\n".join(f" - {f}" for f in files)
|
| 99 |
+
return "Workspace is empty"
|
| 100 |
+
except Exception as e:
|
| 101 |
+
return f"Error listing files: {str(e)}"
|
| 102 |
+
|
| 103 |
+
def download_file(filename):
|
| 104 |
+
"""Prepare file for download"""
|
| 105 |
+
if not filename:
|
| 106 |
+
return None, "Please enter a filename"
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
filepath = WORKSPACE / filename
|
| 110 |
+
if filepath.exists():
|
| 111 |
+
return str(filepath), f"Ready to download: {filename}"
|
| 112 |
+
return None, f"File '{filename}' not found in workspace"
|
| 113 |
+
except Exception as e:
|
| 114 |
+
return None, f"Error: {str(e)}"
|
| 115 |
+
|
| 116 |
+
# Create the Gradio interface
|
| 117 |
+
with gr.Blocks(title="Python IDE", theme=gr.themes.Soft()) as app:
|
| 118 |
+
gr.Markdown("# ๐ Python IDE")
|
| 119 |
+
gr.Markdown("Write, run, and test Python code with file management and package installation")
|
| 120 |
+
|
| 121 |
+
with gr.Row():
|
| 122 |
+
with gr.Column(scale=2):
|
| 123 |
+
code_editor = gr.Code(
|
| 124 |
+
label="Python Code Editor",
|
| 125 |
+
language="python",
|
| 126 |
+
value='# Write your Python code here\nprint("Hello, World!")\n\n# You can use installed packages\n# import numpy as np\n# print(np.array([1, 2, 3]))',
|
| 127 |
+
lines=20
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
with gr.Row():
|
| 131 |
+
run_btn = gr.Button("โถ๏ธ Run Code", variant="primary", scale=2)
|
| 132 |
+
clear_btn = gr.Button("๐๏ธ Clear", scale=1)
|
| 133 |
+
|
| 134 |
+
output_box = gr.Textbox(
|
| 135 |
+
label="Output",
|
| 136 |
+
lines=10,
|
| 137 |
+
max_lines=20,
|
| 138 |
+
interactive=False,
|
| 139 |
+
placeholder="Output will appear here..."
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
with gr.Column(scale=1):
|
| 143 |
+
gr.Markdown("### ๐ฆ Package Manager")
|
| 144 |
+
package_input = gr.Textbox(
|
| 145 |
+
label="Package Name",
|
| 146 |
+
placeholder="e.g., numpy, pandas, requests"
|
| 147 |
+
)
|
| 148 |
+
install_btn = gr.Button("Install Package")
|
| 149 |
+
install_output = gr.Textbox(
|
| 150 |
+
label="Installation Output",
|
| 151 |
+
lines=5,
|
| 152 |
+
interactive=False
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
gr.Markdown("### ๐พ File Management")
|
| 156 |
+
|
| 157 |
+
with gr.Tab("Save"):
|
| 158 |
+
save_filename = gr.Textbox(
|
| 159 |
+
label="Filename",
|
| 160 |
+
placeholder="e.g., my_script.py"
|
| 161 |
+
)
|
| 162 |
+
save_btn = gr.Button("Save Current Code")
|
| 163 |
+
save_output = gr.Textbox(label="Status", lines=2)
|
| 164 |
+
|
| 165 |
+
with gr.Tab("Upload"):
|
| 166 |
+
upload_file = gr.File(
|
| 167 |
+
label="Upload Python File",
|
| 168 |
+
file_types=[".py", ".txt"]
|
| 169 |
+
)
|
| 170 |
+
upload_output = gr.Textbox(label="Status", lines=2)
|
| 171 |
+
|
| 172 |
+
with gr.Tab("Download"):
|
| 173 |
+
download_filename = gr.Textbox(
|
| 174 |
+
label="Filename to Download",
|
| 175 |
+
placeholder="e.g., my_script.py"
|
| 176 |
+
)
|
| 177 |
+
download_btn = gr.Button("Prepare Download")
|
| 178 |
+
download_file_output = gr.File(label="Download File")
|
| 179 |
+
download_status = gr.Textbox(label="Status", lines=2)
|
| 180 |
+
|
| 181 |
+
list_btn = gr.Button("๐ List Workspace Files")
|
| 182 |
+
list_output = gr.Textbox(label="Workspace Files", lines=5)
|
| 183 |
+
|
| 184 |
+
gr.Markdown("""
|
| 185 |
+
### ๐ Tips:
|
| 186 |
+
- Write your code in the editor and click "Run Code"
|
| 187 |
+
- Install packages before using them in your code
|
| 188 |
+
- Save your work frequently
|
| 189 |
+
- Upload existing .py files to continue working on them
|
| 190 |
+
- All files are saved in a workspace directory
|
| 191 |
+
""")
|
| 192 |
+
|
| 193 |
+
# Event handlers
|
| 194 |
+
files_state = gr.State([])
|
| 195 |
+
|
| 196 |
+
run_btn.click(
|
| 197 |
+
fn=run_python_code,
|
| 198 |
+
inputs=[code_editor, files_state],
|
| 199 |
+
outputs=output_box
|
| 200 |
+
)
|
| 201 |
+
|
| 202 |
+
clear_btn.click(
|
| 203 |
+
fn=lambda: "",
|
| 204 |
+
outputs=code_editor
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
install_btn.click(
|
| 208 |
+
fn=install_package,
|
| 209 |
+
inputs=package_input,
|
| 210 |
+
outputs=install_output
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
save_btn.click(
|
| 214 |
+
fn=lambda fn, code: save_file(fn, code)[0],
|
| 215 |
+
inputs=[save_filename, code_editor],
|
| 216 |
+
outputs=save_output
|
| 217 |
+
)
|
| 218 |
+
|
| 219 |
+
upload_file.change(
|
| 220 |
+
fn=load_file,
|
| 221 |
+
inputs=upload_file,
|
| 222 |
+
outputs=[code_editor, upload_output]
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
download_btn.click(
|
| 226 |
+
fn=download_file,
|
| 227 |
+
inputs=download_filename,
|
| 228 |
+
outputs=[download_file_output, download_status]
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
list_btn.click(
|
| 232 |
+
fn=list_workspace_files,
|
| 233 |
+
outputs=list_output
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
if __name__ == "__main__":
|
| 237 |
+
app.launch()
|