datatable.py 1.2 KB

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