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

viz: Combine code for rendering in browser and Jupyter #2180

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Changes from all commits
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
120 changes: 38 additions & 82 deletions mesa/visualization/jupyter_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
See the Visualization Tutorial and example models for more details.
"""

import sys
import threading

import reacton.ipywidgets as widgets
Expand Down Expand Up @@ -158,92 +157,49 @@ def do_reseed():
"""Update the random seed for the model."""
reactive_seed.value = model.random.random()

# jupyter
dependencies = [current_step.value, reactive_seed.value]

def render_in_jupyter():
"""Render the visualization components in Jupyter notebook."""
with solara.GridFixed(columns=2):
UserInputs(user_params, on_change=handle_change_model_params)
ModelController(model, play_interval, current_step, reset_counter)
solara.Markdown(md_text=f"###Step - {current_step}")
# if space drawer is disabled, do not include it
layout_types = [{"Space": "default"}] if space_drawer else []

with solara.GridFixed(columns=2):
# 4. Space
if space_drawer == "default":
# draw with the default implementation
components_matplotlib.SpaceMatplotlib(
model, agent_portrayal, dependencies=dependencies
)
elif space_drawer == "altair":
components_altair.SpaceAltair(
model, agent_portrayal, dependencies=dependencies
)
elif space_drawer:
# if specified, draw agent space with an alternate renderer
space_drawer(model, agent_portrayal)
# otherwise, do nothing (do not draw space)

# 5. Plots
if measures:
for measure in measures:
if callable(measure):
# Is a custom object
measure(model)
else:
components_matplotlib.PlotMatplotlib(
model, measure, dependencies=dependencies
)

def render_in_browser():
"""Render the visualization components in a web browser."""
# if space drawer is disabled, do not include it
layout_types = [{"Space": "default"}] if space_drawer else []

if measures:
layout_types += [{"Measure": elem} for elem in range(len(measures))]

grid_layout_initial = make_initial_grid_layout(layout_types=layout_types)
grid_layout, set_grid_layout = solara.use_state(grid_layout_initial)

with solara.Sidebar():
with solara.Card("Controls", margin=1, elevation=2):
solara.InputText(
label="Seed",
value=reactive_seed,
continuous_update=True,
)
UserInputs(user_params, on_change=handle_change_model_params)
ModelController(model, play_interval, current_step, reset_counter)
solara.Button(label="Reseed", color="primary", on_click=do_reseed)
with solara.Card("Information", margin=1, elevation=2):
solara.Markdown(md_text=f"Step - {current_step}")

items = [
Card(
model,
measures,
agent_portrayal,
space_drawer,
dependencies,
color="white",
layout_type=layout_types[i],
if measures:
layout_types += [{"Measure": elem} for elem in range(len(measures))]

grid_layout_initial = make_initial_grid_layout(layout_types=layout_types)
grid_layout, set_grid_layout = solara.use_state(grid_layout_initial)

with solara.Sidebar():
with solara.Card("Controls", margin=1, elevation=2):
solara.InputText(
label="Seed",
value=reactive_seed,
continuous_update=True,
)
for i in range(len(layout_types))
]
solara.GridDraggable(
items=items,
grid_layout=grid_layout,
resizable=True,
draggable=True,
on_grid_layout=set_grid_layout,
UserInputs(user_params, on_change=handle_change_model_params)
ModelController(model, play_interval, current_step, reset_counter)
solara.Button(label="Reseed", color="primary", on_click=do_reseed)
with solara.Card("Information", margin=1, elevation=2):
solara.Markdown(md_text=f"Step - {current_step}")

items = [
Card(
model,
measures,
agent_portrayal,
space_drawer,
dependencies,
color="white",
layout_type=layout_types[i],
)

if ("ipykernel" in sys.argv[0]) or ("colab_kernel_launcher.py" in sys.argv[0]):
# When in Jupyter or Google Colab
render_in_jupyter()
else:
render_in_browser()
for i in range(len(layout_types))
]
solara.GridDraggable(
items=items,
grid_layout=grid_layout,
resizable=True,
draggable=True,
on_grid_layout=set_grid_layout,
)


@solara.component
Expand Down