actually get rid of callable var fr fr (#4821)
This commit is contained in:
parent
6fb491471b
commit
aac61c69c2
@ -29,7 +29,7 @@ from reflex.event import (
|
|||||||
from reflex.utils import format
|
from reflex.utils import format
|
||||||
from reflex.utils.imports import ImportVar
|
from reflex.utils.imports import ImportVar
|
||||||
from reflex.vars import VarData
|
from reflex.vars import VarData
|
||||||
from reflex.vars.base import CallableVar, Var, get_unique_variable_name
|
from reflex.vars.base import Var, get_unique_variable_name
|
||||||
from reflex.vars.sequence import LiteralStringVar
|
from reflex.vars.sequence import LiteralStringVar
|
||||||
|
|
||||||
DEFAULT_UPLOAD_ID: str = "default"
|
DEFAULT_UPLOAD_ID: str = "default"
|
||||||
@ -45,7 +45,6 @@ upload_files_context_var_data: VarData = VarData(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@CallableVar
|
|
||||||
def upload_file(id_: str = DEFAULT_UPLOAD_ID) -> Var:
|
def upload_file(id_: str = DEFAULT_UPLOAD_ID) -> Var:
|
||||||
"""Get the file upload drop trigger.
|
"""Get the file upload drop trigger.
|
||||||
|
|
||||||
@ -75,7 +74,6 @@ def upload_file(id_: str = DEFAULT_UPLOAD_ID) -> Var:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@CallableVar
|
|
||||||
def selected_files(id_: str = DEFAULT_UPLOAD_ID) -> Var:
|
def selected_files(id_: str = DEFAULT_UPLOAD_ID) -> Var:
|
||||||
"""Get the list of selected files.
|
"""Get the list of selected files.
|
||||||
|
|
||||||
|
@ -13,14 +13,12 @@ from reflex.event import CallableEventSpec, EventSpec, EventType
|
|||||||
from reflex.style import Style
|
from reflex.style import Style
|
||||||
from reflex.utils.imports import ImportVar
|
from reflex.utils.imports import ImportVar
|
||||||
from reflex.vars import VarData
|
from reflex.vars import VarData
|
||||||
from reflex.vars.base import CallableVar, Var
|
from reflex.vars.base import Var
|
||||||
|
|
||||||
DEFAULT_UPLOAD_ID: str
|
DEFAULT_UPLOAD_ID: str
|
||||||
upload_files_context_var_data: VarData
|
upload_files_context_var_data: VarData
|
||||||
|
|
||||||
@CallableVar
|
|
||||||
def upload_file(id_: str = DEFAULT_UPLOAD_ID) -> Var: ...
|
def upload_file(id_: str = DEFAULT_UPLOAD_ID) -> Var: ...
|
||||||
@CallableVar
|
|
||||||
def selected_files(id_: str = DEFAULT_UPLOAD_ID) -> Var: ...
|
def selected_files(id_: str = DEFAULT_UPLOAD_ID) -> Var: ...
|
||||||
@CallableEventSpec
|
@CallableEventSpec
|
||||||
def clear_selected_files(id_: str = DEFAULT_UPLOAD_ID) -> EventSpec: ...
|
def clear_selected_files(id_: str = DEFAULT_UPLOAD_ID) -> EventSpec: ...
|
||||||
|
@ -144,7 +144,7 @@ class ColorModeIconButton(IconButton):
|
|||||||
|
|
||||||
if allow_system:
|
if allow_system:
|
||||||
|
|
||||||
def color_mode_item(_color_mode: str):
|
def color_mode_item(_color_mode: Literal["light", "dark", "system"]):
|
||||||
return dropdown_menu.item(
|
return dropdown_menu.item(
|
||||||
_color_mode.title(), on_click=set_color_mode(_color_mode)
|
_color_mode.title(), on_click=set_color_mode(_color_mode)
|
||||||
)
|
)
|
||||||
|
@ -12,7 +12,7 @@ from reflex.utils.exceptions import ReflexError
|
|||||||
from reflex.utils.imports import ImportVar
|
from reflex.utils.imports import ImportVar
|
||||||
from reflex.utils.types import get_origin
|
from reflex.utils.types import get_origin
|
||||||
from reflex.vars import VarData
|
from reflex.vars import VarData
|
||||||
from reflex.vars.base import CallableVar, LiteralVar, Var
|
from reflex.vars.base import LiteralVar, Var
|
||||||
from reflex.vars.function import FunctionVar
|
from reflex.vars.function import FunctionVar
|
||||||
from reflex.vars.object import ObjectVar
|
from reflex.vars.object import ObjectVar
|
||||||
|
|
||||||
@ -48,7 +48,6 @@ def _color_mode_var(_js_expr: str, _var_type: Type = str) -> Var:
|
|||||||
).guess_type()
|
).guess_type()
|
||||||
|
|
||||||
|
|
||||||
@CallableVar
|
|
||||||
def set_color_mode(
|
def set_color_mode(
|
||||||
new_color_mode: LiteralColorMode | Var[LiteralColorMode] | None = None,
|
new_color_mode: LiteralColorMode | Var[LiteralColorMode] | None = None,
|
||||||
) -> Var[EventChain]:
|
) -> Var[EventChain]:
|
||||||
|
@ -1903,61 +1903,6 @@ def _or_operation(a: Var, b: Var):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@dataclasses.dataclass(
|
|
||||||
eq=False,
|
|
||||||
frozen=True,
|
|
||||||
slots=True,
|
|
||||||
)
|
|
||||||
class CallableVar(Var):
|
|
||||||
"""Decorate a Var-returning function to act as both a Var and a function.
|
|
||||||
|
|
||||||
This is used as a compatibility shim for replacing Var objects in the
|
|
||||||
API with functions that return a family of Var.
|
|
||||||
"""
|
|
||||||
|
|
||||||
fn: Callable[..., Var] = dataclasses.field(
|
|
||||||
default_factory=lambda: lambda: Var(_js_expr="undefined")
|
|
||||||
)
|
|
||||||
original_var: Var = dataclasses.field(
|
|
||||||
default_factory=lambda: Var(_js_expr="undefined")
|
|
||||||
)
|
|
||||||
|
|
||||||
def __init__(self, fn: Callable[..., Var]):
|
|
||||||
"""Initialize a CallableVar.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
fn: The function to decorate (must return Var)
|
|
||||||
"""
|
|
||||||
original_var = fn()
|
|
||||||
super(CallableVar, self).__init__(
|
|
||||||
_js_expr=original_var._js_expr,
|
|
||||||
_var_type=original_var._var_type,
|
|
||||||
_var_data=VarData.merge(original_var._get_all_var_data()),
|
|
||||||
)
|
|
||||||
object.__setattr__(self, "fn", fn)
|
|
||||||
object.__setattr__(self, "original_var", original_var)
|
|
||||||
|
|
||||||
def __call__(self, *args: Any, **kwargs: Any) -> Var:
|
|
||||||
"""Call the decorated function.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
*args: The args to pass to the function.
|
|
||||||
**kwargs: The kwargs to pass to the function.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The Var returned from calling the function.
|
|
||||||
"""
|
|
||||||
return self.fn(*args, **kwargs)
|
|
||||||
|
|
||||||
def __hash__(self) -> int:
|
|
||||||
"""Calculate the hash of the object.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
The hash of the object.
|
|
||||||
"""
|
|
||||||
return hash((type(self).__name__, self.original_var))
|
|
||||||
|
|
||||||
|
|
||||||
RETURN_TYPE = TypeVar("RETURN_TYPE")
|
RETURN_TYPE = TypeVar("RETURN_TYPE")
|
||||||
|
|
||||||
DICT_KEY = TypeVar("DICT_KEY")
|
DICT_KEY = TypeVar("DICT_KEY")
|
||||||
|
@ -87,7 +87,7 @@ def UploadFile():
|
|||||||
),
|
),
|
||||||
rx.box(
|
rx.box(
|
||||||
rx.foreach(
|
rx.foreach(
|
||||||
rx.selected_files,
|
rx.selected_files(),
|
||||||
lambda f: rx.text(f, as_="p"),
|
lambda f: rx.text(f, as_="p"),
|
||||||
),
|
),
|
||||||
id="selected_files",
|
id="selected_files",
|
||||||
|
@ -61,7 +61,7 @@ def ColorToggleApp():
|
|||||||
rx.icon(tag="moon", size=20),
|
rx.icon(tag="moon", size=20),
|
||||||
value="dark",
|
value="dark",
|
||||||
),
|
),
|
||||||
on_change=set_color_mode,
|
on_change=set_color_mode(),
|
||||||
variant="classic",
|
variant="classic",
|
||||||
radius="large",
|
radius="large",
|
||||||
value=color_mode,
|
value=color_mode,
|
||||||
|
Loading…
Reference in New Issue
Block a user