derived_variable
DerivedVariable
from dara.core.interactivity.derived_variable import DerivedVariable
class DerivedVariable(NonDataVariable, Generic[VariableType])
A DerivedVariable allows a value to be derived (via a function) from the current value of a set of other variables with a python function. This is one of two primary ways that python logic can be embedded into the application (the other being the @py_component decorator).
DerivedVariables can be chained together to form complex data flows whilst keeping everything organized and structured in an easy to follow way. DerivedVariable results are cached automatically and will only be recalculated when necessary.
Attributes
- cache: Optional[BaseCachePolicy]
- variables: List[AnyVariable]
- polling_interval: Optional[int]
- deps:
- nested: List[str]
- uid: str
Methods
__init__
def __init__(func: Callable[..., VariableType],
variables: List[AnyVariable],
cache: Optional[CacheArgType] = Cache.Type.GLOBAL,
run_as_task: bool = False,
polling_interval: Optional[int] = None,
deps: Optional[List[AnyVariable]] = None,
uid: Optional[str] = None,
_get_value: Optional[Callable[..., Awaitable[Any]]] = None)
A DerivedVariable allows a value to be derived (via a function) from the current value of a set of other
variables with a python function. This is one of two primary ways that python logic can be embedded into the application (the other being the @py_component decorator).
DerivedVariables can be chained together to form complex data flows whilst keeping everything organized and structured in an easy to follow way. DerivedVariable results are cached automatically and will only be recalculated when necessary.
Arguments:
func
: the function to derive a new value from the input variables.variables
: a set of input variables that will be passed to the deriving functioncache
: whether to cache the result, defaults to global caching. Other options are to cache per user session, per user or to not cache at allrun_as_task
: whether to run the calculation in a separate process, recommended for any CPU intensive tasks, defaults to Falsepolling_interval
: an optional polling interval for the DerivedVariable. Setting this will cause the component to poll the backend and refresh itself every n seconds.deps
: an optional array of variables, specifying which dependant variables changing should trigger a recalculation of the derived variabledeps = None
-func
is ran everytime (default behaviour),deps = []
-func
is ran once on initial startup,deps = [var1, var2]
-func
is ran whenever one of these vars changesdeps = [var1.get('nested_property')]
-func
is ran only when the nested property changes, other changes to the variable are ignoreduid
: the unique identifier for this variable; if not provided a random one is generated
validate_deps
@validator('deps', pre=True, always=True)
@classmethod
def validate_deps(cls, deps: Any, values: Dict) -> List[AnyVariable]
If deps is not specified, set deps to include all variables used
trigger
def trigger(force: bool = True)
Get a TriggerVariable action for the variable that can be used to force a recalculation.
Arguments:
force
: whether the recalculation should ignore any caching settings, defaults to True
add_latest_value
@classmethod
async def add_latest_value(cls, store: CacheStore,
var_entry: DerivedVariableRegistryEntry,
cache_key: str)
Adds the latest value of this DerivedVariable to the registry. This method considers the cache_type of this DerivedVariable and adds or updates its entry in the registry.
Arguments:
var_entry
: the registry entry for the derived variablecache_key
: the cache key for the derived variable
get_value
@classmethod
async def get_value(cls,
var_entry: DerivedVariableRegistryEntry,
store: CacheStore,
task_mgr: TaskManager,
args: List[Any],
force: bool = False) -> DerivedVariableResult
Get the value of this DerivedVariable. This method will check the main app store for an appropriate response
first, if it does not find one then it will run the underlying function in a separate process and return the result. Adding it to the cache in the process.
Arguments:
var
: the registry entry for the derived variablestore
: the store instance to check for cached valuestask_mgr
: task manager instanceargs
: the arguments to call the underlying function withforce
: whether to ignore cache
DerivedVariableRegistryEntry
from dara.core.interactivity.derived_variable import DerivedVariableRegistryEntry
class DerivedVariableRegistryEntry(CachedRegistryEntry)
Attributes
- deps:
- func:
- run_as_task: bool
- variables: List[AnyVariable]
- polling_interval: Optional[int]
- get_value: Callable[Awaitable[Any]]
get_value
Handler to get the value of the derived variable. Defaults to DerivedVariable.get_value, should match the signature