-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add hybrid_property #3806
Draft
benedikt-bartscher
wants to merge
16
commits into
reflex-dev:main
Choose a base branch
from
benedikt-bartscher:hybrid-properties
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
add hybrid_property #3806
benedikt-bartscher
wants to merge
16
commits into
reflex-dev:main
from
benedikt-bartscher:hybrid-properties
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We could implement the same for diff --git a/reflex/vars.py b/reflex/vars.py
index 8f1f0186..c502d814 100644
--- a/reflex/vars.py
+++ b/reflex/vars.py
@@ -2154,10 +2154,16 @@ class BaseVar(Var):
return setter
+VAR_CALLABLE = Callable[[Any], Var]
+
+
@dataclasses.dataclass(init=False, eq=False)
class ComputedVar(Var, property):
"""A field with computed getters."""
+ # The optional var function for the property.
+ _var: VAR_CALLABLE | None = None
+
# Whether to track dependencies and cache computed values
_cache: bool = dataclasses.field(default=False)
@@ -2322,6 +2328,18 @@ class ComputedVar(Var, property):
return True
return datetime.datetime.now() - last_updated > self._update_interval
+ def var(self, func: VAR_CALLABLE) -> Self:
+ """Set the (optional) var function for the property.
+
+ Args:
+ func: The var function to set.
+
+ Returns:
+ The property instance with the var function set.
+ """
+ self._var = func
+ return self
+
def __get__(self, instance: BaseState | None, owner):
"""Get the ComputedVar value.
@@ -2334,6 +2352,9 @@ class ComputedVar(Var, property):
Returns:
The value of the var for the given instance.
"""
+ if instance is None and self._var is not None:
+ return self._var(owner, value=self)
+
if instance is None or not self._cache:
return super().__get__(instance, owner)
@@ -2546,8 +2567,6 @@ def computed_var(
# Partial function of computed_var with cache=True
cached_var = functools.partial(computed_var, cache=True, _deprecated_cached_var=True)
-VAR_CALLABLE = Callable[[Any], Var]
-
class HybridProperty(property):
"""A hybrid property that can also be used in frontend/as var.""" |
I can move this to experimental if needed |
Merge remote-tracking branch 'upstream/main' into hybrid-properties
Merge remote-tracking branch 'upstream/main' into hybrid-properties
…var' into hybrid-properties
benedikt-bartscher
force-pushed
the
hybrid-properties
branch
from
September 10, 2024 20:28
3559702
to
30ded81
Compare
I think we can expose this under Marking as Draft until resynced with main and the changes needed are done. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Reflex currently renders
@property
s on States like this:<property object at 0x723b334e5940>
.This PR adds a
hybrid_property
decorator which functions like a normal python property but additionally allows (class-level) access from the frontend. You can use the same code for frontend and backend, or implement 2 different methods.example
Note:
full_name
andhas_last_name
are notrx.var
s and are not sent over the wire (websocket). They are instead computed fromfirst_name
andlast_name
separately in frontend (js) and backend (python).