Skip to content

Commit

Permalink
ENH: Add convenience function to fetch itk-vtk-viewer
Browse files Browse the repository at this point in the history
This allows a user to import standalone_viewer and pass in the url from their
web browser in order to connect to the server and get the itk-vtk-viewer object
back. They can then call functions to update the viewer programmatically from a
Python REPL or IPython session.
  • Loading branch information
bnmajor committed Jun 19, 2023
1 parent 4b5d550 commit 1cc1be5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions itkwidgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
register_itkwasm_imjoy_codecs()

from .viewer import Viewer, view, compare_images
from .standalone_server import standalone_viewer

__all__ = [
"Viewer",
"view",
"compare_images",
"standalone_viewer",
]
23 changes: 21 additions & 2 deletions itkwidgets/standalone.py → itkwidgets/standalone_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,33 @@
from itkwidgets.imjoy import register_itkwasm_imjoy_codecs_cli
from itkwidgets._initialization_params import build_config, parse_input_data, build_init_data
from pathlib import Path
from urllib.parse import urlencode
from urllib.parse import parse_qs, urlencode, urlparse


def _snake_to_camel(variable):
first, *words = variable.split('_')
return first + ''.join([w.capitalize() for w in words])


async def standalone_viewer(url):
query = parse_qs(urlparse(url).query)
server_url = f"http://{SERVER_HOST}:{SERVER_PORT}"
workspace = query["workspace"][0]
token = query["token"][0]

server = await connect_to_server({
'client_id': 'itkwidgets-interactive',
'name': 'itkwidgets_interactive',
"server_url": server_url,
"workspace": workspace,
"token": token
})

svc = await server.get_service(
f'{workspace}/itkwidgets-client:itk-vtk-viewer')
return await svc.viewer()


def input_dict():
user_input = vars(opts)
if opts.image:
Expand Down Expand Up @@ -124,4 +143,4 @@ def main():
parser.add_argument('--gradient-opacity', type=float, help='Set the gradient opacity in the volume rendering. Values range from 0.0 to 1.0.')
opts = parser.parse_args()

main(opts)
main()

0 comments on commit 1cc1be5

Please sign in to comment.