File size: 2,243 Bytes
227ab66 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
from __future__ import annotations
from typing import Any
from gradio.components.base import Component
from gradio.data_classes import GradioModel
class NiiVueViewerData(GradioModel):
background_url: str | None = None
overlay_url: str | None = None
class NiiVueViewer(Component):
"""WebGL NIfTI viewer using NiiVue."""
data_model = NiiVueViewerData
def __init__(
self,
value: NiiVueViewerData | dict[str, Any] | None = None,
*,
label: str | None = None,
height: int = 500,
show_label: bool = True,
container: bool = True,
scale: int | None = None,
min_width: int = 160,
visible: bool = True,
elem_id: str | None = None,
elem_classes: list[str] | str | None = None,
render: bool = True,
key: int | str | tuple[int | str, ...] | None = None,
):
self.height = height
super().__init__(
label=label,
show_label=show_label,
container=container,
scale=scale,
min_width=min_width,
visible=visible,
elem_id=elem_id,
elem_classes=elem_classes,
render=render,
key=key,
value=value,
)
def preprocess(self, payload: NiiVueViewerData | None) -> dict[str, Any] | None:
if payload is None:
return None
return {
"background_url": payload.background_url,
"overlay_url": payload.overlay_url,
}
def postprocess(self, value: dict[str, Any] | None) -> NiiVueViewerData | None:
if value is None:
return None
# Handle dict input (typical usage in app)
return NiiVueViewerData(
background_url=value.get("background_url"),
overlay_url=value.get("overlay_url"),
)
def example_payload(self) -> Any:
return {
"background_url": "https://niivue.github.io/niivue/images/mni152.nii.gz",
"overlay_url": None,
}
def example_value(self) -> Any:
return {
"background_url": "https://niivue.github.io/niivue/images/mni152.nii.gz",
"overlay_url": None,
}
|