Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View in validator #285

Merged
merged 2 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/source/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ Use the `ome_zarr` command to interrogate Zarr datasets::
# Local data (after downloading as below)
$ ome_zarr info 6001240.zarr/

view
====

Use the `ome_zarr` command to view Zarr data in the https://ome.github.io/ome-ngff-validator::

# Local data (after downloading as below)
$ ome_zarr view 6001240.zarr/

download
========

Expand Down
13 changes: 13 additions & 0 deletions ome_zarr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .scale import Scaler
from .utils import download as zarr_download
from .utils import info as zarr_info
from .utils import view as zarr_view


def config_logging(loglevel: int, args: argparse.Namespace) -> None:
Expand All @@ -29,6 +30,12 @@ def info(args: argparse.Namespace) -> None:
list(zarr_info(args.path, stats=args.stats))


def view(args: argparse.Namespace) -> None:
"""Wrap the :func:`~ome_zarr.utils.view` method."""
config_logging(logging.WARN, args)
zarr_view(args.path, args.port)


def download(args: argparse.Namespace) -> None:
"""Wrap the :func:`~ome_zarr.utils.download` method."""
config_logging(logging.WARN, args)
Expand Down Expand Up @@ -105,6 +112,12 @@ def main(args: Union[List[str], None] = None) -> None:
parser_download.add_argument("--output", default=".")
parser_download.set_defaults(func=download)

# view (in ome-ngff-validator in a browser)
parser_view = subparsers.add_parser("view")
parser_view.add_argument("path")
parser_view.add_argument("--port", type=int, default=8000)
parser_view.set_defaults(func=view)

# create
parser_create = subparsers.add_parser("create")
parser_create.add_argument(
Expand Down
36 changes: 36 additions & 0 deletions ome_zarr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import json
import logging
import os
import webbrowser
from http.server import ( # type: ignore[attr-defined]
HTTPServer,
SimpleHTTPRequestHandler,
test,
)
from pathlib import Path
from typing import Iterator, List

Expand Down Expand Up @@ -45,6 +52,35 @@ def info(path: str, stats: bool = False) -> Iterator[Node]:
yield node


def view(input_path: str, port: int = 8000) -> None:
# serve the parent directory in a simple server with CORS. Open browser

parent_dir, image_name = os.path.split(input_path)
parent_dir = str(parent_dir)

class CORSRequestHandler(SimpleHTTPRequestHandler):
def end_headers(self) -> None:
self.send_header("Access-Control-Allow-Origin", "*")
SimpleHTTPRequestHandler.end_headers(self)

def translate_path(self, path: str) -> str:
# Since we don't call the class constructor ourselves,
# we set the directory here instead
self.directory = parent_dir
super_path = super().translate_path(path)
return super_path

# open ome-ngff-validator in a web browser...
url = (
f"https://ome.github.io/ome-ngff-validator/"
f"?source=http://localhost:{port}/{image_name}"
)
webbrowser.open(url)

# ...then start serving content
test(CORSRequestHandler, HTTPServer, port=port)


def download(input_path: str, output_dir: str = ".") -> None:
"""Download an OME-Zarr from the given path.

Expand Down