Recommendation on passing input values in #76
-
What is a recommended way of passing the input values into a handler? Is it a callback style only? def handle_inputs(*inputs):
print(inputs)
with ui.row():
ui.input()
ui.input()
ui.input()
...
ui.button(on_click=handle_inputs) # I'd like to pass all the final input values in at once here |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Well, my recommendation depends. The most trivial way would be to pass all values: def handle_inputs(a, b, c):
print(a, b, c)
a = ui.input()
b = ui.input()
c = ui.input()
ui.button(on_click=lambda: handle_inputs(a.value, b.value, c.value)) If the list of inputs gets too long, you could access the UI elements directly: def handle_more_inputs():
print(d.value, e.value, f.value)
d = ui.input()
e = ui.input()
f = ui.input()
ui.button(on_click=handle_more_inputs) But maybe it's better to pack all data into a model class: @dataclass
class Model:
g: str = ''
h: str = ''
i: str = ''
def handle_model(model):
print(model.g, model.h, model.i)
model = Model()
ui.input().bind_value_to(model, 'g')
ui.input().bind_value_to(model, 'h')
ui.input().bind_value_to(model, 'i')
ui.button(on_click=lambda: handle_model(model)) In the examples above I dropped the def handle_form(x, y, z):
print(x, y, z)
with ui.row() as form:
ui.input()
ui.input()
ui.input()
ui.button(on_click=lambda: handle_form(*form.get_input_values())) What do you think? Did you have something completely different in mind? |
Beta Was this translation helpful? Give feedback.
Well, my recommendation depends. The most trivial way would be to pass all values:
If the list of inputs gets too long, you could access the UI elements directly:
But maybe it's better to pack all data into a model class: