1
0

bare.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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.config import PerformanceMode, environment
  8. from reflex.utils import console
  9. from reflex.utils.decorator import once
  10. from reflex.utils.imports import ParsedImportDict
  11. from reflex.vars import BooleanVar, ObjectVar, Var
  12. from reflex.vars.base import GLOBAL_CACHE, VarData
  13. from reflex.vars.sequence import LiteralStringVar
  14. @once
  15. def get_performance_mode():
  16. """Get the performance mode.
  17. Returns:
  18. The performance mode.
  19. """
  20. return environment.REFLEX_PERF_MODE.get()
  21. def validate_str(value: str):
  22. """Validate a string value.
  23. Args:
  24. value: The value to validate.
  25. Raises:
  26. ValueError: If the value is a Var and the performance mode is set to raise.
  27. """
  28. perf_mode = get_performance_mode()
  29. if perf_mode != PerformanceMode.OFF and value.startswith("reflex___state"):
  30. if perf_mode == PerformanceMode.WARN:
  31. console.warn(
  32. f"Output includes {value!s} which will be displayed as a string. If you are calling `str` on a Var, consider using .to_string() instead."
  33. )
  34. elif perf_mode == PerformanceMode.RAISE:
  35. raise ValueError(
  36. f"Output includes {value!s} which will be displayed as a string. If you are calling `str` on a Var, consider using .to_string() instead."
  37. )
  38. class Bare(Component):
  39. """A component with no tag."""
  40. contents: Var[Any]
  41. @classmethod
  42. def create(cls, contents: Any) -> Component:
  43. """Create a Bare component, with no tag.
  44. Args:
  45. contents: The contents of the component.
  46. Returns:
  47. The component.
  48. """
  49. if isinstance(contents, Var):
  50. if isinstance(contents, LiteralStringVar):
  51. validate_str(contents._var_value)
  52. return cls(contents=contents)
  53. else:
  54. if isinstance(contents, str):
  55. validate_str(contents)
  56. contents = str(contents) if contents is not None else ""
  57. return cls(contents=contents)
  58. def _get_all_hooks_internal(self) -> dict[str, VarData | None]:
  59. """Include the hooks for the component.
  60. Returns:
  61. The hooks for the component.
  62. """
  63. hooks = super()._get_all_hooks_internal()
  64. if isinstance(self.contents, Var):
  65. var_data = self.contents._get_all_var_data()
  66. if var_data:
  67. for component in var_data.components:
  68. hooks |= component._get_all_hooks_internal()
  69. return hooks
  70. def _get_all_hooks(self) -> dict[str, VarData | None]:
  71. """Include the hooks for the component.
  72. Returns:
  73. The hooks for the component.
  74. """
  75. hooks = super()._get_all_hooks()
  76. if isinstance(self.contents, Var):
  77. var_data = self.contents._get_all_var_data()
  78. if var_data:
  79. for component in var_data.components:
  80. hooks |= component._get_all_hooks()
  81. return hooks
  82. def _get_all_imports(self, collapse: bool = False) -> ParsedImportDict:
  83. """Include the imports for the component.
  84. Args:
  85. collapse: Whether to collapse the imports.
  86. Returns:
  87. The imports for the component.
  88. """
  89. imports = super()._get_all_imports(collapse=collapse)
  90. if isinstance(self.contents, Var):
  91. var_data = self.contents._get_all_var_data()
  92. if var_data:
  93. imports |= {k: list(v) for k, v in var_data.imports}
  94. return imports
  95. def _get_all_dynamic_imports(self) -> set[str]:
  96. """Get dynamic imports for the component.
  97. Returns:
  98. The dynamic imports.
  99. """
  100. dynamic_imports = super()._get_all_dynamic_imports()
  101. if isinstance(self.contents, Var):
  102. var_data = self.contents._get_all_var_data()
  103. if var_data:
  104. for component in var_data.components:
  105. dynamic_imports |= component._get_all_dynamic_imports()
  106. return dynamic_imports
  107. def _get_all_custom_code(self) -> set[str]:
  108. """Get custom code for the component.
  109. Returns:
  110. The custom code.
  111. """
  112. custom_code = super()._get_all_custom_code()
  113. if isinstance(self.contents, Var):
  114. var_data = self.contents._get_all_var_data()
  115. if var_data:
  116. for component in var_data.components:
  117. custom_code |= component._get_all_custom_code()
  118. return custom_code
  119. def _get_all_app_wrap_components(self) -> dict[tuple[int, str], Component]:
  120. """Get the components that should be wrapped in the app.
  121. Returns:
  122. The components that should be wrapped in the app.
  123. """
  124. app_wrap_components = super()._get_all_app_wrap_components()
  125. if isinstance(self.contents, Var):
  126. var_data = self.contents._get_all_var_data()
  127. if var_data:
  128. for component in var_data.components:
  129. if isinstance(component, Component):
  130. app_wrap_components |= component._get_all_app_wrap_components()
  131. return app_wrap_components
  132. def _get_all_refs(self) -> set[str]:
  133. """Get the refs for the children of the component.
  134. Returns:
  135. The refs for the children.
  136. """
  137. refs = super()._get_all_refs()
  138. if isinstance(self.contents, Var):
  139. var_data = self.contents._get_all_var_data()
  140. if var_data:
  141. for component in var_data.components:
  142. refs |= component._get_all_refs()
  143. return refs
  144. def _render(self) -> Tag:
  145. if isinstance(self.contents, Var):
  146. if isinstance(self.contents, (BooleanVar, ObjectVar)):
  147. return Tagless(contents=f"{{{self.contents.to_string()!s}}}")
  148. return Tagless(contents=f"{{{self.contents!s}}}")
  149. return Tagless(contents=str(self.contents))
  150. def _add_style_recursive(
  151. self, style: ComponentStyle, theme: Component | None = None
  152. ) -> Component:
  153. """Add style to the component and its children.
  154. Args:
  155. style: The style to add.
  156. theme: The theme to add.
  157. Returns:
  158. The component with the style added.
  159. """
  160. new_self = super()._add_style_recursive(style, theme)
  161. if isinstance(self.contents, Var):
  162. var_data = self.contents._get_all_var_data()
  163. if var_data:
  164. for component in var_data.components:
  165. if isinstance(component, Component):
  166. component._add_style_recursive(style, theme)
  167. GLOBAL_CACHE.clear()
  168. return new_self
  169. def _get_vars(
  170. self, include_children: bool = False, ignore_ids: set[int] | None = None
  171. ) -> Iterator[Var]:
  172. """Walk all Vars used in this component.
  173. Args:
  174. include_children: Whether to include Vars from children.
  175. ignore_ids: The ids to ignore.
  176. Yields:
  177. The contents if it is a Var, otherwise nothing.
  178. """
  179. yield self.contents