Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,112 @@
|
|
1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import random
|
3 |
+
import sys
|
4 |
+
import logging
|
5 |
+
import tkinter as tk
|
6 |
+
from tkinter import ttk, filedialog, scrolledtext
|
7 |
+
import subprocess
|
8 |
|
9 |
+
# Configure logging
|
10 |
+
logging.basicConfig(
|
11 |
+
level=logging.INFO,
|
12 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
13 |
+
handlers=[
|
14 |
+
logging.FileHandler('app.log'),
|
15 |
+
logging.StreamHandler()
|
16 |
+
]
|
17 |
+
)
|
18 |
+
|
19 |
+
# Random code snippets
|
20 |
+
CODE_SNIPPETS = [
|
21 |
+
"print('Hello, World!')",
|
22 |
+
"for i in range(10): print(i)",
|
23 |
+
"def greet(name): return f'Hello, {name}!'",
|
24 |
+
"class Example: pass"
|
25 |
+
]
|
26 |
+
|
27 |
+
class CodeGeneratorApp:
|
28 |
+
def __init__(self, root):
|
29 |
+
self.root = root
|
30 |
+
self.root.title("Python Code Generator")
|
31 |
+
self.root.geometry("800x600")
|
32 |
+
self.current_output_folder = None
|
33 |
+
self.create_widgets()
|
34 |
+
|
35 |
+
def create_widgets(self):
|
36 |
+
# Output Folder Selection
|
37 |
+
self.output_folder_label = ttk.Label(self.root, text="Select Output Folder:")
|
38 |
+
self.output_folder_label.pack(pady=5)
|
39 |
+
self.output_folder_button = ttk.Button(self.root, text="Browse...", command=self.select_output_folder)
|
40 |
+
self.output_folder_button.pack(pady=5)
|
41 |
+
|
42 |
+
# Script Name Entry
|
43 |
+
self.script_name_label = ttk.Label(self.root, text="Enter Script Name (without .py):")
|
44 |
+
self.script_name_label.pack(pady=5)
|
45 |
+
self.script_name_entry = ttk.Entry(self.root, width=50)
|
46 |
+
self.script_name_entry.pack(pady=5)
|
47 |
+
|
48 |
+
# Generate and Execute Buttons
|
49 |
+
self.generate_button = ttk.Button(self.root, text="Generate Code", command=self.generate_code)
|
50 |
+
self.generate_button.pack(pady=10)
|
51 |
+
self.execute_button = ttk.Button(self.root, text="Execute Code", command=self.execute_code)
|
52 |
+
self.execute_button.pack(pady=10)
|
53 |
+
|
54 |
+
# Output Window
|
55 |
+
self.output_frame = ttk.Frame(self.root)
|
56 |
+
self.output_frame.pack(pady=10, fill='both', expand=True)
|
57 |
+
self.output_label = ttk.Label(self.output_frame, text="Output:")
|
58 |
+
self.output_label.pack(pady=5)
|
59 |
+
self.output_text = scrolledtext.ScrolledText(self.output_frame, width=100, height=20)
|
60 |
+
self.output_text.pack(pady=5)
|
61 |
+
|
62 |
+
def select_output_folder(self):
|
63 |
+
self.current_output_folder = filedialog.askdirectory()
|
64 |
+
self.output_text.insert('1.0', f"Output folder set to: {self.current_output_folder}\n")
|
65 |
+
|
66 |
+
def generate_code(self):
|
67 |
+
if not self.current_output_folder:
|
68 |
+
self.output_text.insert('1.0', "Please select an output folder first.\n")
|
69 |
+
return
|
70 |
+
|
71 |
+
script_name = self.script_name_entry.get()
|
72 |
+
if not script_name:
|
73 |
+
script_name = f"script_{random.randint(1000, 9999)}"
|
74 |
+
|
75 |
+
codeSnippet = random.choice(CODE_SNIPPETS)
|
76 |
+
scriptcontent = f"import logging\nlogging.basicConfig(level=logging.INFO)\n\n{codeSnippet}"
|
77 |
+
|
78 |
+
try:
|
79 |
+
with open(os.path.join(self.current_output_folder, f"{script_name}.py"), 'w') as f:
|
80 |
+
f.write(scriptcontent)
|
81 |
+
self.output_text.insert('1.0', f"Script generated successfully: {script_name}.py\n")
|
82 |
+
except Exception as e:
|
83 |
+
self.output_text.insert('1.0', f"Error generating script: {str(e)}\n")
|
84 |
+
|
85 |
+
def execute_code(self):
|
86 |
+
script_name = self.script_name_entry.get()
|
87 |
+
if not script_name:
|
88 |
+
self.output_text.insert('1.0', "Please name the script first.\n")
|
89 |
+
return
|
90 |
+
|
91 |
+
script_path = os.path.join(self.current_output_folder, f"{script_name}.py")
|
92 |
+
if not os.path.exists(script_path):
|
93 |
+
self.output_text.insert('1.0', f"Script not found at: {script_path}\n")
|
94 |
+
return
|
95 |
+
|
96 |
+
try:
|
97 |
+
process = subprocess.Popen([sys.executable, script_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
98 |
+
output, error = process.communicate()
|
99 |
+
if output:
|
100 |
+
self.output_text.insert('1.0', f"Output:\n{output}\n")
|
101 |
+
if error:
|
102 |
+
self.output_text.insert('1.0', f"Error:\n{error}\n")
|
103 |
+
except Exception as e:
|
104 |
+
self.output_text.insert('1.0', f"Execution error: {str(e)}\n")
|
105 |
+
|
106 |
+
def main():
|
107 |
+
root = tk.Tk()
|
108 |
+
app = CodeGeneratorApp(root)
|
109 |
+
root.mainloop()
|
110 |
+
|
111 |
+
if __name__ == "__main__":
|
112 |
+
main()
|