aggrid_documentation.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from nicegui import ui
  2. from ..documentation_tools import text_demo
  3. def main_demo() -> None:
  4. grid = ui.aggrid({
  5. 'columnDefs': [
  6. {'headerName': 'Name', 'field': 'name'},
  7. {'headerName': 'Age', 'field': 'age'},
  8. ],
  9. 'rowData': [
  10. {'name': 'Alice', 'age': 18},
  11. {'name': 'Bob', 'age': 21},
  12. {'name': 'Carol', 'age': 42},
  13. ],
  14. 'rowSelection': 'multiple',
  15. }).classes('max-h-40')
  16. def update():
  17. grid.options['rowData'][0]['age'] += 1
  18. grid.update()
  19. ui.button('Update', on_click=update)
  20. ui.button('Select all', on_click=lambda: grid.call_api_method('selectAll'))
  21. def more() -> None:
  22. @text_demo('Select AG Grid Rows', '''
  23. You can add checkboxes to grid cells to allow the user to select single or multiple rows.
  24. To retrieve the currently selected rows, use the `get_selected_rows` method.
  25. This method returns a list of rows as dictionaries.
  26. If `rowSelection` is set to `'single'` or to get the first selected row,
  27. you can also use the `get_selected_row` method.
  28. This method returns a single row as a dictionary or `None` if no row is selected.
  29. See the [AG Grid documentation](https://www.ag-grid.com/javascript-data-grid/row-selection/#example-single-row-selection) for more information.
  30. ''')
  31. def aggrid_with_selectable_rows():
  32. grid = ui.aggrid({
  33. 'columnDefs': [
  34. {'headerName': 'Name', 'field': 'name', 'checkboxSelection': True},
  35. {'headerName': 'Age', 'field': 'age'},
  36. ],
  37. 'rowData': [
  38. {'name': 'Alice', 'age': 18},
  39. {'name': 'Bob', 'age': 21},
  40. {'name': 'Carol', 'age': 42},
  41. ],
  42. 'rowSelection': 'multiple',
  43. }).classes('max-h-40')
  44. async def output_selected_rows():
  45. rows = await grid.get_selected_rows()
  46. if rows:
  47. for row in rows:
  48. ui.notify(f"{row['name']}, {row['age']}")
  49. else:
  50. ui.notify('No rows selected.')
  51. async def output_selected_row():
  52. row = await grid.get_selected_row()
  53. if row:
  54. ui.notify(f"{row['name']}, {row['age']}")
  55. else:
  56. ui.notify('No row selected!')
  57. ui.button('Output selected rows', on_click=output_selected_rows)
  58. ui.button('Output selected row', on_click=output_selected_row)
  59. @text_demo('Filter Rows using Mini Filters', '''
  60. You can add [mini filters](https://ag-grid.com/javascript-data-grid/filter-set-mini-filter/)
  61. to the header of each column to filter the rows.
  62. Note how the "agTextColumnFilter" matches individual characters, like "a" in "Alice" and "Carol",
  63. while the "agNumberColumnFilter" matches the entire number, like "18" and "21", but not "1".
  64. ''')
  65. def aggrid_with_minifilters():
  66. ui.aggrid({
  67. 'columnDefs': [
  68. {'headerName': 'Name', 'field': 'name', 'filter': 'agTextColumnFilter', 'floatingFilter': True},
  69. {'headerName': 'Age', 'field': 'age', 'filter': 'agNumberColumnFilter', 'floatingFilter': True},
  70. ],
  71. 'rowData': [
  72. {'name': 'Alice', 'age': 18},
  73. {'name': 'Bob', 'age': 21},
  74. {'name': 'Carol', 'age': 42},
  75. ],
  76. }).classes('max-h-40')
  77. @text_demo('AG Grid with Conditional Cell Formatting', '''
  78. This demo shows how to use [cellClassRules](https://www.ag-grid.com/javascript-grid-cell-styles/#cell-class-rules)
  79. to conditionally format cells based on their values.
  80. ''')
  81. def aggrid_with_conditional_cell_formatting():
  82. ui.aggrid({
  83. 'columnDefs': [
  84. {'headerName': 'Name', 'field': 'name'},
  85. {'headerName': 'Age', 'field': 'age', 'cellClassRules': {
  86. 'bg-red-300': 'x < 21',
  87. 'bg-green-300': 'x >= 21',
  88. }},
  89. ],
  90. 'rowData': [
  91. {'name': 'Alice', 'age': 18},
  92. {'name': 'Bob', 'age': 21},
  93. {'name': 'Carol', 'age': 42},
  94. ],
  95. })
  96. @text_demo('Create Grid from Pandas Dataframe', '''
  97. You can create an AG Grid from a Pandas Dataframe using the `from_pandas` method.
  98. This method takes a Pandas Dataframe as input and returns an AG Grid.
  99. ''')
  100. def aggrid_from_pandas():
  101. import pandas as pd
  102. df = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4]})
  103. ui.aggrid.from_pandas(df).classes('max-h-40')
  104. @text_demo('Render columns as HTML', '''
  105. You can render columns as HTML by passing a list of column indices to the `html_columns` argument.
  106. ''')
  107. def aggrid_with_html_columns():
  108. ui.aggrid({
  109. 'columnDefs': [
  110. {'headerName': 'Name', 'field': 'name'},
  111. {'headerName': 'URL', 'field': 'url'},
  112. ],
  113. 'rowData': [
  114. {'name': 'Google', 'url': '<a href="https://google.com">https://google.com</a>'},
  115. {'name': 'Facebook', 'url': '<a href="https://facebook.com">https://facebook.com</a>'},
  116. ],
  117. }, html_columns=[1])
  118. @text_demo('Respond to an AG Grid event', '''
  119. All AG Grid events are passed through to NiceGUI via the AG Grid global listener.
  120. These events can be subscribed to using the `.on()` method.
  121. ''')
  122. def aggrid_with_html_columns():
  123. ui.aggrid({
  124. 'columnDefs': [
  125. {'headerName': 'Name', 'field': 'name'},
  126. {'headerName': 'Age', 'field': 'age'},
  127. ],
  128. 'rowData': [
  129. {'name': 'Alice', 'age': 18},
  130. {'name': 'Bob', 'age': 21},
  131. {'name': 'Carol', 'age': 42},
  132. ],
  133. }).on('cellClicked', lambda event: ui.notify(f'Cell value: {event.args["value"]}'))