Skip to content
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

Simplify solara code #1786

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
re-introduced backend switch and removed self
  • Loading branch information
Corvince committed Sep 1, 2023
commit 0c0b1f1372bc95735e0462b1db6d128863c22a2a
25 changes: 15 additions & 10 deletions mesa/experimental/jupyter_viz.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import threading

import matplotlib.pyplot as plt
import networkx as nx
import reacton.ipywidgets as widgets
import solara
Expand All @@ -8,6 +9,9 @@

import mesa

# Avoid interactive backend
plt.switch_backend("agg")


@solara.component
def JupyterViz(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is simpler. The reason why I split into JupyterContainer, MesaComponent, and JupyterViz was the answer to these questions on Discord:

The question:

Is it possible to do just

model_params = {
    "N": {
        "type": "SliderInt",
        "value": 50,
        "label": "Number of agents:",
        "min": 10,
        "max": 100,
        "step": 1,
    },
    "width": 10,
    "height": 10,
}
JupyterViz(
    BoltzmannWealthModel, model_params, measures=["Gini"], name="Money Model"
)

The answer,

@rht you can also assign to page = MesaComponent(...) but they you would have to reverse your model, so JupyterViz shouldn't call MesaComponent, but the other way around

This was the reason for the MesaComponent and JupyterViz split. But in hindsight, the split wasn't necessary.

This was the reason for the JupyterContainer split: https://discord.com/channels/1106593685241614489/1106593686223069309/1123583246307954828.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the power of hindsight... but still quite impressive to make it work with all the necessary basic features! Solara uses quite a different paradigm, so not everything is clear from the beginning

Expand Down Expand Up @@ -60,6 +64,7 @@ def make_model():
@solara.component
def ModelController(model, play_interval, current_step, set_current_step):
playing = solara.use_reactive(False)
thread = solara.use_reactive(None)

def on_value_play(change):
if model.running:
Expand All @@ -71,22 +76,22 @@ def do_step():
model.step()
set_current_step(model.schedule.steps)

def do_play(self):
def do_play():
model.running = True
while model.running:
self.do_step()
do_step()

def threaded_do_play(self):
if self.thread is not None and self.thread.is_alive():
def threaded_do_play():
if thread is not None and thread.is_alive():
return
self.thread = threading.Thread(target=self.do_play)
self.thread.start()
thread.value = threading.Thread(target=do_play)
thread.start()

def do_pause(self):
if (self.thread is None) or (not self.thread.is_alive()):
def do_pause():
if (thread is None) or (not thread.is_alive()):
return
self.model.running = False
self.thread.join()
model.running = False
thread.join()

with solara.Row():
solara.Button(label="Step", color="primary", on_click=do_step)
Expand Down