datatable.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """Table components."""
  2. from typing import Any, List
  3. from reflex.components.component import Component
  4. from reflex.components.tags import Tag
  5. from reflex.utils import format, imports, types
  6. from reflex.vars import BaseVar, ComputedVar, ImportVar, 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. alias = "DataTableGrid"
  14. # The data to display. Either a list of lists or a pandas dataframe.
  15. data: Any
  16. # The list of columns to display. Required if data is a list and should not be provided
  17. # if the data field is a dataframe
  18. columns: Var[List]
  19. # Enable a search bar.
  20. search: Var[bool]
  21. # Enable sorting on columns.
  22. sort: Var[bool]
  23. # Enable resizable columns.
  24. resizable: Var[bool]
  25. # Enable pagination.
  26. pagination: Var[bool]
  27. @classmethod
  28. def create(cls, *children, **props):
  29. """Create a datatable component.
  30. Args:
  31. *children: The children of the component.
  32. **props: The props to pass to the component.
  33. Returns:
  34. The datatable component.
  35. Raises:
  36. ValueError: If a pandas dataframe is passed in and columns are also provided.
  37. """
  38. data = props.get("data")
  39. columns = props.get("columns")
  40. # The annotation should be provided if data is a computed var. We need this to know how to
  41. # render pandas dataframes.
  42. if isinstance(data, ComputedVar) and data.type_ == Any:
  43. raise ValueError(
  44. "Annotation of the computed var assigned to the data field should be provided."
  45. )
  46. if columns and isinstance(columns, ComputedVar) and columns.type_ == Any:
  47. raise ValueError(
  48. "Annotation of the computed var assigned to the column field should be provided."
  49. )
  50. # If data is a pandas dataframe and columns are provided throw an error.
  51. if (
  52. types.is_dataframe(type(data))
  53. or (isinstance(data, Var) and types.is_dataframe(data.type_))
  54. ) and props.get("columns"):
  55. raise ValueError(
  56. "Cannot pass in both a pandas dataframe and columns to the data_table component."
  57. )
  58. # If data is a list and columns are not provided, throw an error
  59. if (
  60. (isinstance(data, Var) and types._issubclass(data.type_, List))
  61. or issubclass(type(data), List)
  62. ) and not props.get("columns"):
  63. raise ValueError(
  64. "column field should be specified when the data field is a list type"
  65. )
  66. # Create the component.
  67. return super().create(
  68. *children,
  69. **props,
  70. )
  71. def _get_imports(self) -> imports.ImportDict:
  72. return imports.merge_imports(
  73. super()._get_imports(),
  74. {"": {ImportVar(tag="gridjs/dist/theme/mermaid.css")}},
  75. )
  76. def _render(self) -> Tag:
  77. if isinstance(self.data, Var):
  78. self.columns = BaseVar(
  79. name=f"{self.data.name}.columns"
  80. if types.is_dataframe(self.data.type_)
  81. else f"{self.columns.name}",
  82. type_=List[Any],
  83. state=self.data.state,
  84. )
  85. self.data = BaseVar(
  86. name=f"{self.data.name}.data"
  87. if types.is_dataframe(self.data.type_)
  88. else f"{self.data.name}",
  89. type_=List[List[Any]],
  90. state=self.data.state,
  91. )
  92. # If given a pandas df break up the data and columns
  93. if types.is_dataframe(type(self.data)):
  94. self.columns = Var.create(list(self.data.columns.values.tolist())) # type: ignore
  95. self.data = Var.create(format.format_dataframe_values(self.data)) # type: ignore
  96. # Render the table.
  97. return super()._render()