Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,110 +1,48 @@
|
|
1 |
-
import gradio as gr
|
2 |
import subprocess
|
3 |
-
import
|
4 |
-
import sys
|
5 |
-
|
6 |
-
# Function to handle input() calls in code by simulating user input
|
7 |
-
class InputMock:
|
8 |
-
def __init__(self, inputs):
|
9 |
-
# Split inputs by commas and remove any '=' sign to handle cases like 'x=10' -> '10'
|
10 |
-
self.inputs = [i.split('=')[-1].strip() for i in inputs.split(",")]
|
11 |
-
self.index = 0
|
12 |
-
|
13 |
-
def readline(self):
|
14 |
-
if self.index < len(self.inputs):
|
15 |
-
# Return the current input and move to the next
|
16 |
-
value = self.inputs[self.index]
|
17 |
-
self.index += 1
|
18 |
-
return value
|
19 |
-
else:
|
20 |
-
# No more inputs, raise EOF to simulate end of input
|
21 |
-
return ''
|
22 |
-
|
23 |
-
# Function to install pip packages
|
24 |
-
def install_packages(packages):
|
25 |
-
try:
|
26 |
-
if packages:
|
27 |
-
package_list = packages.split(',')
|
28 |
-
for package in package_list:
|
29 |
-
package = package.strip()
|
30 |
-
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
31 |
-
return "Packages installed successfully.\n"
|
32 |
-
except subprocess.CalledProcessError as e:
|
33 |
-
return f"Package installation failed: {str(e)}\n"
|
34 |
|
35 |
-
|
36 |
-
def execute_code(code, language, inputs, packages):
|
37 |
try:
|
38 |
-
#
|
39 |
-
|
40 |
|
41 |
-
#
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
# Replace input() with mock input
|
48 |
-
sys_input_backup = sys.stdin
|
49 |
-
sys.stdin = InputMock(inputs)
|
50 |
-
|
51 |
-
exec(code) # Execute the Python code
|
52 |
-
|
53 |
-
# Restore stdout and input after execution
|
54 |
-
output = new_stdout.getvalue()
|
55 |
-
sys.stdout = old_stdout
|
56 |
-
sys.stdin = sys_input_backup
|
57 |
-
|
58 |
-
return f"{install_msg}Output:\n{output}\nErrors: None"
|
59 |
|
60 |
-
#
|
61 |
-
|
62 |
-
output = subprocess.check_output(code, shell=True).decode("utf-8")
|
63 |
-
return f"{install_msg}Output:\n{output}\nErrors: None"
|
64 |
|
65 |
-
#
|
66 |
-
|
67 |
-
# Save the HTML code to a temporary file
|
68 |
-
with open("temp.html", "w") as f:
|
69 |
-
f.write(code)
|
70 |
-
|
71 |
-
# Open the HTML file in the default browser
|
72 |
-
subprocess.check_call(["start", "temp.html"], shell=True)
|
73 |
-
|
74 |
-
# Wait for 5 seconds to allow the browser to load
|
75 |
-
import time
|
76 |
-
time.sleep(5)
|
77 |
-
|
78 |
-
# Capture any errors from the console
|
79 |
-
output = subprocess.check_output(["tasklist", "/FI", "IMAGENAME eq chrome.exe"], shell=True).decode("utf-8")
|
80 |
-
return f"{install_msg}Output:\n{output}\nErrors: None"
|
81 |
|
82 |
-
|
83 |
-
return "Output: None\nErrors: Unsupported language"
|
84 |
|
85 |
except Exception as e:
|
86 |
-
return f"
|
87 |
|
88 |
# Gradio interface setup
|
89 |
demo = gr.Blocks()
|
90 |
|
91 |
with demo:
|
92 |
-
gr.Markdown("##
|
93 |
|
94 |
with gr.Row():
|
95 |
-
|
96 |
-
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
packages_field = gr.Textbox(label="Packages", placeholder="Enter pip packages separated by commas (e.g., numpy, pandas)")
|
101 |
output = gr.Textbox(label="Output", placeholder="Output will appear here", lines=10)
|
102 |
|
103 |
-
# Button to trigger
|
104 |
-
|
105 |
|
106 |
-
# Bind the button to the function that
|
107 |
-
|
108 |
|
109 |
# Launch the Gradio app
|
110 |
demo.launch()
|
|
|
|
|
1 |
import subprocess
|
2 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
def build_android_app(app_name, package_name, website_url, app_logo):
|
|
|
5 |
try:
|
6 |
+
# Create a new Android app
|
7 |
+
subprocess.check_call(["flutter", "create", app_name])
|
8 |
|
9 |
+
# Configure the app
|
10 |
+
with open(f"{app_name}/pubspec.yaml", "r+") as f:
|
11 |
+
content = f.read()
|
12 |
+
f.seek(0)
|
13 |
+
f.write(content.replace("name: flutter_app", f"name: {app_name}").replace("description: A new Flutter project.", f"description: {website_url}").replace("# The following line ensures that the Material Icons font is\n # included with your application, so that you can use the icons in\n # the material Icons class.", f" flutter:\n assets:\n - {app_logo}"))
|
14 |
+
f.truncate()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# Install dependencies
|
17 |
+
subprocess.check_call(["flutter", "pub", "get"], cwd=app_name)
|
|
|
|
|
18 |
|
19 |
+
# Run the app
|
20 |
+
subprocess.check_call(["flutter", "run"], cwd=app_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
return "App built successfully."
|
|
|
23 |
|
24 |
except Exception as e:
|
25 |
+
return f"Error: {str(e)}"
|
26 |
|
27 |
# Gradio interface setup
|
28 |
demo = gr.Blocks()
|
29 |
|
30 |
with demo:
|
31 |
+
gr.Markdown("## Android App Builder\nEnter your app details to build an Android app")
|
32 |
|
33 |
with gr.Row():
|
34 |
+
app_name = gr.Textbox(label="App Name")
|
35 |
+
package_name = gr.Textbox(label="Package Name")
|
36 |
|
37 |
+
website_url = gr.Textbox(label="Website URL")
|
38 |
+
app_logo = gr.Textbox(label="App Logo")
|
|
|
39 |
output = gr.Textbox(label="Output", placeholder="Output will appear here", lines=10)
|
40 |
|
41 |
+
# Button to trigger app building
|
42 |
+
build_button = gr.Button("Build App")
|
43 |
|
44 |
+
# Bind the button to the function that builds the app
|
45 |
+
build_button.click(fn=build_android_app, inputs=[app_name, package_name, website_url, app_logo], outputs=output)
|
46 |
|
47 |
# Launch the Gradio app
|
48 |
demo.launch()
|