-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add server extension using h5grove to replace jupyterlab_hdf
- Loading branch information
Showing
12 changed files
with
201 additions
and
40 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
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
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
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"NotebookApp": { | ||
"nbserver_extensions": { | ||
"jupyterlab_hdf": true | ||
"jupyterlab_h5web": true | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,19 @@ | ||
from .widget import H5Web | ||
|
||
|
||
def _jupyter_server_extension_points(): | ||
return [{"module": "jupyterlab_h5web"}] | ||
|
||
|
||
def _load_jupyter_server_extension(server_app): | ||
"""Registers the API handler to receive HTTP requests from the frontend extension.""" | ||
from .handlers import setup_handlers | ||
|
||
setup_handlers(server_app.web_app, server_app.notebook_dir) | ||
server_app.log.info( | ||
f"Jupyterlab-h5web extension will serve HDF5 files from {server_app.notebook_dir}" | ||
) | ||
|
||
|
||
# For backward compatibility with notebook server - useful for Binder/JupyterHub | ||
load_jupyter_server_extension = _load_jupyter_server_extension |
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,72 @@ | ||
import h5py | ||
import tornado.web | ||
from jupyter_server.base.handlers import APIHandler | ||
from jupyter_server.utils import url_path_join | ||
from pathlib import Path | ||
|
||
from h5grove.encoders import encode | ||
from h5grove.content import create_content, ResolvedEntityContent, DatasetContent | ||
|
||
from .utils import as_absolute_path | ||
|
||
|
||
class BaseHandler(APIHandler): | ||
def initialize(self, base_dir: Path) -> None: | ||
self.base_dir = base_dir | ||
|
||
@tornado.web.authenticated | ||
def get(self, file_path: str): | ||
path = self.get_query_argument("path", None) | ||
format = self.get_query_argument("format", None) | ||
|
||
with h5py.File(as_absolute_path(self.base_dir, Path(file_path)), "r") as h5file: | ||
content = self.get_content(h5file, path) | ||
|
||
encoded_content_chunks, headers = encode(content, format) | ||
|
||
for key, value in headers.items(): | ||
self.set_header(key, value) | ||
for chunk in encoded_content_chunks: | ||
self.write(chunk) | ||
self.finish() | ||
|
||
def get_content(self, h5file, path): | ||
raise NotImplementedError | ||
|
||
|
||
class AttributeHandler(BaseHandler): | ||
def get_content(self, h5file, path): | ||
content = create_content(h5file, path) | ||
assert isinstance(content, ResolvedEntityContent) | ||
return content.attributes() | ||
|
||
|
||
class DataHandler(BaseHandler): | ||
def get_content(self, h5file, path): | ||
selection = self.get_query_argument("selection", None) | ||
|
||
content = create_content(h5file, path) | ||
assert isinstance(content, DatasetContent) | ||
return content.data(selection) | ||
|
||
|
||
class MetadataHandler(BaseHandler): | ||
def get_content(self, h5file, path): | ||
content = create_content(h5file, path) | ||
return content.metadata() | ||
|
||
|
||
def setup_handlers(web_app, base_dir: str): | ||
pattern = "(.*)" | ||
endpoints = {"attr": AttributeHandler, "data": DataHandler, "meta": MetadataHandler} | ||
|
||
handlers = [ | ||
( | ||
url_path_join(web_app.settings["base_url"], "h5web", endpoint, pattern), | ||
handler, | ||
{"base_dir": Path(base_dir)}, | ||
) | ||
for endpoint, handler in endpoints | ||
] | ||
|
||
web_app.add_handlers(pattern, handlers) |
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,8 @@ | ||
from pathlib import Path | ||
|
||
|
||
def as_absolute_path(base_dir: Path, file_path: Path) -> Path: | ||
if file_path.is_absolute(): | ||
return file_path | ||
|
||
return base_dir / file_path |
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
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
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
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
Oops, something went wrong.