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,
agent,
agent_layer,
chart,
env,
grid_layer,
params,
)
@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()
@agent_layer("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)
@params(include=["speed"])
class Config:
speed = 1.0
scenario = SimulationScenario(port=8765)
model = Aviary()
config = Config()
scenario.add_all(model)
scenario.add_all(config)
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.
Layer and item binding fields now support these common forms:
- omitted fields,
None, orauto()for one-time same-name discovery from the class or initialized instance - selector strings such as
x="pos[0]"oritem_iterable_projector="agents"when the source path differs from the output field or layer id - literal values such as
coord_offset="float"oricon="circle"; arbitrary strings still default to selectors unless they match a known literal value, CSS-like string such as"#3498db", or JSON-like string - explicit helpers such as
attr("pos[0]"),value("red"),auto(), andskip() - direct callables such as
size=lambda agent: agent.radius
None/auto() field discovery is resolved once when instances initialize or when layer_bindings(instance) materializes an instance-specific binding, so fields assigned in __init__ are picked up without scanning during projection:
from tensnap import agent, agent_layer, grid_layer, value
@agent(x="position[0]", y="position[1]", color=value("red"))
class Bird:
def __init__(self, bird_id: int, position: tuple[int, int]):
self.id = bird_id
self.position = position
@grid_layer()
@agent_layer("birds")
class Aviary:
def __init__(self):
self.width = 20
self.height = 10
self.birds = [Bird(1, (2, 3))]
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. For most targets, use scenario.add_all(target) to register available environment/layer, chart, and action bindings. Parameters are opt-in with @params(...) or an explicit config.
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.
add_all(..., dry_run=True) and the targeted add_* dry-run modes report registry ids without mutating the scenario. Mesa lifecycle helpers use this to unregister and rebuild model-owned registrations cleanly.
For Mesa models, prefer BoundModelReinitializer from tensnap or tensnap.bindings.lifecycle. Use bind_kwargs(...) to expose constructor keyword arguments as resettable parameters when needed. When a constructor kwarg and a model-owned parameter publish the same source field during register_model(), the model parameter keeps UI ownership and the reinitializer aliases its reset value from the model field instead of registering a duplicate kwarg parameter. configure_reinit(...) now applies the default Mesa cleanup automatically when no explicit cleanup callback is provided.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tensnap-0.2.4.tar.gz.
File metadata
- Download URL: tensnap-0.2.4.tar.gz
- Upload date:
- Size: 81.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
017c97e9fc5dab5256813a65852ffccd76a95119dd53625d4fff751561b716f8
|
|
| MD5 |
d50cea4a506c688521e139dfb5757c39
|
|
| BLAKE2b-256 |
244309f17f3eb89157dce31b72ccc86d5c4cfc0782f7dfce59b24eb5771bedd7
|
Provenance
The following attestation bundles were made for tensnap-0.2.4.tar.gz:
Publisher:
python-publish.yml on billstark001/tensnap
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tensnap-0.2.4.tar.gz -
Subject digest:
017c97e9fc5dab5256813a65852ffccd76a95119dd53625d4fff751561b716f8 - Sigstore transparency entry: 2130495566
- Sigstore integration time:
-
Permalink:
billstark001/tensnap@a19c037ef542428d788289e39770a9c9809829de -
Branch / Tag:
refs/tags/py-v0.2.4 - Owner: https://github.com/billstark001
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@a19c037ef542428d788289e39770a9c9809829de -
Trigger Event:
push
-
Statement type:
File details
Details for the file tensnap-0.2.4-py3-none-any.whl.
File metadata
- Download URL: tensnap-0.2.4-py3-none-any.whl
- Upload date:
- Size: 75.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c852f45d8fc37d5f43287489f20b342c78109d91686099c9b83ea8350b98a08
|
|
| MD5 |
26938db1f321afd7b6f6a4c749a7cfed
|
|
| BLAKE2b-256 |
849b1a315c4b883c65bf06264e81bc0361f080677ec3f35997aa377b0caa43da
|
Provenance
The following attestation bundles were made for tensnap-0.2.4-py3-none-any.whl:
Publisher:
python-publish.yml on billstark001/tensnap
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tensnap-0.2.4-py3-none-any.whl -
Subject digest:
9c852f45d8fc37d5f43287489f20b342c78109d91686099c9b83ea8350b98a08 - Sigstore transparency entry: 2130495616
- Sigstore integration time:
-
Permalink:
billstark001/tensnap@a19c037ef542428d788289e39770a9c9809829de -
Branch / Tag:
refs/tags/py-v0.2.4 - Owner: https://github.com/billstark001
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@a19c037ef542428d788289e39770a9c9809829de -
Trigger Event:
push
-
Statement type: