Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docstring for jupyterviz make_user_input that documents supported inputs #1784

Merged
merged 5 commits into from
Sep 1, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions mesa/experimental/jupyter_viz.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,28 +133,37 @@ def function(viz):
return function


def make_user_input(user_input, k, v):
if v["type"] == "SliderInt":
def make_user_input(user_input, options):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially included the name ("k") because I figured it might be used in the future. Looking at the code again, I think the name can be used as a fallback label instead of the current generic "label".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok, that makes sense; I'll revise.

Should the method throw an exception if the type is not supported? If so, does ValueError seem appropriate to you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rht updated with name fallback logic, exception for unsupported type, and preliminary unit test for this method

"""Initialize a user input for configurable model parameters.
Currently supports :class:`solara.SliderInt`, :class:`solara.SliderFloat`,
and :class:`solara.Select`.

Args:
user_input: :class:`solara.reactive` object with initial value
options: dictionary with options for the input, including label,
min and max values, and other fields specific to the input type.
"""
if options["type"] == "SliderInt":
solara.SliderInt(
v.get("label", "label"),
options.get("label", "label"),
value=user_input,
min=v.get("min"),
max=v.get("max"),
step=v.get("step"),
min=options.get("min"),
max=options.get("max"),
step=options.get("step"),
)
elif v["type"] == "SliderFloat":
elif options["type"] == "SliderFloat":
solara.SliderFloat(
v.get("label", "label"),
options.get("label", "label"),
value=user_input,
min=v.get("min"),
max=v.get("max"),
step=v.get("step"),
min=options.get("min"),
max=options.get("max"),
step=options.get("step"),
)
elif v["type"] == "Select":
elif options["type"] == "Select":
solara.Select(
v.get("label", "label"),
value=v.get("value"),
values=v.get("values"),
options.get("label", "label"),
value=options.get("value"),
values=options.get("values"),
)


Expand All @@ -164,10 +173,10 @@ def MesaComponent(viz, space_drawer=None, play_interval=400):

# 1. User inputs
user_inputs = {}
for k, v in viz.model_params_input.items():
user_input = solara.use_reactive(v["value"])
user_inputs[k] = user_input.value
make_user_input(user_input, k, v)
for name, options in viz.model_params_input.items():
user_input = solara.use_reactive(options["value"])
user_inputs[name] = user_input.value
make_user_input(user_input, options)

# 2. Model
def make_model():
Expand Down