Skip to content

Commit 991a017

Browse files
rhttpike3
authored andcommitted
feat: Implement StaticText UserParam
1 parent 8000dfb commit 991a017

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

examples/wolf_sheep/wolf_sheep/server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def wolf_sheep_portrayal(agent):
4848
)
4949

5050
model_params = {
51+
# The following line is an example to showcase StaticText.
52+
"title": mesa.visualization.StaticText("Parameters:"),
5153
"grass": mesa.visualization.Checkbox("Grass Enabled", True),
5254
"grass_regrowth_time": mesa.visualization.Slider("Grass Regrowth Time", 20, 1, 50),
5355
"initial_sheep": mesa.visualization.Slider(

mesa/visualization/UserParam.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,18 @@ def value(self, value):
253253
"Selected choice value not in available choices, selected first choice from 'choices' list"
254254
)
255255
self._value = self.choices[0]
256+
257+
258+
class StaticText(UserParam):
259+
"""
260+
A non-input textbox for displaying model info.
261+
262+
Example:
263+
static_text = StaticText("This is a descriptive textbox")
264+
"""
265+
266+
def __init__(self, value=None):
267+
self.param_type = STATIC_TEXT
268+
self._value = value
269+
valid = isinstance(self.value, str)
270+
self.maybe_raise_error(valid)

tests/test_usersettableparam.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
from unittest import TestCase
2-
from mesa.visualization.UserParam import UserSettableParameter, Slider, Checkbox, Choice
2+
from mesa.visualization.UserParam import (
3+
UserSettableParameter,
4+
Slider,
5+
Checkbox,
6+
Choice,
7+
StaticText,
8+
)
39

410

511
class TestOption(TestCase):
@@ -20,6 +26,7 @@ def setUp(self):
2026
"slider", value=123, min_value=100, max_value=200
2127
)
2228
self.slider_option_standalone = Slider(value=123, min_value=100, max_value=200)
29+
self.static_text_option = StaticText("Hurr, Durr Im'a Sheep")
2330

2431
def test_number(self):
2532
assert self.number_option.value == 123
@@ -52,3 +59,6 @@ def test_slider(self):
5259
assert option.json["value"] == 200
5360
with self.assertRaises(ValueError):
5461
Slider()
62+
63+
def test_static_text(self):
64+
assert self.static_text_option.value == "Hurr, Durr Im'a Sheep"

0 commit comments

Comments
 (0)