joshuarauh commited on
Commit
8c94fd2
·
verified ·
1 Parent(s): 9a57eea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -365,18 +365,22 @@ def extract_and_run_sympy_code(response_text):
365
  from contextlib import redirect_stdout
366
  output_buffer = io.StringIO()
367
 
368
- logger.debug("Attempting to execute code")
369
- # Execute with try/except to catch specific error
370
  try:
371
- exec(sympy_code, globals_dict, local_vars)
372
-
373
- # Print all variables that were created
374
- print("SymPy Calculation Results:")
375
- print("-" * 25)
376
- for var_name, value in local_vars.items():
377
- if (not var_name.startswith('__') and
378
- not hasattr(value, '__module__')):
379
- print(f"{var_name} = {value}")
 
 
 
 
 
 
380
  except NameError as e:
381
  logger.error(f"NameError during execution: {str(e)}")
382
  logger.debug(f"Available names: {list(globals_dict.keys())}")
@@ -387,6 +391,7 @@ def extract_and_run_sympy_code(response_text):
387
  logger.error(f"Other error during execution: {str(e)}")
388
  return f"Error executing SymPy code: {str(e)}"
389
 
 
390
  result = output_buffer.getvalue()
391
  logger.debug(f"Final output:\n{result}")
392
  return result
 
365
  from contextlib import redirect_stdout
366
  output_buffer = io.StringIO()
367
 
 
 
368
  try:
369
+ # Execute the code and capture all output in one redirect_stdout block
370
+ with redirect_stdout(output_buffer):
371
+ # First print a header
372
+ print("SymPy Calculation Results:")
373
+ print("-" * 25)
374
+
375
+ # Execute the code
376
+ exec(sympy_code, globals_dict, local_vars)
377
+
378
+ # Print all variables created
379
+ for var_name, value in local_vars.items():
380
+ if (not var_name.startswith('__') and
381
+ not hasattr(value, '__module__')):
382
+ print(f"{var_name} = {value}")
383
+
384
  except NameError as e:
385
  logger.error(f"NameError during execution: {str(e)}")
386
  logger.debug(f"Available names: {list(globals_dict.keys())}")
 
391
  logger.error(f"Other error during execution: {str(e)}")
392
  return f"Error executing SymPy code: {str(e)}"
393
 
394
+ # Get the captured output
395
  result = output_buffer.getvalue()
396
  logger.debug(f"Final output:\n{result}")
397
  return result