joshuarauh commited on
Commit
d38e732
·
verified ·
1 Parent(s): 079e233

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -17
app.py CHANGED
@@ -332,34 +332,35 @@ def extract_and_run_sympy_code(response_text):
332
 
333
  sympy_code = response_text[code_start:code_end].strip()
334
 
335
- # Create a string buffer to capture print output
336
  import io
337
  import sys
338
  from contextlib import redirect_stdout
339
-
340
- # Create string buffer for output
 
341
  output_buffer = io.StringIO()
342
 
343
- # Create globals dict with all SymPy symbols
344
- sympy_globals = {}
345
- sympy_globals.update({name: getattr(sympy, name) for name in dir(sympy)})
346
 
347
  # Create locals dict to capture new variables
348
  local_vars = {}
349
-
350
- # Execute the original code
351
- exec(sympy_code, sympy_globals, local_vars)
352
-
353
- # Print all variables that were created (excluding built-ins and modules)
354
  with redirect_stdout(output_buffer):
355
- print("SymPy Calculation Results:")
356
- print("-" * 25)
357
- for var_name, value in local_vars.items():
358
- if not var_name.startswith('__') and not hasattr(value, '__module__'):
359
- print(f"{var_name}: {value}")
360
 
361
- return output_buffer.getvalue()
 
 
 
 
 
362
 
 
 
363
  except Exception as e:
364
  return f"Error executing SymPy code: {str(e)}"
365
 
 
332
 
333
  sympy_code = response_text[code_start:code_end].strip()
334
 
335
+ # Import necessary modules
336
  import io
337
  import sys
338
  from contextlib import redirect_stdout
339
+ import sympy
340
+
341
+ # Create a string buffer to capture print output
342
  output_buffer = io.StringIO()
343
 
344
+ # Create globals dict with SymPy explicitly included
345
+ sympy_globals = {"sympy": sympy}
346
+ sympy_globals.update(vars(sympy))
347
 
348
  # Create locals dict to capture new variables
349
  local_vars = {}
350
+
351
+ # Redirect stdout and execute the code
 
 
 
352
  with redirect_stdout(output_buffer):
353
+ exec(sympy_code, sympy_globals, local_vars)
 
 
 
 
354
 
355
+ # Append the calculated variables
356
+ output_buffer.write("SymPy Calculation Results:\n")
357
+ output_buffer.write("-" * 25 + "\n")
358
+ for var_name, value in local_vars.items():
359
+ if not var_name.startswith('__') and not hasattr(value, '__module__'):
360
+ output_buffer.write(f"{var_name}: {value}\n")
361
 
362
+ return output_buffer.getvalue()
363
+
364
  except Exception as e:
365
  return f"Error executing SymPy code: {str(e)}"
366