Skip to content

Commit

Permalink
feat: Add add_image
Browse files Browse the repository at this point in the history
  • Loading branch information
manzt committed Oct 19, 2023
1 parent f532c5e commit 652717d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion python/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"checkJs": true,
"allowJs": true,
"lib": [
"ES2020",
"ES2022",
"DOM",
"DOM.Iterable"
]
Expand Down
24 changes: 19 additions & 5 deletions python/src/vizarr/_widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ function send(model, payload, { timeout = 3000 } = {}) {
});
}

/** @param {import("npm:@anywidget/types").AnyModel} model */
function get_source(model) {
let source = model.get("_source");
/**
* @param {import("npm:@anywidget/types").AnyModel} model
* @param {string | { id: string }} source
*/
function get_source(model, source) {
if (typeof source === "string") {
return source;
}
Expand Down Expand Up @@ -70,9 +72,9 @@ function get_source(model) {

/**
* @typedef Model
* @property {string | { id: string }} _source
* @property {string} height
* @property {ViewState=} view_state
* @property {{ source: string | { id: string }}[]} _configs
*/

/**
Expand Down Expand Up @@ -104,6 +106,18 @@ export function render({ model, el }) {
}, 200),
);
}
viewer.addImage({ source: get_source(model) });
{
// sources are append-only now
for (const config of model.get("_configs")) {
const source = get_source(model, config.source);
viewer.addImage({ ...config, source });
}
model.on("change:_configs", () => {
const last = model.get("_configs").at(-1);
if (!last) return;
const source = get_source(model, last.source);
viewer.addImage({ ...last, source });
});
}
el.appendChild(div);
}
21 changes: 13 additions & 8 deletions python/src/vizarr/_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ def _store_keyprefix(obj):

class Viewer(anywidget.AnyWidget):
_esm = pathlib.Path(__file__).parent / "_widget.js"
_source = traitlets.Any().tag(sync=True)
_configs = traitlets.List().tag(sync=True)
view_state = traitlets.Dict().tag(sync=True)
height = traitlets.Unicode("500px").tag(sync=True);

def __init__(self, source, **kwargs):
def __init__(self, source=None, **kwargs):
super().__init__(**kwargs)
self._store_paths = []
if not isinstance(source, str):
store, key_prefix = _store_keyprefix(source)
source = { "id": len(self._store_paths) }
self._store_paths.append((store, key_prefix))
super().__init__(_source=source, **kwargs)
if source is not None:
self.add_image(source=source)
self.on_msg(self._handle_custom_msg)

def _handle_custom_msg(self, msg, buffers):
Expand All @@ -45,7 +43,6 @@ def _handle_custom_msg(self, msg, buffers):

if msg["payload"]["type"] == "has":
self.send({ "uuid": msg["uuid"], "payload": key in store })
print(store)
return

if msg["payload"]["type"] == "get":
Expand All @@ -55,3 +52,11 @@ def _handle_custom_msg(self, msg, buffers):
buffers = []
self.send({ "uuid": msg["uuid"], "payload": { "success": len(buffers) == 1 } }, buffers)
return

def add_image(self, source, **config):
if not isinstance(source, str):
store, key_prefix = _store_keyprefix(source)
source = { "id": len(self._store_paths) }
self._store_paths.append((store, key_prefix))
config["source"] = source
self._configs = self._configs + [config]

0 comments on commit 652717d

Please sign in to comment.