datatable.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. # 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. @classmethod
  26. def create(cls, *children, **props):
  27. """Create a datatable component.
  28. Args:
  29. *children: The children of the component.
  30. **props: The props to pass to the component.
  31. Returns:
  32. The datatable component.
  33. Raises:
  34. ValueError: If a pandas dataframe is passed in and columns are also provided.
  35. """
  36. # If data is a pandas dataframe and columns are provided throw an error.
  37. if utils.is_dataframe(type(props.get("data"))) and props.get("columns"):
  38. raise ValueError(
  39. "Cannot pass in both a pandas dataframe and columns to the data_table component."
  40. )
  41. # Create the component.
  42. return super().create(
  43. *children,
  44. **props,
  45. )
  46. def _get_imports(self) -> ImportDict:
  47. return utils.merge_imports(
  48. super()._get_imports(), {"": {"gridjs/dist/theme/mermaid.css"}}
  49. )
  50. def _render(self) -> Tag:
  51. if utils.is_dataframe(type(self.data)):
  52. # If given a pandas df break up the data and columns
  53. self.columns = Var.create(list(self.data.columns.values.tolist())) # type: ignore
  54. self.data = Var.create(list(self.data.values.tolist())) # type: ignore
  55. return super()._render()