aggrid.py 2.9 KB

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