1515from .pathsim_utils import make_pathsim_model , map_str_to_object
1616from pathsim .blocks import Scope , Spectrum
1717
18- # Import pathsim_utils to share eval_namespace
19- from . import pathsim_utils
20-
2118# Sphinx imports for docstring processing
2219from docutils .core import publish_parts
2320
@@ -324,14 +321,9 @@ def run_pathsim():
324321 return jsonify ({"success" : False , "error" : f"Server error: { str (e )} " }), 500
325322
326323
327- # Global namespace for user-defined variables and functions
328- eval_namespace = {}
329-
330-
331324@app .route ("/execute-python" , methods = ["POST" ])
332325def execute_python ():
333- """Execute Python code and update the global eval_namespace with any new variables/functions."""
334- global eval_namespace
326+ """Execute Python code and returns variables/functions."""
335327
336328 try :
337329 data = request .json
@@ -341,16 +333,13 @@ def execute_python():
341333 return jsonify ({"success" : False , "error" : "No code provided" }), 400
342334
343335 # Create a temporary namespace that includes current eval_namespace
344- temp_namespace = eval_namespace . copy ()
345- temp_namespace .update (globals ())
336+ temp_namespace = {}
337+ # temp_namespace.update(globals())
346338
347339 # Capture stdout and stderr
348340 stdout_capture = io .StringIO ()
349341 stderr_capture = io .StringIO ()
350342
351- # Track variables before execution
352- vars_before = set (temp_namespace .keys ())
353-
354343 try :
355344 with redirect_stdout (stdout_capture ), redirect_stderr (stderr_capture ):
356345 exec (code , temp_namespace )
@@ -363,34 +352,29 @@ def execute_python():
363352 return jsonify ({"success" : False , "error" : error_output }), 400
364353
365354 # Find new variables and functions
366- vars_after = set (temp_namespace .keys ())
367- new_vars = vars_after - vars_before
355+ vars = set (temp_namespace .keys ())
356+ # new_vars = vars_after - vars_before
368357
369358 # Filter out built-ins and modules, keep user-defined items
370359 user_variables = {}
371360 user_functions = []
372361
373- for var_name in new_vars :
362+ for var_name in vars :
374363 if not var_name .startswith ("__" ):
375364 value = temp_namespace [var_name ]
376365 if callable (value ) and hasattr (value , "__name__" ):
377366 user_functions .append (var_name )
378- # Add function to eval_namespace
379- eval_namespace [var_name ] = value
380367 else :
381368 # Try to serialize the value for display
382369 try :
383370 if isinstance (value , (int , float , str , bool , list , dict )):
384371 user_variables [var_name ] = value
385372 else :
386373 user_variables [var_name ] = str (value )
387- # Add variable to eval_namespace
388- eval_namespace [var_name ] = value
389374 except Exception :
390375 user_variables [var_name ] = (
391376 f"<{ type (value ).__name__ } object>"
392377 )
393- eval_namespace [var_name ] = value
394378
395379 return jsonify (
396380 {
@@ -411,47 +395,6 @@ def execute_python():
411395 return jsonify ({"success" : False , "error" : f"Server error: { str (e )} " }), 500
412396
413397
414- @app .route ("/get-eval-namespace" , methods = ["GET" ])
415- def get_eval_namespace ():
416- """Get the current eval_namespace for debugging/inspection."""
417- try :
418- # Create a serializable version of the namespace
419- serializable_namespace = {}
420-
421- for key , value in eval_namespace .items ():
422- if callable (value ):
423- serializable_namespace [key ] = f"<function { key } >"
424- else :
425- try :
426- if isinstance (value , (int , float , str , bool , list , dict )):
427- serializable_namespace [key ] = value
428- else :
429- serializable_namespace [key ] = str (value )
430- except Exception :
431- serializable_namespace [key ] = f"<{ type (value ).__name__ } object>"
432-
433- return jsonify ({"success" : True , "namespace" : serializable_namespace })
434- except Exception as e :
435- return jsonify (
436- {"success" : False , "error" : f"Error retrieving namespace: { str (e )} " }
437- ), 500
438-
439-
440- @app .route ("/clear-eval-namespace" , methods = ["POST" ])
441- def clear_eval_namespace ():
442- """Clear the eval_namespace."""
443- global eval_namespace
444- try :
445- eval_namespace .clear ()
446- return jsonify (
447- {"success" : True , "message" : "Eval namespace cleared successfully." }
448- )
449- except Exception as e :
450- return jsonify (
451- {"success" : False , "error" : f"Error clearing namespace: { str (e )} " }
452- ), 500
453-
454-
455398# Catch-all route for React Router (SPA routing)
456399@app .route ("/<path:path>" )
457400def catch_all (path ):
0 commit comments