Taf2023 commited on
Commit
5edc6a5
·
verified ·
1 Parent(s): 7447d98

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +110 -0
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import subprocess
3
+ import io
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
+ # Function to execute Python, Bash, and HTML code
36
+ def execute_code(code, language, inputs, packages):
37
+ try:
38
+ # Handle pip installations if any packages are provided
39
+ install_msg = install_packages(packages)
40
+
41
+ # Handle Python code execution
42
+ if language == "Python":
43
+ old_stdout = sys.stdout
44
+ new_stdout = io.StringIO()
45
+ sys.stdout = new_stdout
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
+ # Handle Bash code execution
61
+ elif language == "Bash":
62
+ output = subprocess.check_output(code, shell=True).decode("utf-8")
63
+ return f"{install_msg}Output:\n{output}\nErrors: None"
64
+
65
+ # Handle HTML code execution
66
+ elif language == "HTML":
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
+ else:
83
+ return "Output: None\nErrors: Unsupported language"
84
+
85
+ except Exception as e:
86
+ return f"{install_msg}Output: None\nErrors: {str(e)}"
87
+
88
+ # Gradio interface setup
89
+ demo = gr.Blocks()
90
+
91
+ with demo:
92
+ gr.Markdown("## Python, Bash, and HTML IDE with Inputs\nEnter your code, inputs, and select the language to run it")
93
+
94
+ with gr.Row():
95
+ code_input = gr.Code(label="Editor", language="python", lines=20)
96
+ language_dropdown = gr.Dropdown(label="Language", choices=["Python", "Bash", "HTML"], value="Python")
97
+
98
+ # Clarified that input values should only be raw values, not variable assignments like 'x=5'
99
+ inputs_field = gr.Textbox(label="Inputs", placeholder="Enter values separated by commas (e.g., 5, 10)")
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 execution
104
+ run_button = gr.Button("Run Code")
105
+
106
+ # Bind the button to the function that executes the code
107
+ run_button.click(fn=execute_code, inputs=[code_input, language_dropdown, inputs_field, packages_field], outputs=output)
108
+
109
+ # Launch the Gradio app
110
+ demo.launch()