datatable.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Table components."""
  2. from typing import Any, List
  3. from pynecone import utils
  4. from pynecone.components.component import Component, ImportDict
  5. from pynecone.components.tags import Tag
  6. from pynecone.var import Var
  7. class Gridjs(Component):
  8. """A component that wraps a nivo bar component."""
  9. library = "gridjs-react"
  10. class DataTable(Gridjs):
  11. """A data table component."""
  12. tag = "Grid"
  13. df: Var[Any]
  14. # The data to display. EIther a list of lists or a pandas dataframe.
  15. data: Any
  16. # The columns to display.
  17. columns: Var[List]
  18. # Enable a search bar.
  19. search: Var[bool]
  20. # Enable sorting on columns.
  21. sort: Var[bool]
  22. # Enable resizable columns.
  23. resizable: Var[bool]
  24. # Enable pagination.
  25. pagination: Var[bool]
  26. def _get_imports(self) -> ImportDict:
  27. return utils.merge_imports(
  28. super()._get_imports(), {"": {"gridjs/dist/theme/mermaid.css"}}
  29. )
  30. def _render(self) -> Tag:
  31. if utils.is_dataframe(type(self.data)):
  32. self.columns = Var.create(list(self.data.columns.values.tolist())) # type: ignore
  33. self.data = Var.create(list(self.data.values.tolist())) # type: ignore
  34. if isinstance(self.df, Var):
  35. self.columns = self.df["columns"]
  36. self.data = self.df["data"]
  37. return super()._render()