reflex
reflex copied to clipboard
Dynamically add vars to a State
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:
- [x] Have you followed the guidelines stated in CONTRIBUTING.md file?
- [x] Have you checked to ensure there aren't any other open Pull Requests for the desired changed?
Type of change
- [x] New feature (non-breaking change which adds functionality)
- [x] This change requires a documentation update
New Feature Submission:
- [x] Does your submission pass the tests?
- [x] Have you linted your code locally prior to submission?
Changes To Core Features:
- [x] Have you added an explanation of what your changes do and why you'd like us to include them?
- [x] Have you written new tests for your core changes, as applicable?
- [x] Have you successfully ran tests with your changes locally?