refreshable_documentation.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from nicegui import ui
  2. from ...model import UiElementDocumentation
  3. class RefreshableDocumentation(UiElementDocumentation, element=ui.refreshable):
  4. def main_demo(self) -> None:
  5. import random
  6. numbers = []
  7. @ui.refreshable
  8. def number_ui() -> None:
  9. ui.label(', '.join(str(n) for n in sorted(numbers)))
  10. def add_number() -> None:
  11. numbers.append(random.randint(0, 100))
  12. number_ui.refresh()
  13. number_ui()
  14. ui.button('Add random number', on_click=add_number)
  15. def more(self) -> None:
  16. @self.demo('Refreshable UI with parameters', '''
  17. Here is a demo of how to use the refreshable decorator to create a UI that can be refreshed with different parameters.
  18. ''')
  19. def refreshable_with_parameters():
  20. from datetime import datetime
  21. import pytz
  22. @ui.refreshable
  23. def clock_ui(timezone: str):
  24. ui.label(f'Current time in {timezone}:')
  25. ui.label(datetime.now(tz=pytz.timezone(timezone)).strftime('%H:%M:%S'))
  26. clock_ui('Europe/Berlin')
  27. ui.button('Refresh', on_click=clock_ui.refresh)
  28. ui.button('Refresh for New York', on_click=lambda: clock_ui.refresh('America/New_York'))
  29. ui.button('Refresh for Tokyo', on_click=lambda: clock_ui.refresh('Asia/Tokyo'))
  30. @self.demo('Refreshable UI for input validation', '''
  31. Here is a demo of how to use the refreshable decorator to give feedback about the validity of user input.
  32. ''')
  33. def input_validation():
  34. import re
  35. pwd = ui.input('Password', password=True, on_change=lambda: show_info.refresh())
  36. rules = {
  37. 'Lowercase letter': lambda s: re.search(r'[a-z]', s),
  38. 'Uppercase letter': lambda s: re.search(r'[A-Z]', s),
  39. 'Digit': lambda s: re.search(r'\d', s),
  40. 'Special character': lambda s: re.search(r"[!@#$%^&*(),.?':{}|<>]", s),
  41. 'min. 8 characters': lambda s: len(s) >= 8,
  42. }
  43. @ui.refreshable
  44. def show_info():
  45. for rule, check in rules.items():
  46. with ui.row().classes('items-center gap-2'):
  47. if check(pwd.value or ''):
  48. ui.icon('done', color='green')
  49. ui.label(rule).classes('text-xs text-green strike-through')
  50. else:
  51. ui.icon('radio_button_unchecked', color='red')
  52. ui.label(rule).classes('text-xs text-red')
  53. show_info()
  54. @self.demo('Refreshable UI with reactive state', '''
  55. You can create reactive state variables with the `ui.state` function, like `count` and `color` in this demo.
  56. They can be used like normal variables for creating UI elements like the `ui.label`.
  57. Their corresponding setter functions can be used to set new values, which will automatically refresh the UI.
  58. ''')
  59. def reactive_state():
  60. @ui.refreshable
  61. def counter(name: str):
  62. with ui.card():
  63. count, set_count = ui.state(0)
  64. color, set_color = ui.state('black')
  65. ui.label(f'{name} = {count}').classes(f'text-{color}')
  66. ui.button(f'{name} += 1', on_click=lambda: set_count(count + 1))
  67. ui.select(['black', 'red', 'green', 'blue'],
  68. value=color, on_change=lambda e: set_color(e.value))
  69. with ui.row():
  70. counter('A')
  71. counter('B')