Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,25 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
|
3 |
def run_code(code):
|
4 |
try:
|
|
|
|
|
|
|
|
|
5 |
# Execute the code in a separate namespace
|
6 |
namespace = {}
|
7 |
exec(code, namespace)
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
output = "No output captured."
|
12 |
except Exception as e:
|
13 |
output = f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
14 |
return output
|
15 |
|
16 |
# Define the Gradio interface
|
|
|
1 |
import gradio as gr
|
2 |
+
import sys
|
3 |
+
from io import StringIO
|
4 |
|
5 |
def run_code(code):
|
6 |
try:
|
7 |
+
# Redirect stdout to a buffer
|
8 |
+
output_buffer = StringIO()
|
9 |
+
sys.stdout = output_buffer
|
10 |
+
|
11 |
# Execute the code in a separate namespace
|
12 |
namespace = {}
|
13 |
exec(code, namespace)
|
14 |
+
|
15 |
+
# Get the output from the buffer
|
16 |
+
output = output_buffer.getvalue()
|
|
|
17 |
except Exception as e:
|
18 |
output = f"Error: {str(e)}"
|
19 |
+
finally:
|
20 |
+
# Reset stdout to its original value
|
21 |
+
sys.stdout = sys.__stdout__
|
22 |
+
|
23 |
return output
|
24 |
|
25 |
# Define the Gradio interface
|