File size: 965 Bytes
e656836
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import gradio as gr
import subprocess

def compile_and_run_code(code):
    try:
        # Save the code to a temporary file
        with open("temp_code.py", "w") as file:
            file.write(code)

        # Execute the code and capture the output
        result = subprocess.run(
            ["python", "temp_code.py"],
            capture_output=True,
            text=True
        )

        # Return the output or errors
        if result.returncode == 0:
            return result.stdout
        else:
            return result.stderr

    except Exception as e:
        return str(e)

# Create the Gradio interface
interface = gr.Interface(
    fn=compile_and_run_code,
    inputs=gr.Textbox(lines=20, placeholder="Write your Python code here..."),
    outputs=gr.Textbox(label="Output"),
    title="Python Code Compiler",
    description="Write and run Python code directly in this simple interface."
)

if __name__ == "__main__":
    interface.launch()