Out[2]:
Minimal example¶
In [3]:
Copied!
import genstudio.plot as Plot
(
Plot.initialState({"clicks": 0})
| [
"div.bg-yellow-200.p-4",
{"onClick": Plot.js("(e) => $state.clicks = ($state.clicks || 0) + 1")},
Plot.js("`Clicked ${$state.clicks} times`"),
]
| [
"div.p-3.border.text-center",
{"onClick": Plot.js("(e) => $state.update({clicks: 0})")},
"Reset",
]
)
import genstudio.plot as Plot
(
Plot.initialState({"clicks": 0})
| [
"div.bg-yellow-200.p-4",
{"onClick": Plot.js("(e) => $state.clicks = ($state.clicks || 0) + 1")},
Plot.js("`Clicked ${$state.clicks} times`"),
]
| [
"div.p-3.border.text-center",
{"onClick": Plot.js("(e) => $state.update({clicks: 0})")},
"Reset",
]
)
Out[3]:
State Updates¶
State updates in GenStudio work bidirectionally between Python and JavaScript:
Python → JavaScript Updates¶
When you update state from Python using widget.state.update()
or by setting attributes directly:
- The update is normalized into a list of
[key, operation, payload]
tuples - For synced state, the update is applied locally to
widget.state
- The update is serialized to JSON and sent to JavaScript via widget messaging
- Any registered Python listeners are notified
JavaScript → Python Updates¶
When state is updated from JavaScript using $state.update()
:
- The update is normalized into
[key, operation, payload]
format - The update is applied locally to the JavaScript state store
- For synced state keys, the update is sent back to Python
- Python applies the update and notifies any listeners
Update Operations¶
Updates support different operations beyond just setting values:
append
: Add item to end of listconcat
: Join two lists togethersetAt
: Set value at specific indexreset
: Reset value to initial state