Skip to content

Commit 53d76ce

Browse files
started integrating
1 parent 41b0458 commit 53d76ce

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/App.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@ const DnDFlow = () => {
564564
edges,
565565
solverParams,
566566
globalVariables,
567-
events
567+
events,
568+
pythonCode
568569
};
569570

570571
const response = await fetch(getApiEndpoint('/run-pathsim'), {

src/pathsim_utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,8 @@ def make_pathsim_model(graph_data: dict) -> tuple[Simulation, float]:
532532
global_namespace = make_global_variables(global_vars)
533533

534534
# Create a combined namespace that includes built-in functions and global variables
535-
eval_namespace = {**globals(), **global_namespace}
535+
eval_namespace = globals().copy()
536+
eval_namespace.update(global_namespace)
536537

537538
solver_prms, extra_params, duration = make_solver_params(
538539
solver_prms, eval_namespace
@@ -554,6 +555,18 @@ def make_pathsim_model(graph_data: dict) -> tuple[Simulation, float]:
554555
for node in nodes:
555556
var_name = make_var_name(node)
556557
eval_namespace[var_name] = find_block_by_id(node["id"], blocks)
558+
559+
# Execute python code
560+
# NOTE Rem: we execute it after creating blocks so that user code can reference blocks by their var names
561+
# ideally we would execute this just after `make_global_variables` so that global variables can be defined in the code editor
562+
# but I couldn't get it to work, it must be a namespace issue
563+
python_code = graph_data.get("pythonCode", "")
564+
if python_code:
565+
try:
566+
exec(python_code, eval_namespace)
567+
except Exception as e:
568+
return jsonify({"error": f"Error executing Python code: {str(e)}"}), 400
569+
557570
events += make_events(graph_data.get("events", []), eval_namespace)
558571

559572
# Create the simulation

0 commit comments

Comments
 (0)