diff --git a/caterva2/services/plugins/image/__init__.py b/caterva2/services/plugins/image/__init__.py new file mode 100644 index 00000000..5bf25bfe --- /dev/null +++ b/caterva2/services/plugins/image/__init__.py @@ -0,0 +1,128 @@ +import ast +import pathlib + +# Requirements +import jinja2 +import numpy as np +import PIL.Image +from fastapi import Depends, FastAPI, Request, responses +from fastapi.responses import HTMLResponse +from fastapi.templating import Jinja2Templates + +# Project +from caterva2.services import db +from caterva2.services.server import get_container, optional_user, resize_image +from caterva2.services.server import templates as sub_templates + +app = FastAPI() +BASE_DIR = pathlib.Path(__file__).resolve().parent +templates = Jinja2Templates(directory=BASE_DIR / "templates") +templates.env.loader = jinja2.ChoiceLoader( + [ + templates.env.loader, # Preserve the original loader + sub_templates.env.loader, # Add the sub-templates loader + ] +) + +name = "image" # Identifies the plugin +label = "Image" +contenttype = "image" + + +urlbase = None + + +def init(urlbase_): + global urlbase + urlbase = urlbase_ + + +def url(path: str) -> str: + return f"{urlbase}/{path}" + + +def guess(path: pathlib.Path, meta) -> bool: + """Does dataset (given path and metadata) seem of this content type?""" + if not hasattr(meta, "dtype"): + return False # not an array + + dtype = meta.dtype + if dtype is None: + return False + + # Structured dtype + if isinstance(dtype, str) and dtype.startswith("["): + dtype = eval(dtype) # TODO Make it safer + + # Sometimes dtype is a tuple (e.g. (' + + + {% with id="image-spinner" %} + {% include 'includes/loading.html' %} + {% endwith %} + + +{{ width }} x {{ height }} (original size) +
+ + + diff --git a/caterva2/services/server.py b/caterva2/services/server.py index 7570aace..93154f33 100644 --- a/caterva2/services/server.py +++ b/caterva2/services/server.py @@ -2609,13 +2609,18 @@ def main(): # Register display plugins (delay module load) try: - from .plugins import tomography # When used as module + from .plugins import image, tomography # When used as module except ImportError: - from caterva2.services.plugins import tomography # When used as script + from caterva2.services.plugins import image, tomography # When used as script + # tomography app.mount(f"/plugins/{tomography.name}", tomography.app) plugins[tomography.contenttype] = tomography tomography.init(settings.urlbase) + # image + app.mount(f"/plugins/{image.name}", image.app) + plugins[image.contenttype] = image + image.init(settings.urlbase) # Mount media media = settings.statedir / "media"