Skip to content
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

implement rx dynamic #4195

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reflex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@
"SessionStorage",
"ComponentState",
"State",
"dynamic",
],
"style": ["Style", "toggle_color_mode"],
"utils.imports": ["ImportVar"],
Expand Down
1 change: 1 addition & 0 deletions reflex/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ from .state import Cookie as Cookie
from .state import LocalStorage as LocalStorage
from .state import SessionStorage as SessionStorage
from .state import State as State
from .state import dynamic as dynamic
from .state import var as var
from .style import Style as Style
from .style import toggle_color_mode as toggle_color_mode
Expand Down
47 changes: 47 additions & 0 deletions reflex/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
Set,
Tuple,
Type,
TypeVar,
Union,
cast,
get_args,
Expand Down Expand Up @@ -75,6 +76,7 @@
from reflex.utils.exceptions import (
ComputedVarShadowsBaseVars,
ComputedVarShadowsStateVar,
DynamicComponentInvalidSignature,
DynamicRouteArgShadowsStateVar,
EventHandlerShadowsBuiltInStateMethod,
ImmutableStateError,
Expand Down Expand Up @@ -2091,6 +2093,51 @@ class State(BaseState):
is_hydrated: bool = False


T = TypeVar("T", bound=BaseState)


def dynamic(func: Callable[[T], Component]):
"""Create a dynamically generated components from a state class.

Args:
func: The function to generate the component.

Returns:
The dynamically generated component.

Raises:
DynamicComponentInvalidSignature: If the function does not have exactly one parameter.
DynamicComponentInvalidSignature: If the function does not have a type hint for the state class.
"""
number_of_parameters = len(inspect.signature(func).parameters)

func_signature = get_type_hints(func)

if "return" in func_signature:
func_signature.pop("return")

values = list(func_signature.values())

if number_of_parameters != 1:
raise DynamicComponentInvalidSignature(
"The function must have exactly one parameter, which is the state class."
)

if len(values) != 1:
raise DynamicComponentInvalidSignature(
"You must provide a type hint for the state class in the function."
)

state_class: Type[T] = values[0]

def wrapper() -> Component:
from reflex.components.base.fragment import fragment

return fragment(state_class._evaluate(lambda state: func(state)))

return wrapper


class FrontendEventExceptionState(State):
"""Substate for handling frontend exceptions."""

Expand Down
4 changes: 4 additions & 0 deletions reflex/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,7 @@ class StateSchemaMismatchError(ReflexError, TypeError):

class EnvironmentVarValueError(ReflexError, ValueError):
"""Raised when an environment variable is set to an invalid value."""


class DynamicComponentInvalidSignature(ReflexError, TypeError):
"""Raised when a dynamic component has an invalid signature."""
Loading