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

Dynamically add vars to a State #381

Merged
merged 3 commits into from
Jan 29, 2023

Conversation

Lendemor
Copy link
Collaborator

Description:

Add a new classmethod add_var to pc.State allowing to add dynamically a var to a State, that can then be used in the same way as a var defined statically.

To add a variable called "dynamic_var" of type int with default value 42:

State.add_var("dynamic_var", int, 42)

This variable can then be accessed in two way :

State.dynamic_var # access it the same way you would a static var
getattr(State, "dynamic_var") # access it like this if the name of the variable to access change at runtime

Closes #303 #346

Note: I added some basic tests for the new method but they could probably be improved.

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.

Great job finding this

@picklelo picklelo merged commit b06f612 into reflex-dev:main Jan 29, 2023
@Lendemor Lendemor deleted the dynamically_add_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.

Dynamically add state vars
2 participants