Skip to content

Support Scenarios in Visualization #3176

@EwoutH

Description

@EwoutH

Problem

Mesa now has first-class support for Scenarios (#3103, #3168, and updated example #3167), but SolaraViz doesn’t support them. This creates two distinct issues:

Issue 1: Parameter routing during reset
When users create sliders for scenario parameters (density, vision), clicking “Reset” fails because SolaraViz tries to pass these directly to MyModel.__init__(), which doesn’t accept them—they belong to the Scenario.

Issue 2: Leveraging Scenario for auto-generated UI
Scenarios already define typed parameters with defaults. How can we use this to create a clean, minimal-duplication API for generating UI controls? The challenge: Scenarios define defaults but not min/max ranges for sliders.

Current situation:

class MyScenario(Scenario):
    density: float = 0.7
    vision: int = 7

class MyModel(Model):
    def __init__(self, height=40, width=40, scenario: MyScenario | None = None):
        super().__init__(scenario=scenario)

Possible solutions

Option 1: Add scenario_params argument

page = SolaraViz(
    model,
    renderer,
    model_params={"height": 40, "width": 40},
    scenario_params={
        "density": Slider(...),
        "vision": Slider(...),
    }
)

Mesa changes needed:

  • Add scenario_params parameter to SolaraViz()
  • Update ModelController.do_reset() to reconstruct Scenario from parameters
  • Render scenario params in separate UI section

Pros: Clear, explicit, easy to understand
Cons: More verbose, requires manual specification

Option 2: Auto-detect from type hints

# SolaraViz introspects model.scenario and auto-generates UI
page = SolaraViz(model, renderer)

# Optional customization
page = SolaraViz(model, renderer, scenario_params={"density": Slider(...)})

Mesa changes needed:

  • Introspect model.scenario to get Scenario class
  • Use get_type_hints() and _scenario_defaults to auto-generate controls
  • Allow overrides via scenario_params

Pros: Minimal user code, leverages existing type hints
Cons: Less explicit, auto-generated ranges might not fit

Option 3: Something else?

Other nice ideas for a clean API?

Discussion Questions

  1. How would the user API look?
  2. Should scenario parameters appear in a separate UI section?
  3. How do we handle the rng parameter that scenarios own?

Implementation areas

Regardless of option chosen:

  • solara_viz.py: Update SolaraViz, ModelCreator, ModelController
  • Documentation: Add examples and migration guide

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions