Skip to main content

Agent-based model visualization toolkit - Python bindings

Project description

Tensnap Python Bindings

Python bindings for the Tensnap protocol v0.2 runtime.

Installation

pip install tensnap

Quick Start

import asyncio

from tensnap import SimulationScenario
from tensnap.bindings import (
    BindParametersConfig,
    agent,
    agent_layer,
    chart,
    env,
    grid_layer,
)


@agent(x="position[0]", y="position[1]")
class Bird:
    def __init__(self, bird_id: int, position: tuple[int, int]):
        self.id = bird_id
        self.position = position


@grid_layer(width="width", height="height")
@agent_layer("birds", item_iterable_projector="birds")
@env(id="main")
class Aviary:
    def __init__(self):
        self.width = 20
        self.height = 10
        self.birds = [Bird(1, (2, 3)), Bird(2, (4, 5))]

    def step(self) -> None:
        for bird in self.birds:
            x, y = bird.position
            bird.position = (x + 1, y)

    @chart("population", "Population")
    def population(self) -> int:
        return len(self.birds)


class Config:
    speed = 1.0


scenario = SimulationScenario(port=8765)
model = Aviary()
config = Config()

scenario.add_environment(model)
scenario.add_parameters(config, BindParametersConfig(exclude="^_"))
scenario.add_charts(model)


async def main() -> None:
    await scenario.register_model_handler(model_step=model.step)
    await scenario.run()


if __name__ == "__main__":
    asyncio.run(main())

tensnap.bindings is the unified attach/readback surface for Python bindings. Use it for decorators such as env, grid_layer, and agent_layer, and for readback helpers such as environment_binding, layer_bindings, and bindings.

SimulationScenario registers the renderer-driven built-in actions start, step, and reset during construction. The initial synchronized state is always time 0, and the first simulated tick emitted by start or step is 1. If you need extra actions, attach them with @action(...) and register them with scenario.add_actions(target).

register_model_handler(model_init=None, model_step=None, model_reset=None) lets you keep reset distinct from init. If model_reset is omitted, the default handler falls back to model_init.

Examples

Example simulations are located in the repository root:

  • examples/python/ - Standard Python examples (flock, hk, sirs)
  • examples/python_mesa/ - Mesa-based examples (cgol, sugarscape, mushroom)

Documentation

Full documentation: https://github.com/billstark001/tensnap

License

See LICENSE file in the repository root.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tensnap-0.2.1.tar.gz (54.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tensnap-0.2.1-py3-none-any.whl (51.6 kB view details)

Uploaded Python 3

File details

Details for the file tensnap-0.2.1.tar.gz.

File metadata

  • Download URL: tensnap-0.2.1.tar.gz
  • Upload date:
  • Size: 54.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tensnap-0.2.1.tar.gz
Algorithm Hash digest
SHA256 0efa34d992bd578085ee1f944fd2b2083864c6fabb2951e2723f32236dfaf557
MD5 aa80ef4975b53448c4883cb103859be6
BLAKE2b-256 22dffede333d35c3cad9ac71a886c9ff7c35ff0d5434759eaa1d48ed6690db64

See more details on using hashes here.

Provenance

The following attestation bundles were made for tensnap-0.2.1.tar.gz:

Publisher: python-publish.yml on billstark001/tensnap

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tensnap-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: tensnap-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 51.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tensnap-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8f113b275c1693ac1ca6e12b3f74f9fce6355e66de3da4c6aed455d7c052e249
MD5 50980e80bcd3a651e927f7de739f4a63
BLAKE2b-256 8d020f50d0e88388b4a0a1d6b12b11cb88d281529efedb69468d547b3a4c5192

See more details on using hashes here.

Provenance

The following attestation bundles were made for tensnap-0.2.1-py3-none-any.whl:

Publisher: python-publish.yml on billstark001/tensnap

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page