aggrid.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from __future__ import annotations
  2. from pathlib import Path
  3. from typing import Dict, List, Optional, cast
  4. from ..dependencies import register_library, register_vue_component
  5. from ..element import Element
  6. from ..functions.javascript import run_javascript
  7. register_vue_component('aggrid', Path(__file__).parent / 'aggrid.js')
  8. library_name = register_library(Path('aggrid') / 'ag-grid-community.min.js')
  9. class AgGrid(Element):
  10. def __init__(self, options: Dict, *, html_columns: List[int] = [], theme: str = 'balham') -> None:
  11. """AG Grid
  12. An element to create a grid using `AG Grid <https://www.ag-grid.com/>`_.
  13. The `call_api_method` method can be used to call an AG Grid API method.
  14. :param options: dictionary of AG Grid options
  15. :param html_columns: list of columns that should be rendered as HTML (default: `[]`)
  16. :param theme: AG Grid theme (default: 'balham')
  17. """
  18. super().__init__('aggrid')
  19. self._props['options'] = options
  20. self._props['html_columns'] = html_columns
  21. self._classes = ['nicegui-aggrid', f'ag-theme-{theme}']
  22. self.use_component('aggrid')
  23. self.use_library(library_name)
  24. @staticmethod
  25. def from_pandas(df: 'pandas.DataFrame', *, theme: str = 'balham') -> AgGrid:
  26. """Create an AG Grid from a Pandas DataFrame.
  27. :param df: Pandas DataFrame
  28. :param theme: AG Grid theme (default: 'balham')
  29. :return: AG Grid
  30. """
  31. return AgGrid({
  32. 'columnDefs': [{'field': col} for col in df.columns],
  33. 'rowData': df.to_dict('records'),
  34. }, theme=theme)
  35. @property
  36. def options(self) -> Dict:
  37. return self._props['options']
  38. def update(self) -> None:
  39. super().update()
  40. self.run_method('update_grid')
  41. def call_api_method(self, name: str, *args) -> None:
  42. """Call an AG Grid API method.
  43. See `AG Grid API <https://www.ag-grid.com/javascript-data-grid/grid-api/>`_ for a list of methods.
  44. :param name: name of the method
  45. :param args: arguments to pass to the method
  46. """
  47. self.run_method('call_api_method', name, *args)
  48. async def get_selected_rows(self) -> List[Dict]:
  49. """Get the currently selected rows.
  50. This method is especially useful when the grid is configured with ``rowSelection: 'multiple'``.
  51. See `AG Grid API <https://www.ag-grid.com/javascript-data-grid/row-selection/#reference-selection-getSelectedRows>`_ for more information.
  52. :return: list of selected row data
  53. """
  54. result = await run_javascript(f'return getElement({self.id}).gridOptions.api.getSelectedRows();')
  55. return cast(List[Dict], result)
  56. async def get_selected_row(self) -> Optional[Dict]:
  57. """Get the single currently selected row.
  58. This method is especially useful when the grid is configured with ``rowSelection: 'single'``.
  59. :return: row data of the first selection if any row is selected, otherwise `None`
  60. """
  61. rows = await self.get_selected_rows()
  62. return rows[0] if rows else None