Omega-02 commited on
Commit
580fa5d
1 Parent(s): ad55ed0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -4
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
- # Capture the output
9
- output = namespace.get('output', None)
10
- if output is None:
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