Local State

You can define a dictionary using the following code that can contain objects that do not abide by the linear execution model of the notebook:

import zero_true as zt

local_state = zt.state()
local_state.setdefault('counter', 0)

button = zt.Button(id='button')

if button.value:
    local_state['counter'] += 1

print(local_state['counter'])

This example will create a button that increments a counter every time it is clicked. The counter will persist across runs while a normal counter variable would be reset on each run. These state objects will be local to each user of an app.

Global State

A global state dictionary is also available to be utilized that behaves similarly to the local state but is shared across all users. Here is an example:

import zero_true as zt

zt.global_state.setdefault('counter', 0)

button = zt.Button(id='button')

if button.value:
    zt.global_state['counter'] += 1

print(zt.global_state['counter'])
Changes to this dictionary will not be broadcasted to all users but on subsequent runs the changes will be reflected in each user’s app.