Spaces:
Running
Running
Update shared_vis_python_exe.py
Browse files- shared_vis_python_exe.py +57 -57
shared_vis_python_exe.py
CHANGED
|
@@ -281,66 +281,66 @@ class PythonExecutor:
|
|
| 281 |
|
| 282 |
# return result, report
|
| 283 |
|
| 284 |
-
def execute(
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
) -> Tuple[Union[str, Dict[str, Any]], str]:
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
runtime.exec_code("\n".join(code))
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
elif answer_symbol:
|
| 304 |
-
runtime.exec_code("\n".join(code))
|
| 305 |
-
result = runtime._global_vars.get(answer_symbol, "")
|
| 306 |
-
elif answer_expr:
|
| 307 |
-
runtime.exec_code("\n".join(code))
|
| 308 |
-
result = runtime.eval_code(answer_expr)
|
| 309 |
-
else:
|
| 310 |
-
if len(code) > 1:
|
| 311 |
-
runtime.exec_code("\n".join(code[:-1]))
|
| 312 |
-
result = runtime.eval_code(code[-1])
|
| 313 |
-
else:
|
| 314 |
runtime.exec_code("\n".join(code))
|
| 315 |
-
result =
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
result = {
|
| 321 |
-
'
|
| 322 |
-
'
|
| 323 |
-
}
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
report = f"Error: {str(e)}\n{traceback.format_exc()}"
|
| 335 |
-
|
| 336 |
-
# Ensure result is serializable
|
| 337 |
-
try:
|
| 338 |
-
pickle.dumps(result)
|
| 339 |
-
except Exception as e:
|
| 340 |
-
result = f"Result serialization error: {str(e)}"
|
| 341 |
-
report = f"Serialization Error: {str(e)}"
|
| 342 |
-
|
| 343 |
-
return result, report
|
| 344 |
|
| 345 |
|
| 346 |
|
|
|
|
| 281 |
|
| 282 |
# return result, report
|
| 283 |
|
| 284 |
+
def execute(
|
| 285 |
+
self,
|
| 286 |
+
code,
|
| 287 |
+
messages,
|
| 288 |
+
get_answer_from_stdout=True,
|
| 289 |
+
runtime_class=None,
|
| 290 |
+
answer_symbol=None,
|
| 291 |
+
answer_expr=None,
|
| 292 |
+
) -> Tuple[Union[str, Dict[str, Any]], str]:
|
| 293 |
+
print(runtime_class)
|
| 294 |
+
runtime = self.persistent_runtime
|
| 295 |
+
|
| 296 |
+
try:
|
| 297 |
+
if get_answer_from_stdout:
|
| 298 |
+
program_io = io.StringIO()
|
| 299 |
+
with redirect_stdout(program_io):
|
| 300 |
+
runtime.exec_code("\n".join(code))
|
| 301 |
+
program_io.seek(0)
|
| 302 |
+
result = program_io.read()
|
| 303 |
+
elif answer_symbol:
|
| 304 |
runtime.exec_code("\n".join(code))
|
| 305 |
+
result = runtime._global_vars.get(answer_symbol, "")
|
| 306 |
+
elif answer_expr:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
runtime.exec_code("\n".join(code))
|
| 308 |
+
result = runtime.eval_code(answer_expr)
|
| 309 |
+
else:
|
| 310 |
+
if len(code) > 1:
|
| 311 |
+
runtime.exec_code("\n".join(code[:-1]))
|
| 312 |
+
result = runtime.eval_code(code[-1])
|
| 313 |
+
else:
|
| 314 |
+
runtime.exec_code("\n".join(code))
|
| 315 |
+
result = ""
|
| 316 |
+
|
| 317 |
+
# Check for captured figures
|
| 318 |
+
captured_figures = runtime._global_vars.get("_captured_figures", [])
|
| 319 |
+
if captured_figures:
|
| 320 |
+
result = {
|
| 321 |
+
'text': result,
|
| 322 |
+
'images': captured_figures
|
| 323 |
+
} if result else {'images': captured_figures}
|
| 324 |
+
else:
|
| 325 |
+
result = {'text': result} if result else {}
|
| 326 |
+
|
| 327 |
+
report = "Done"
|
| 328 |
+
|
| 329 |
+
except Exception as e:
|
| 330 |
result = {
|
| 331 |
+
'error': str(e),
|
| 332 |
+
'traceback': traceback.format_exc()
|
| 333 |
+
}
|
| 334 |
+
report = f"Error: {str(e)}\n{traceback.format_exc()}"
|
| 335 |
+
|
| 336 |
+
# Ensure result is serializable
|
| 337 |
+
try:
|
| 338 |
+
pickle.dumps(result)
|
| 339 |
+
except Exception as e:
|
| 340 |
+
result = f"Result serialization error: {str(e)}"
|
| 341 |
+
report = f"Serialization Error: {str(e)}"
|
| 342 |
+
|
| 343 |
+
return result, report
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
|
| 345 |
|
| 346 |
|