page_layout.py 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from nicegui import ui
  2. from ...model import SectionDocumentation
  3. from ..more.card_documentation import CardDocumentation
  4. from ..more.carousel_documentation import CarouselDocumentation
  5. from ..more.column_documentation import ColumnDocumentation
  6. from ..more.context_menu_documentation import ContextMenuDocumentation
  7. from ..more.dialog_documentation import DialogDocumentation
  8. from ..more.expansion_documentation import ExpansionDocumentation
  9. from ..more.grid_documentation import GridDocumentation
  10. from ..more.menu_documentation import MenuDocumentation
  11. from ..more.notify_documentation import NotifyDocumentation
  12. from ..more.pagination_documentation import PaginationDocumentation
  13. from ..more.row_documentation import RowDocumentation
  14. from ..more.scroll_area_documentation import ScrollAreaDocumentation
  15. from ..more.separator_documentation import SeparatorDocumentation
  16. from ..more.splitter_documentation import SplitterDocumentation
  17. from ..more.stepper_documentation import StepperDocumentation
  18. from ..more.tabs_documentation import TabsDocumentation
  19. from ..more.timeline_documentation import TimelineDocumentation
  20. class PageLayoutDocumentation(SectionDocumentation, title='Page *Layout*', name='page_layout'):
  21. def content(self) -> None:
  22. @self.demo('Auto-context', '''
  23. In order to allow writing intuitive UI descriptions, NiceGUI automatically tracks the context in which elements are created.
  24. This means that there is no explicit `parent` parameter.
  25. Instead the parent context is defined using a `with` statement.
  26. It is also passed to event handlers and timers.
  27. In the demo, the label "Card content" is added to the card.
  28. And because the `ui.button` is also added to the card, the label "Click!" will also be created in this context.
  29. The label "Tick!", which is added once after one second, is also added to the card.
  30. This design decision allows for easily creating modular components that keep working after moving them around in the UI.
  31. For example, you can move label and button somewhere else, maybe wrap them in another container, and the code will still work.
  32. ''')
  33. def auto_context_demo():
  34. with ui.card():
  35. ui.label('Card content')
  36. ui.button('Add label', on_click=lambda: ui.label('Click!'))
  37. ui.timer(1.0, lambda: ui.label('Tick!'), once=True)
  38. self.intro(CardDocumentation())
  39. self.intro(ColumnDocumentation())
  40. self.intro(RowDocumentation())
  41. self.intro(GridDocumentation())
  42. @self.demo('Clear Containers', '''
  43. To remove all elements from a row, column or card container, use can call
  44. ```py
  45. container.clear()
  46. ```
  47. Alternatively, you can remove individual elements by calling
  48. - `container.remove(element: Element)`,
  49. - `container.remove(index: int)`, or
  50. - `element.delete()`.
  51. ''')
  52. def clear_containers_demo():
  53. container = ui.row()
  54. def add_face():
  55. with container:
  56. ui.icon('face')
  57. add_face()
  58. ui.button('Add', on_click=add_face)
  59. ui.button('Remove', on_click=lambda: container.remove(0) if list(container) else None)
  60. ui.button('Clear', on_click=container.clear)
  61. self.intro(ExpansionDocumentation())
  62. self.intro(ScrollAreaDocumentation())
  63. self.intro(SeparatorDocumentation())
  64. self.intro(SplitterDocumentation())
  65. self.intro(TabsDocumentation())
  66. self.intro(StepperDocumentation())
  67. self.intro(TimelineDocumentation())
  68. self.intro(CarouselDocumentation())
  69. self.intro(PaginationDocumentation())
  70. self.intro(MenuDocumentation())
  71. self.intro(ContextMenuDocumentation())
  72. @self.demo('Tooltips', '''
  73. Simply call the `tooltip(text:str)` method on UI elements to provide a tooltip.
  74. For more artistic control you can nest tooltip elements and apply props, classes and styles.
  75. ''')
  76. def tooltips_demo():
  77. ui.label('Tooltips...').tooltip('...are shown on mouse over')
  78. with ui.button(icon='thumb_up'):
  79. ui.tooltip('I like this').classes('bg-green')
  80. self.intro(NotifyDocumentation())
  81. self.intro(DialogDocumentation())