-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
There was a problem hiding this 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 = { |
There was a problem hiding this comment.
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]] = {}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 = { |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
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
Checklist
All Submissions:
Type of change
New Feature Submission:
Changes To Core Features: