xd11yggy commited on
Commit
9f49532
·
verified ·
1 Parent(s): 84a1f31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -3
app.py CHANGED
@@ -13,13 +13,18 @@ class CodeExecutor:
13
  f.write(inputs)
14
 
15
  output_file = f"{self.temp_dir.name}/output.txt"
 
16
  try:
17
- with open(output_file, "w") as f:
18
  subprocess.run(
19
- ["python", "-c", code], check=True, timeout=5, stdin=open(input_file, "r"), stdout=f, stderr=f
20
  )
21
  with open(output_file, "r") as f:
22
  output = f.read()
 
 
 
 
23
  except subprocess.CalledProcessError as e:
24
  output = e.output.decode("utf-8")
25
  except subprocess.TimeoutExpired:
@@ -36,7 +41,7 @@ def create_interface():
36
  gr.Markdown("Enter Python code and inputs to execute.")
37
  code_input = gr.Code(language="python", label="Code")
38
  inputs_input = gr.Textbox(label="Inputs")
39
- output_text = gr.Textbox(label="Output", lines=10)
40
  run_button = gr.Button("Run")
41
  run_button.click(
42
  CodeExecutor().execute, inputs=[code_input, inputs_input], outputs=output_text
 
13
  f.write(inputs)
14
 
15
  output_file = f"{self.temp_dir.name}/output.txt"
16
+ error_file = f"{self.temp_dir.name}/error.txt"
17
  try:
18
+ with open(output_file, "w") as f, open(error_file, "w") as e:
19
  subprocess.run(
20
+ ["python", "-c", code], check=True, timeout=5, stdin=open(input_file, "r"), stdout=f, stderr=e
21
  )
22
  with open(output_file, "r") as f:
23
  output = f.read()
24
+ with open(error_file, "r") as f:
25
+ error = f.read()
26
+ if error:
27
+ output = f"Error:\n{error}"
28
  except subprocess.CalledProcessError as e:
29
  output = e.output.decode("utf-8")
30
  except subprocess.TimeoutExpired:
 
41
  gr.Markdown("Enter Python code and inputs to execute.")
42
  code_input = gr.Code(language="python", label="Code")
43
  inputs_input = gr.Textbox(label="Inputs")
44
+ output_text = gr.Textbox(label="Output", lines=20)
45
  run_button = gr.Button("Run")
46
  run_button.click(
47
  CodeExecutor().execute, inputs=[code_input, inputs_input], outputs=output_text