-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
27 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
"anywidget": patch | ||
"@anywidget/types": patch | ||
--- | ||
|
||
Add experimental reducer API for sending/await-ing custom messages | ||
|
||
Introduces a unified API for dispatching messages from the front end, and | ||
await-ing response from Python. This removes a lot of boilerplate required for | ||
this kind of pattern previously. This API is experimental and opt-in, only if | ||
`_experimental_anywidget_reducer` is implemented on the anywidget subclass. | ||
|
||
```py | ||
class Widget(anywidget.AnyWidget): | ||
_esm = """ | ||
async function render({ model, el, experimental }) { | ||
let [response, buffers] = await experimental.dispatch("ping"); | ||
// Handle the response | ||
console.log(response) // pong | ||
} | ||
export default { render }; | ||
""" | ||
|
||
def _experimental_anywidget_reducer(self, action, buffers): | ||
assert action == "ping" | ||
return "pong", [] | ||
``` |