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

Register routers through FPS #102

Closed
Closed
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
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ toolz
uvicorn
xarray>=0.16
zarr
fps
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ select = B,C,E,F,W,T4,B9

[isort]
known_first_party=xpublish
known_third_party=cachey,dask,fastapi,numcodecs,numpy,pandas,pkg_resources,pytest,setuptools,sphinx_autosummary_accessors,starlette,uvicorn,xarray,zarr
known_third_party=cachey,dask,fastapi,fps,numcodecs,numpy,pandas,pkg_resources,pytest,setuptools,sphinx_autosummary_accessors,starlette,uvicorn,xarray,zarr
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
Expand Down
7 changes: 7 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
package_dir={'xpublish': 'xpublish'},
include_package_data=True,
install_requires=install_requires,
entry_points={
'fps_router': [
'xpublish-base = xpublish.routers.base',
'xpublish-common = xpublish.routers.common',
'xpublish-zarr = xpublish.routers.zarr',
]
},
license='MIT',
zip_safe=False,
keywords=['xarray', 'zarr', 'api'],
Expand Down
8 changes: 8 additions & 0 deletions xpublish/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ def serve(self, host='0.0.0.0', port=9000, log_level='debug', **kwargs):
"""
uvicorn.run(self.app, host=host, port=port, log_level=log_level, **kwargs)

def serve_in_jupyter(self):
"""Serve via Jupyverse."""
from fps.main import app as fps_app

fps_app.dependency_overrides[get_dataset_ids] = lambda: list(self._datasets)
fps_app.dependency_overrides[get_dataset] = self._get_dataset_func
fps_app.dependency_overrides[get_cache] = lambda: self.cache


@xr.register_dataset_accessor('rest')
class RestAccessor:
Expand Down
12 changes: 8 additions & 4 deletions xpublish/routers/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import xarray as xr
from fastapi import APIRouter, Depends
from fps.hooks import register_router
from starlette.responses import HTMLResponse
from zarr.storage import attrs_key

Expand All @@ -8,25 +9,25 @@
base_router = APIRouter()


@base_router.get('/')
@base_router.get('/xpublish')
def html_representation(dataset: xr.Dataset = Depends(get_dataset)):
"""Returns a HTML representation of the dataset."""

with xr.set_options(display_style='html'):
return HTMLResponse(dataset._repr_html_())


@base_router.get('/keys')
@base_router.get('/xpublish/keys')
def list_keys(dataset: xr.Dataset = Depends(get_dataset)):
return list(dataset.variables)


@base_router.get('/dict')
@base_router.get('/xpublish/dict')
def to_dict(dataset: xr.Dataset = Depends(get_dataset)):
return dataset.to_dict(data=False)


@base_router.get('/info')
@base_router.get('/xpublish/info')
def info(
dataset: xr.Dataset = Depends(get_dataset),
zvariables: dict = Depends(get_zvariables),
Expand All @@ -52,3 +53,6 @@ def info(
info['global_attributes'] = meta[attrs_key]

return info


r = register_router(base_router)
6 changes: 5 additions & 1 deletion xpublish/routers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
import sys

from fastapi import APIRouter, Depends
from fps.hooks import register_router

from ..dependencies import get_dataset_ids
from ..utils.info import get_sys_info, netcdf_and_hdf5_versions

common_router = APIRouter()


@common_router.get('/versions')
@common_router.get('/xpublish/versions')
def get_versions():
versions = dict(get_sys_info() + netcdf_and_hdf5_versions())
modules = [
Expand Down Expand Up @@ -46,3 +47,6 @@ def get_versions():
@dataset_collection_router.get('/datasets')
def get_dataset_collection_keys(ids: list = Depends(get_dataset_ids)):
return ids


r = register_router(common_router)
12 changes: 8 additions & 4 deletions xpublish/routers/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cachey
import xarray as xr
from fastapi import APIRouter, Depends, HTTPException
from fps.hooks import register_router
from starlette.responses import Response
from zarr.storage import array_meta_key, attrs_key, group_meta_key

Expand All @@ -17,7 +18,7 @@
zarr_router = APIRouter()


@zarr_router.get(f'/{zarr_metadata_key}')
@zarr_router.get(f'/xpublish/{zarr_metadata_key}')
def get_zmetadata(
dataset: xr.Dataset = Depends(get_dataset), zmetadata: dict = Depends(_get_zmetadata)
):
Expand All @@ -26,19 +27,19 @@ def get_zmetadata(
return Response(json.dumps(zjson).encode('ascii'), media_type='application/json')


@zarr_router.get(f'/{group_meta_key}')
@zarr_router.get(f'/xpublish/{group_meta_key}')
def get_zgroup(zmetadata: dict = Depends(_get_zmetadata)):

return zmetadata['metadata'][group_meta_key]


@zarr_router.get(f'/{attrs_key}')
@zarr_router.get(f'/xpublish/{attrs_key}')
def get_zattrs(zmetadata: dict = Depends(_get_zmetadata)):

return zmetadata['metadata'][attrs_key]


@zarr_router.get('/{var}/{chunk}')
@zarr_router.get('/xpublish/{var}/{chunk}')
def get_variable_chunk(
var: str,
chunk: str,
Expand Down Expand Up @@ -84,3 +85,6 @@ def get_variable_chunk(
cache.put(cache_key, response, ct.time, len(echunk))

return response


r = register_router(zarr_router)