|
@@ -35,6 +35,7 @@ from typing import (
|
|
|
|
|
|
import dill
|
|
|
from sqlalchemy.orm import DeclarativeBase
|
|
|
+from typing_extensions import Self
|
|
|
|
|
|
from reflex.config import get_config
|
|
|
from reflex.vars.base import (
|
|
@@ -43,6 +44,7 @@ from reflex.vars.base import (
|
|
|
Var,
|
|
|
computed_var,
|
|
|
dispatch,
|
|
|
+ get_unique_variable_name,
|
|
|
is_computed_var,
|
|
|
)
|
|
|
|
|
@@ -695,6 +697,36 @@ class BaseState(Base, ABC, extra=pydantic.Extra.allow):
|
|
|
and hasattr(value, "__code__")
|
|
|
)
|
|
|
|
|
|
+ @classmethod
|
|
|
+ def _evaluate(cls, f: Callable[[Self], Any]) -> Var:
|
|
|
+ """Evaluate a function to a ComputedVar. Experimental.
|
|
|
+
|
|
|
+ Args:
|
|
|
+ f: The function to evaluate.
|
|
|
+
|
|
|
+ Returns:
|
|
|
+ The ComputedVar.
|
|
|
+ """
|
|
|
+ console.warn(
|
|
|
+ "The _evaluate method is experimental and may be removed in future versions."
|
|
|
+ )
|
|
|
+ from reflex.components.base.fragment import fragment
|
|
|
+ from reflex.components.component import Component
|
|
|
+
|
|
|
+ unique_var_name = get_unique_variable_name()
|
|
|
+
|
|
|
+ @computed_var(_js_expr=unique_var_name, return_type=Component)
|
|
|
+ def computed_var_func(state: Self):
|
|
|
+ return fragment(f(state))
|
|
|
+
|
|
|
+ setattr(cls, unique_var_name, computed_var_func)
|
|
|
+ cls.computed_vars[unique_var_name] = computed_var_func
|
|
|
+ cls.vars[unique_var_name] = computed_var_func
|
|
|
+ cls._update_substate_inherited_vars({unique_var_name: computed_var_func})
|
|
|
+ cls._always_dirty_computed_vars.add(unique_var_name)
|
|
|
+
|
|
|
+ return getattr(cls, unique_var_name)
|
|
|
+
|
|
|
@classmethod
|
|
|
def _mixins(cls) -> List[Type]:
|
|
|
"""Get the mixin classes of the state.
|