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

allow backend variables starting with _ #390

Merged
merged 2 commits into from
Jan 30, 2023

Conversation

Lendemor
Copy link
Collaborator

Description

Allow the use of variables starting with _ within a State.
Those variable will be considered as backend variables, and will never be sent to the frontend.
You can read/write a backend variable from within an event handler.

Backend variables can also be used within a computed var to generate a value that will be sent to the frontend.

(This is a non-breaking change because pydantic already ignored any variable starting with _)

Closes #307

Note: I added test for the utils function is_backend_variable() but I'm not too sure how to test the backend variable, I'm open to suggestions if I need to add them.

Checklist

All Submissions:

  • Have you followed the guidelines stated in CONTRIBUTING.md file?
  • Have you checked to ensure there aren't any other open Pull Requests for the desired changed?

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

New Feature Submission:

  • Does your submission pass the tests?
  • Have you linted your code locally prior to submission?

Changes To Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

Copy link
Contributor

@picklelo picklelo left a comment

Choose a reason for hiding this comment

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

Sweet - just one question otherwise looks good :)

@@ -117,6 +117,12 @@ def __init_subclass__(cls, **kwargs):
if parent_state is not None:
cls.inherited_vars = parent_state.vars

cls.backend_vars = {
Copy link
Contributor

@picklelo picklelo Jan 30, 2023

Choose a reason for hiding this comment

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

Don't we need to add this as a Pydantic field to the pc.State class?

class State(Base, ABC):
    """The state of the app."""
    ...
    # Backend vars that are never sent to the client
    backend_vars: ClassVar[Dict[str, Var]] = {}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't notice any problem without the declaration, but I agree we should add it.

However, Dict[str, Var] => Dict[str, Any] (because backend variables are never transformed into Var, they are simply pure Python variables.
I'm not sure what the ClassVar does, but if it's needed I can add it.

Copy link
Contributor

Choose a reason for hiding this comment

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

ClassVar just makes it so it's not tied to the instance but the class instead. So it won't appear in the .dict and .json methods I believe.

@@ -117,6 +117,12 @@ def __init_subclass__(cls, **kwargs):
if parent_state is not None:
cls.inherited_vars = parent_state.vars

cls.backend_vars = {
Copy link
Contributor

Choose a reason for hiding this comment

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

ClassVar just makes it so it's not tied to the instance but the class instead. So it won't appear in the .dict and .json methods I believe.

"""Check if this variable name correspond to a backend variable.

Args:
name (str): The name of the variable to check
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: don't need the type in the docstring since we use type annotations

@picklelo picklelo merged commit 4971f6d into reflex-dev:main Jan 30, 2023
@Lendemor Lendemor deleted the backend_vars branch January 31, 2023 00:40
ACucos1 pushed a commit to ACucos1/rd-pynecone that referenced this pull request Feb 28, 2023
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.

Allow backend-only state vars
2 participants