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

Add frame slider to IPyLeaflet widget #1595

Merged
merged 5 commits into from
Aug 5, 2024
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
37 changes: 34 additions & 3 deletions large_image/tilesource/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import importlib.util
import json
import os
import re
import weakref
from typing import Any, Dict, List, Optional, Tuple, Union, cast

Expand Down Expand Up @@ -161,7 +162,7 @@
:param resource: a girder resource path of an item or file that exists
on the girder client.
"""
self._layer = self._map = self._metadata = None
self._layer = self._map = self._metadata = self._frame_slider = None
self._ts = ts
if (not url or not metadata) and gc and (id or resource):
fileId = None
Expand Down Expand Up @@ -259,6 +260,7 @@
ipyleaflet layer, and the center of the tile source.
"""
from ipyleaflet import Map, basemaps, projections
from ipywidgets import IntSlider, VBox

try:
default_zoom = metadata['levels'] - metadata['sourceLevels']
Expand Down Expand Up @@ -307,6 +309,19 @@
else:
center = (metadata['sizeY'] / 2, metadata['sizeX'] / 2)

children: List[Any] = []
frames = metadata.get('frames')
if frames is not None:
self._frame_slider = IntSlider(
value=0,
min=0,
max=len(frames) - 1,
description='Frame:',
)
if self._frame_slider:
self._frame_slider.observe(self.update_frame, names='value')
children.append(self._frame_slider)

m = Map(
crs=crs,
basemap=basemaps.OpenStreetMap.Mapnik if self._geospatial else layer,
Expand All @@ -321,7 +336,9 @@
if self._geospatial:
m.add_layer(layer)
self._map = m
return m
children.append(m)

return VBox(children)

@property
def layer(self) -> Any:
Expand Down Expand Up @@ -376,6 +393,19 @@
return transf.transform(x, y)
return x, self._metadata['sizeY'] - y

def update_frame(self, event, **kwargs):
frame = int(event.get('new'))

Check warning on line 397 in large_image/tilesource/jupyter.py

View check run for this annotation

Codecov / codecov/patch

large_image/tilesource/jupyter.py#L397

Added line #L397 was not covered by tests
if self._layer:
if 'frame=' in self._layer.url:
self._layer.url = re.sub(r'frame=(\d+)', f'frame={frame}', self._layer.url)

Check warning on line 400 in large_image/tilesource/jupyter.py

View check run for this annotation

Codecov / codecov/patch

large_image/tilesource/jupyter.py#L400

Added line #L400 was not covered by tests
else:
if '?' in self._layer.url:
self._layer.url = self._layer.url.replace('?', f'?frame={frame}&')

Check warning on line 403 in large_image/tilesource/jupyter.py

View check run for this annotation

Codecov / codecov/patch

large_image/tilesource/jupyter.py#L403

Added line #L403 was not covered by tests
else:
self._layer.url += f'?frame={frame}'

Check warning on line 405 in large_image/tilesource/jupyter.py

View check run for this annotation

Codecov / codecov/patch

large_image/tilesource/jupyter.py#L405

Added line #L405 was not covered by tests

self._layer.redraw()

Check warning on line 407 in large_image/tilesource/jupyter.py

View check run for this annotation

Codecov / codecov/patch

large_image/tilesource/jupyter.py#L407

Added line #L407 was not covered by tests


def launch_tile_server(tile_source: IPyLeafletMixin, port: int = 0) -> Any:
import tornado.httpserver
Expand Down Expand Up @@ -420,10 +450,11 @@
x = int(self.get_argument('x'))
y = int(self.get_argument('y'))
z = int(self.get_argument('z'))
frame = int(self.get_argument('frame', default='0'))

Check warning on line 453 in large_image/tilesource/jupyter.py

View check run for this annotation

Codecov / codecov/patch

large_image/tilesource/jupyter.py#L453

Added line #L453 was not covered by tests
encoding = self.get_argument('encoding', 'PNG')
try:
tile_binary = manager.tile_source.getTile( # type: ignore[attr-defined]
x, y, z, encoding=encoding)
x, y, z, encoding=encoding, frame=frame)
except TileSourceXYZRangeError as e:
self.clear()
self.set_status(404)
Expand Down
14 changes: 14 additions & 0 deletions test/test_jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ def testJupyterIpyleafletGeospatial():
assert source._jupyter_server_manager.port == port


def testJupyterIpyleafletMultiFrame():
testDir = os.path.dirname(os.path.realpath(__file__))
imagePath = os.path.join(testDir, 'test_files', 'multi_channels.yml')
source = large_image.open(imagePath, projection='EPSG:3857')
assert source.frames

# get display in same method as _ipython_display_
display = source._map.make_map(
source.metadata, source.as_leaflet_layer(), source.getCenter(srs='EPSG:4326'),
)
# ensure first child is frame slider
assert display.children[0].description == 'Frame:'


def testJupyterIpyleaflet():
testDir = os.path.dirname(os.path.realpath(__file__))
imagePath = os.path.join(testDir, 'test_files', 'test_orient0.tif')
Expand Down