Skip to main content

plain_variable

Variable

from dara.core.interactivity.plain_variable import Variable

class Variable(NonDataVariable, Generic[VariableType])

A Variable represents a dynamic value in the system that can be read and written to by components and actions

Attributes

  • default: Optional[VariableType]
  • persist_value: bool
  • store: Optional[PersistenceStore]
  • uid: str
  • nested: List[str]

Methods

__init__

def __init__(default: Optional[VariableType] = None,
persist_value: Optional[bool] = False,
uid: Optional[str] = None,
store: Optional[PersistenceStoreType_co] = None,
nested: Optional[List[str]] = None)

A Variable represents a dynamic value in the system that can be read and written to by components and actions

Arguments:

  • default: the initial value for the variable, defaults to None
  • persist_value: whether to persist the variable value across page reloads
  • uid: the unique identifier for this variable; if not provided a random one is generated
  • store: a persistence store to attach to the variable; modifies where the source of truth for the variable is

serialize_default

@field_serializer('default', mode='wrap')
def serialize_default(default: Any, nxt: SerializerFunctionWrapHandler)

Handle serializing the default value of the Variable using the registry of encoders. This ensures that users can define a serializer with config.add_encoder and it will be used when serializing the Variable.default.

init_override

@staticmethod
@contextmanager
def init_override(override: Callable[[dict], dict])

Override the init function of all Variables created within the context of this function.

with Variable.init_override(lambda kwargs: {**kwargs, 'persist_value': True}):
var = Variable()

Arguments:

  • override: a function that takes a dict of kwargs and returns a modified dict of kwargs

get

def get(key: str)

Create a copy of this Variable that points to a nested key. This is useful when

storing e.g. a dictionary in a Variable and wanting to access a specific key.

from dara.core import Variable, UpdateVariable
from dara_dashboarding_extension import Input, Text, Stack, Button

state = Variable({
'input_value': 'Text',
'settings': {
'language': 'English'
}
})

page_content = Stack(
# Only `input_value` will be displayed
Text(text=state.get('input_value')),

# Only the specified property will be updated
Input(value=state.get('input_value')),

# You can chain the `get` calls to specify a sub-property to use
Input(value=state.get('settings').get('language')),

# You can also use the `UpdateVariable` action to update a sub-property
Button(
'Set Language to German',
onclick=UpdateVariable(lambda _: 'German', variable=state.get('settings').get('language')
)
)

**Arguments**:

- `key`: the key to access; must be a string

sync

def sync()

Create an action to synchronise the value of this Variable with input value sent from the component.


from dara.core import Variable
from dara.components import Select

var = Variable('first')
another_var = Variable()

Select(
value=var,
items=['first', 'second', 'third'],
# syncing value to `another_var` in addition to `var`
onchange=another_var.sync(),
)

toggle

def toggle()

Create an action to toggle the value of this Variable. Note this only works for boolean variables.


from dara.core import Variable
from dara.components import Button

var = Variable(True)

Button(
'Toggle',
onclick=var.toggle(),
)

update

def update(value: Any)

Create an action to update the value of this Variable to a provided value.


from dara.core import Variable
from dara.components import Button

show = Variable(True)

Button(
'Hide',
onclick=var.update(False),
)

create_from_derived

@classmethod
def create_from_derived(cls, other: DerivedVariable)

Create a Variable instance from a DerivedVariable.

The Variable will be initialised with the current value of the DerivedVariable but will still be mutable afterwards.

Arguments:

  • default: the initial value for the variable, defaults to None