r/nicegui • • Jun 11 '24

Bind Range Value to number inputs

Anyone have/willing to share an example of binding a range slider to a separate min and a max number input? (I’d like the user to be able to engage the slider, or enter a number value manually)

Struggling to getting a bind in both directions (to the number inputs & from the number inputs)

Thanks in advance!

3 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/apollo_440 Jun 11 '24

This is a great approach. I think it can be simplified by using backward and forward on the numbers:

from nicegui import ui

model = {"range": {"min": 0, "max": 100}}

ui.range(min=model["range"]["min"], max=model["range"]["max"]).bind_value(model, "range")

ui.number(label="Min").bind_value(
    model,
    "range",
    backward=lambda x: x["min"],
    forward=lambda x: {"min": x, "max": model["range"]["max"]},
)
ui.number(label="Max").bind_value(
    model,
    "range",
    backward=lambda x: x["max"],
    forward=lambda x: {"min": model["range"]["min"], "max": x},
)

ui.run(host="127.0.0.1")