1
0

bindings_documentation.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. def main_demo() -> None:
  2. class Demo:
  3. def __init__(self):
  4. self.number = 1
  5. demo = Demo()
  6. v = ui.checkbox('visible', value=True)
  7. with ui.column().bind_visibility_from(v, 'value'):
  8. ui.slider(min=1, max=3).bind_value(demo, 'number')
  9. ui.toggle({1: 'A', 2: 'B', 3: 'C'}).bind_value(demo, 'number')
  10. ui.number().bind_value(demo, 'number')
  11. def more() -> None:
  12. @text_demo('Bind to dictionary', '''description''')
  13. def bind_dictionary():
  14. dictionary = {'name': 'NiceGUI', 'age': 2}
  15. with ui.grid(columns=2):
  16. ui.label('Name:')
  17. ui.label().bind_text_from(dictionary, 'name')
  18. ui.label('Age:')
  19. ui.label().bind_text_from(dictionary, 'age')
  20. def nicegui_older():
  21. dictionary['age'] += 1
  22. ui.button('Make NiceGUI older!', on_click=nicegui_older)
  23. @text_demo('Bind to variable', '''description, include link to datepicker example''')
  24. def bind_variable():
  25. today_date = '1970-01-01'
  26. def notify_date():
  27. global today_date
  28. ui.notify(f'Today is: {today_date}')
  29. with ui.input('Date') as date:
  30. with date.add_slot('append'):
  31. ui.icon('edit_calendar').on('click', lambda: menu.open()).classes('cursor-pointer')
  32. with ui.menu() as menu:
  33. ui.date(on_change=notify_date).bind_value(date).bind_value(globals(), 'today_date')