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 _evaluate in state #4018

Merged
merged 4 commits into from
Sep 27, 2024
Merged

implement _evaluate in state #4018

merged 4 commits into from
Sep 27, 2024

Conversation

adhami3310
Copy link
Member

Introduces experimental inline-way to define State components.

"""This is a mockup example to demonstrate the usage of server_side function."""

import random

import reflex as rx


class State(rx.State):
    large_number: int = 100
    header_name: str = "Factorial Calculator"

    def randomize_number(self):
        self.large_number = random.randint(1, 900)


def factorial(n: int) -> int:
    """Return the factorial of n."""
    if n == 0:
        return 1
    return n * factorial(n - 1)


def is_prime(n: int) -> bool:
    """Return True if n is prime, False otherwise."""
    if n < 2:
        return False
    return all(n % i != 0 for i in range(2, n))


app = rx.App()


@app.add_page
def index():
    return rx.vstack(
        rx.text(
            f"factorial of {State.large_number} is ",
            State._evaluate(
                lambda state: str(factorial(state.large_number)),
            ),
            width="100%",
        ),
        rx.text(
            f"url-name of {State.header_name} is ",
            State._evaluate(lambda state: state.header_name.lower().replace(" ", "-")),
        ),
        rx.text(
            f"{State.large_number} is ",
            State._evaluate(
                lambda state: (
                    "prime" if is_prime(state.large_number) else "not prime"
                ),
            ),
        ),
        rx.button(
            "Randomize",
            on_click=lambda: State.randomize_number,
        ),
    )

@adhami3310 adhami3310 requested a review from masenf September 27, 2024 22:59
@@ -1559,8 +1559,9 @@ def __init__(
Raises:
TypeError: If the computed var dependencies are not Var instances or var names.
"""
hints = get_type_hints(fget)
hint = hints.get("return", Any)
hint = kwargs.pop("return_type", None) or get_type_hints(fget).get(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we put return_type in the actual __init__ signature? seems a little hard to find/undocumented this way

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might be intentionally that way (using might + intentionally in something i did is a bit questionable, but alas), i did it because users should almost always pass the return type through the function return signature, sometimes in reflex code we don't have that option so it's more of an internal thing.

@masenf masenf merged commit 62021b0 into main Sep 27, 2024
39 checks passed
@masenf masenf deleted the inline-state-components branch December 12, 2024 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants