Skip to content

Commit

Permalink
WIP: set_label_image
Browse files Browse the repository at this point in the history
Try to pull data from client-side rather than pushing from server-side
  • Loading branch information
bnmajor committed Jul 25, 2023
1 parent d6e19f2 commit cb637c9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
20 changes: 20 additions & 0 deletions itkwidgets/standalone/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,26 @@
},
viewer: () => viewer
});
await server.registerService({
name: "setLabelOrImage",
id: "set-label-or-image",
description: "Set the image or label image.",
config: {
visibility: "protected",
require_context: false,
run_in_executor: true,
},
set_label_image: async () => {
remoteService = await server.getService(`${remoteID}:fetch-zarr-store`);
let labelImage = await remoteService.fetchZarrStore("label_image");
viewer.setLabelImage(labelImage);
},
set_image: async () => {
remoteService = await server.getService(`${remoteID}:fetch-zarr-store`);
let image = await remoteService.fetchZarrStore("image");
viewer.setImage(image);
}
});
remoteService = await server.getService(`${remoteID}:itkwidgets-viewer-ready`);
await remoteService.viewerReady(viewer);
}
Expand Down
45 changes: 43 additions & 2 deletions itkwidgets/standalone_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import argparse
import functools
import logging
import socket
import code
Expand Down Expand Up @@ -45,6 +46,7 @@ def find_port(port=SERVER_PORT):
PORT = find_port()
OPTS = None
EVENT = threading.Event()
VIEWER = None


def standalone_viewer(url):
Expand Down Expand Up @@ -97,6 +99,16 @@ async def viewer_ready(itk_viewer):
EVENT.set()


def set_label_or_image(server, type):
workspace = server.config.workspace
svc = server.get_service(f"{workspace}/itkwidgets-client:set-label-or-image")
getattr(svc, f"set_{type}")()


def fetch_zarr_store(store_type):
return getattr(VIEWER, store_type, None)


def start_viewer(server_url):
server = connect_to_server_sync(
{
Expand Down Expand Up @@ -136,6 +148,34 @@ def start_viewer(server_url):
}
)

server.register_service(
{
"name": "zarr_store",
"id": "zarr-store",
"description": "",
"config": {
"visibility": "protected",
"require_context": False,
"run_in_executor": True,
},
"zarr_store": functools.partial(zarr_store, server),
}
)

server.register_service(
{
"name": "data_set",
"id": "data-set",
"description": "Save the image data set via REPL session.",
"config": {
"visibility": "protected",
"require_context": False,
"run_in_executor": True,
},
"set_label_or_image": functools.partial(set_label_or_image, server),
}
)

workspace = server.config.workspace
token = server.generate_token()
params = urlencode({"workspace": workspace, "token": token})
Expand All @@ -144,6 +184,7 @@ def start_viewer(server_url):


def main():
global VIEWER
JWT_SECRET = str(uuid.uuid4())
os.environ["JWT_SECRET"] = JWT_SECRET
hypha_server_env = os.environ.copy()
Expand Down Expand Up @@ -184,14 +225,14 @@ def main():
EVENT.wait() # Wait until viewer is created before launching REPL
workspace = server.config.workspace
svc = server.get_service(f"{workspace}/itkwidgets-client:itk-vtk-viewer")
viewer = view(itk_viewer=svc.viewer())
VIEWER = view(itk_viewer=svc.viewer(), server=server)
banner = f"""
Welcome to the itkwidgets command line tool! Press CTRL+D or
run `exit()` to terminate the REPL session. Use the `viewer`
object to manipulate the viewer.
"""
exitmsg = "Exiting REPL. Press CTRL+C to teminate CLI tool."
code.interact(banner=banner, local={"viewer": viewer}, exitmsg=exitmsg)
code.interact(banner=banner, local={"viewer": VIEWER}, exitmsg=exitmsg)


def cli_entrypoint():
Expand Down
18 changes: 16 additions & 2 deletions itkwidgets/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def __init__(
api.export(self.viewer_rpc)
else:
self._itk_viewer = add_data_kwargs.get('itk_viewer', None)
self.server = add_data_kwargs.get('server', None)
self.workspace = self.server.config.workspace

@property
def loop(self):
Expand Down Expand Up @@ -208,7 +210,13 @@ def set_image(self, image: Image, name: str = 'Image'):
render_type = _detect_render_type(image, 'image')
if render_type is RenderType.IMAGE:
image = _get_viewer_image(image, label=False)
self.queue_request('setImage', image, name)
if ENVIRONMENT is Env.HYPHA:
self.image = image
svc_name = f'{self.workspace}/itkwidgets-server:data-set'
svc = self.server.get_service(svc_name)
svc.set_label_or_image('image')
else:
self.queue_request('setImage', image, name)
elif render_type is RenderType.POINT_SET:
image = _get_viewer_point_set(image)
self.queue_request('setPointSets', image)
Expand Down Expand Up @@ -279,7 +287,13 @@ def set_label_image(self, label_image: Image):
render_type = _detect_render_type(label_image, 'image')
if render_type is RenderType.IMAGE:
label_image = _get_viewer_image(label_image, label=True)
self.queue_request('setLabelImage', label_image)
if ENVIRONMENT is Env.HYPHA:
self.label_image = label_image
svc_name = f"{self.workspace}/itkwidgets-server:data-set"
svc = self.server.get_service(svc_name)
svc.set_label_or_image('label_image')
else:
self.queue_request('setLabelImage', label_image)
elif render_type is RenderType.POINT_SET:
label_image = _get_viewer_point_set(label_image)
self.queue_request('setPointSets', label_image)
Expand Down

0 comments on commit cb637c9

Please sign in to comment.