state

class trame_server.state.State(translator=None, internal=None, commit_fn=None, hot_reload=False, ready=False)

Flexible dictionary managing a server shared state. Variables can be accessed with either the [] or . notation.

Examples:

>>> with state:
...     state.a = 1
...     state.b = 2
... # state is flushed()
ready() None

Mark the state as ready for synchronization.

property is_ready: bool

Return True is the instance is ready for synchronization, False otherwise.

property translator: Translator

Return the translator instance used to namespace the variable names.

client_only(*_args)

Tag a given set of variable name(s) to be client only. This means that when they get changed on the client side, the server will not be aware of their change and no network bandwidth will be used to maintain the server in sync with the client state.

Parameters:

*_args

A list a variable name

to_dict()

Flush current state modification and return the resulting state state as a python dict.

has(key)

Check is a key is currently available in the state

setdefault(key, value)

Set an initial value if the key is not present yet :returns the value in the state for the given key

is_dirty(*_args)

Check if any provided key name(s) still has a pending changed that will need to be flushed.

is_dirty_all(*_args)

Check if all provided key name(s) has a pending changed that will need to be flushed.

dirty(*_args)

Mark existing variable name(s) to be modified in a way that they will be pushed again at flush time. Note that the variable(s) will be unmarked automatically when reset to its previous value.

clean(*_args)

Save pending variable(s) and unmark them as dirty. This will prevent change listener(s) to react or the client to be aware of any change.

update(_dict)

Update the current state dict with the provided one

property modified_keys

Return the set of state’s keys that are modified for the current state.change update.

Usage example:

>>> NAMES = ["a", "b", "c"]
>>> state.update({"a": 1, "b": 2, "c": 3})
>>> @state.change(*NAMES)
... def on_change(*_):
...     for name in state.modified_keys:
...         print(f"{name} value updated to {state[name]}")
>>> with state:
...     state.a += 1
>>> with state:
...     state.a += 1
...     state.b += 2
>>>  with state:
...    state.a += 1
...    state.b += 2
...    state.c += 3
flush()

Force pushing modified state and execute any @state.change listener if the variable value is different (by value AND reference) from its previous value or if dirty has been flagged on the variable and it has not been unflagged since.

property initial

Return the initial state without triggering a flush

change(*_args, **_kwargs)

Use as decorator @server.change(key1, key2, …) so the decorated function will be called like so _fn(**state) when any of the listed key name is getting modified from either client or server.

Can also be used as a function to decorate method functions (see 2nd example below)

Parameters:

*_args

A list of variable name to monitor

Examples:

>>> @state.change("a", "b")  # for functions
... def on_change(a, b, **kwargs):
...     pass
>>> state.change("a")(self.on_a_change)  # for methods
:see-also TrameApp