bare.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. """A bare component."""
  2. from __future__ import annotations
  3. from typing import Any, Iterator
  4. from reflex.components.component import Component, ComponentStyle
  5. from reflex.components.tags import Tag
  6. from reflex.components.tags.tagless import Tagless
  7. from reflex.utils.imports import ParsedImportDict
  8. from reflex.vars import BooleanVar, ObjectVar, Var
  9. from reflex.vars.base import VarData, get_var_caching, set_var_caching
  10. class Bare(Component):
  11. """A component with no tag."""
  12. contents: Var[Any]
  13. @classmethod
  14. def create(cls, contents: Any) -> Component:
  15. """Create a Bare component, with no tag.
  16. Args:
  17. contents: The contents of the component.
  18. Returns:
  19. The component.
  20. """
  21. if isinstance(contents, Var):
  22. return cls(contents=contents)
  23. else:
  24. contents = str(contents) if contents is not None else ""
  25. return cls(contents=contents) # type: ignore
  26. def _get_all_hooks_internal(self) -> dict[str, VarData | None]:
  27. """Include the hooks for the component.
  28. Returns:
  29. The hooks for the component.
  30. """
  31. hooks = super()._get_all_hooks_internal()
  32. if isinstance(self.contents, Var):
  33. var_data = self.contents._get_all_var_data()
  34. if var_data:
  35. for component in var_data.components:
  36. hooks |= component._get_all_hooks_internal()
  37. return hooks
  38. def _get_all_hooks(self) -> dict[str, VarData | None]:
  39. """Include the hooks for the component.
  40. Returns:
  41. The hooks for the component.
  42. """
  43. hooks = super()._get_all_hooks()
  44. if isinstance(self.contents, Var):
  45. var_data = self.contents._get_all_var_data()
  46. if var_data:
  47. for component in var_data.components:
  48. hooks |= component._get_all_hooks()
  49. return hooks
  50. def _get_all_imports(self, collapse: bool = False) -> ParsedImportDict:
  51. """Include the imports for the component.
  52. Args:
  53. collapse: Whether to collapse the imports.
  54. Returns:
  55. The imports for the component.
  56. """
  57. imports = super()._get_all_imports(collapse=collapse)
  58. if isinstance(self.contents, Var):
  59. var_data = self.contents._get_all_var_data()
  60. if var_data:
  61. imports |= {k: list(v) for k, v in var_data.imports}
  62. return imports
  63. def _get_all_dynamic_imports(self) -> set[str]:
  64. """Get dynamic imports for the component.
  65. Returns:
  66. The dynamic imports.
  67. """
  68. dynamic_imports = super()._get_all_dynamic_imports()
  69. if isinstance(self.contents, Var):
  70. var_data = self.contents._get_all_var_data()
  71. if var_data:
  72. for component in var_data.components:
  73. dynamic_imports |= component._get_all_dynamic_imports()
  74. return dynamic_imports
  75. def _get_all_custom_code(self) -> set[str]:
  76. """Get custom code for the component.
  77. Returns:
  78. The custom code.
  79. """
  80. custom_code = super()._get_all_custom_code()
  81. if isinstance(self.contents, Var):
  82. var_data = self.contents._get_all_var_data()
  83. if var_data:
  84. for component in var_data.components:
  85. custom_code |= component._get_all_custom_code()
  86. return custom_code
  87. def _get_all_app_wrap_components(self) -> dict[tuple[int, str], Component]:
  88. """Get the components that should be wrapped in the app.
  89. Returns:
  90. The components that should be wrapped in the app.
  91. """
  92. app_wrap_components = super()._get_all_app_wrap_components()
  93. if isinstance(self.contents, Var):
  94. var_data = self.contents._get_all_var_data()
  95. if var_data:
  96. for component in var_data.components:
  97. if isinstance(component, Component):
  98. app_wrap_components |= component._get_all_app_wrap_components()
  99. return app_wrap_components
  100. def _get_all_refs(self) -> set[str]:
  101. """Get the refs for the children of the component.
  102. Returns:
  103. The refs for the children.
  104. """
  105. refs = super()._get_all_refs()
  106. if isinstance(self.contents, Var):
  107. var_data = self.contents._get_all_var_data()
  108. if var_data:
  109. for component in var_data.components:
  110. refs |= component._get_all_refs()
  111. return refs
  112. def _render(self) -> Tag:
  113. if isinstance(self.contents, Var):
  114. if isinstance(self.contents, (BooleanVar, ObjectVar)):
  115. return Tagless(contents=f"{{{self.contents.to_string()!s}}}")
  116. return Tagless(contents=f"{{{self.contents!s}}}")
  117. return Tagless(contents=str(self.contents))
  118. def _add_style_recursive(
  119. self, style: ComponentStyle, theme: Component | None = None
  120. ) -> Component:
  121. """Add style to the component and its children.
  122. Args:
  123. style: The style to add.
  124. theme: The theme to add.
  125. Returns:
  126. The component with the style added.
  127. """
  128. new_self = super()._add_style_recursive(style, theme)
  129. if isinstance(self.contents, Var):
  130. var_data = self.contents._get_all_var_data()
  131. if var_data:
  132. for component in var_data.components:
  133. if isinstance(component, Component):
  134. component._add_style_recursive(style, theme)
  135. if get_var_caching():
  136. set_var_caching(False)
  137. str(new_self)
  138. set_var_caching(True)
  139. return new_self
  140. def _get_vars(
  141. self, include_children: bool = False, ignore_ids: set[int] | None = None
  142. ) -> Iterator[Var]:
  143. """Walk all Vars used in this component.
  144. Args:
  145. include_children: Whether to include Vars from children.
  146. ignore_ids: The ids to ignore.
  147. Yields:
  148. The contents if it is a Var, otherwise nothing.
  149. """
  150. yield self.contents