From 0c23387557520140c59b47ba319a6b6fdbf2b245 Mon Sep 17 00:00:00 2001 From: corvince <13568919+Corvince@users.noreply.github.com> Date: Wed, 13 Nov 2024 20:54:16 +0100 Subject: [PATCH] Only set model_parameters once --- mesa/visualization/solara_viz.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/mesa/visualization/solara_viz.py b/mesa/visualization/solara_viz.py index 10718e8a804..1f3c8036888 100644 --- a/mesa/visualization/solara_viz.py +++ b/mesa/visualization/solara_viz.py @@ -382,11 +382,17 @@ def ModelCreator( ) user_params, fixed_params = split_model_params(user_params) - # set model_parameters to the default values for all parameters - model_parameters.value = { - **fixed_params, - **{k: v.get("value") for k, v in user_params.items()}, - } + # Use solara.use_effect to run the initialization code only once + solara.use_effect( + # set model_parameters to the default values for all parameters + lambda: model_parameters.set( + { + **fixed_params, + **{k: v.get("value") for k, v in user_params.items()}, + } + ), + [], + ) def on_change(name, value): model_parameters.value = {**model_parameters.value, name: value}