Skip to main content

Mesa Interactive

PyPi

Overview

Mesa Interactive is a specialized Python package designed to provide dynamic visualization and interaction capabilities for Mesa models. It extends the functionalities of the Mesa framework for agent-based modeling.

Quick Installation

To quickly install the package, open your terminal and run:

pip install mesa_interactive

Tutorial: Visualizing the Schelling Segregation Model

This section provides a step-by-step walkthrough to visualize a Schelling Segregation model using Mesa Interactive.

Prerequisites

Ensure that you have mesa-models installed. If not, run the following command:

pip install -U -e git+https://github.com/projectmesa/mesa-examples#egg=mesa-models

Importing Dependencies

Import the essential libraries as follows:

import solara
from mesa_models.schelling.model import Schelling

from mesa_interactive import MesaInteractive, slide, static
from mesa_interactive.components import create_chart, create_grid, create_markdown

Building Components

  1. Instructions: Add Markdown instructions. We use the static helper function, since it doesn't rely on model parameters.
Instructions = static(solara.Markdown("Click on an agent to switch its type."))

  1. GridView: Construct a grid view, enabling a switch_agent_type function on agent click.
def switch_agent_type(model, x, y):
    agent = model.grid.get_cell_list_contents([(x, y)])[0]
    agent.type = 1 - agent.type

GridView = create_grid(color="type", on_click=switch_agent_type)
  1. HappyCount: Display the number of satisfied agents via a Markdown component.
HappyCount = create_markdown(
    lambda model: f"**Happy Agents: {model.happy} of {len(model.schedule.agents)}**"
)
  1. HappyChart: Implement a time-series chart to track the number of happy agents.
HappyChart = create_chart(variables=["happy"], "Happy Agents")

Configuring Model Parameters

Define user-adjustable model parameters using default values and the slide helper functions. It works like the built-in range function but supports arbitrary steps and a default value.

model_params = {
    "width": 20,
    "height": 20,
    "density": slide(0, 1, 0.1, default=0.65),
    "minority_pc": slide(0, 1, 0.05, default=0.2),
    "homophily": slide(0, 8, 1, default=3),
}

Creating the Interactive Page

  1. Initialize the Mesa Interactive interface with the Schelling model, model parameters, and assembled components.
page = MesaInteractive(
    Schelling,
    model_params,
    components=[HappyCount, Instructions, GridView, HappyChart],
    name="Schelling",
    show_dataframe="model",
)
  1. To render your visulazation save your python file as app.py and from a terminal run
solara run app.py

Alternatively you can but everything in a Jupyter notebook and simply call

page

to show the visualization.

Now you have built a fully interactive visualization for the Schelling Segregation model!

Metadata

Release files for mesa-interactive 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mesa-interactive 0.0.5
File Size Uploaded
mesa_interactive-0.0.5.tar.gz 9.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mesa-interactive 0.0.5
File Interpreter ABI Platform
mesa_interactive-0.0.5-py3-none-any.whl Python 3 none any Details

Total release size: 18.9 kB

Release files / mesa_interactive-0.0.5.tar.gz

Download URL mesa_interactive-0.0.5.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
a06dc671e0c2912f15f271746741446b9ffc0d0d1bf8716e182efbba389f0ebe
BLAKE2b-256 checksum
How to use checksums
47bcaeb52eca36452dbab0829f786b88c81a0fc382fb0708626c40f5b9abbd4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / mesa_interactive-0.0.5-py3-none-any.whl

Download URL mesa_interactive-0.0.5-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a52bd1d9241b5e735f5222ccfeaa6d4769526b4f3debc36242b4373b27afe777
BLAKE2b-256 checksum
How to use checksums
a8d9c9653d5edf310bf5ae87dfd9f17dd9d15e2065273f3182a37adf67ea9ebd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

0.0.5 This release

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page