-
Notifications
You must be signed in to change notification settings - Fork 936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Don't continue playing when a model is reset #1796
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,15 +89,25 @@ def ModelController( | |
): | ||
playing = solara.use_reactive(False) | ||
thread = solara.use_reactive(None) | ||
# We track the previous step to detect if user resets the model via | ||
# clicking the reset button or changing the parameters. If previous_step > | ||
# current_step, it means a model reset happens while the simulation is | ||
# still playing. | ||
previous_step = solara.use_reactive(0) | ||
|
||
def on_value_play(change): | ||
if model.running: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should check for model.running and playing.value. That way you won't see any unwanted step+1 on reset |
||
if previous_step.value > current_step and current_step == 0: | ||
# We add extra checks for current_step == 0, just to be sure. | ||
# We automatically stop the playing if a model is reset. | ||
playing.value = False | ||
elif model.running: | ||
do_step() | ||
else: | ||
playing.value = False | ||
|
||
def do_step(): | ||
model.step() | ||
previous_step.value = current_step | ||
set_current_step(model.schedule.steps) | ||
|
||
def do_play(): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you seen the use previous hook? Might be usable here
https://solara.dev/api/use_previous
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tried, but with that, the simulation will always step once after the reset. Not sure why. At least this PR works, and the code is not that much different from if it were using
use_previous
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's still a bit verbose. The step+1 got me curious and I was playing around a bit.
I think a simpler solution would be to use
use_previous
on the model and then look if the previous model is equal to the current model. If it's not set playing to False and reset current step.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's problematic to store 2 copies of model with different specs at any given time. Consumes 2x RAM.