datatable.py 4.0 KB

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