-
Notifications
You must be signed in to change notification settings - Fork 926
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
UI layout #1825
Closed
Closed
UI layout #1825
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
452cca1
caclulate agent size based on Number of Agents
ankitk50 b8e6c09
cap min size of agents to 5
ankitk50 59a5b99
restructure the UI layout
ankitk50 e8fd953
Define UI layout for browser and jupyter
ankitk50 c464a33
Merge branch 'main' into ui_layout
ankitk50 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
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import solara | ||
import threading | ||
import reacton.ipywidgets as widgets | ||
|
||
|
||
@solara.component | ||
def ModelController( | ||
model, play_interval, current_step, set_current_step, reset_counter | ||
): | ||
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 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(): | ||
model.running = True | ||
while model.running: | ||
do_step() | ||
|
||
def threaded_do_play(): | ||
if thread is not None and thread.is_alive(): | ||
return | ||
thread.value = threading.Thread(target=do_play) | ||
thread.start() | ||
|
||
def do_pause(): | ||
if (thread is None) or (not thread.is_alive()): | ||
return | ||
model.running = False | ||
thread.join() | ||
|
||
def do_reset(): | ||
reset_counter.value += 1 | ||
|
||
with solara.Column(): | ||
with solara.Row(gap="10px", justify="center"): | ||
solara.Button( | ||
label="Step", | ||
color="primary", | ||
text=True, | ||
outlined=True, | ||
on_click=do_step, | ||
) | ||
# This style is necessary so that the play widget has almost the same | ||
# height as typical Solara buttons. | ||
solara.Button( | ||
label="Reset", | ||
color="primary", | ||
text=True, | ||
outlined=True, | ||
on_click=do_reset, | ||
) | ||
|
||
# with solara.Row(gap="10px", justify="center"): | ||
solara.Style( | ||
""" | ||
.widget-play { | ||
height: 35px; | ||
} | ||
""" | ||
) | ||
widgets.Play( | ||
value=0, | ||
interval=play_interval, | ||
repeat=True, | ||
show_repeat=False, | ||
on_value=on_value_play, | ||
playing=playing.value, | ||
on_playing=playing.set, | ||
) | ||
|
||
|
||
# threaded_do_play is not used for now because it | ||
# doesn't work in Google colab. We use | ||
# ipywidgets.Play until it is fixed. The threading | ||
# version is definite a much better implementation, | ||
# if it works. | ||
# solara.Button(label="▶", color="primary", on_click=viz.threaded_do_play) | ||
# solara.Button(label="⏸︎", color="primary", on_click=viz.do_pause) | ||
# solara.Button(label="Reset", color="primary", on_click=do_reset) |
Oops, something went wrong.
Oops, something went wrong.
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.
This is another line that needs to be removed.