Skip to content

Commit

Permalink
moved repr_html code to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-mueller committed Apr 12, 2024
1 parent b945fdb commit da9d644
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 105 deletions.
113 changes: 8 additions & 105 deletions src/omero/gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
from omero.cmd import Chgrp2, Delete2, DoAll, SkipHead, Chown2
from omero.cmd.graphs import ChildOption
from omero.api import Save
from omero.gateway.utils import ServiceOptsDict, GatewayConfig, toBoolean
from omero.gateway.utils import ServiceOptsDict, GatewayConfig, toBoolean, image_to_html
from omero.model.enums import PixelsTypeint8, PixelsTypeuint8, PixelsTypeint16
from omero.model.enums import PixelsTypeuint16, PixelsTypeint32
from omero.model.enums import PixelsTypeuint32, PixelsTypefloat
Expand Down Expand Up @@ -291,6 +291,13 @@ def __init__(self, conn=None, obj=None, cache=None, **kwargs):
self._conn.SERVICE_OPTS)
self.__prepare__(**kwargs)

def _repr_html_(self):
"""
Returns an HTML representation of the object. This is used by the
IPython notebook to display the object in a cell.
"""
return image_to_html(self)

def __eq__(self, a):
"""
Returns true if the object is of the same type and has same id and name
Expand Down Expand Up @@ -10426,110 +10433,6 @@ def getROICount(self, shapeType=None, filterByCurrentUser=False):
return count[0][0].getValue()
return len(self._get_rois(shapeType, filterByCurrentUser))

def _repr_html_(self):
import base64

html_style_header = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Details</title>
<style>
img {
min-width: 250px; /* Set the minimum width for all images */
min-height: 250px; /* Set the minimum height for all images */
}
.align-top {
vertical-align: top;
}
.text-right {
text-align: right;
}
</style>
</head>
"""

def obj_html(obj, otype):
return f"""<tr>
<td><b>{otype}</b></td>
<td class=text-right>{obj.id if obj else ""}</td>
<td class=text-right>{obj.name if obj else ""}</td>
</tr>
"""

# create a sub-table for image information
table_imageinfo = f"""
<table>
<tr><th></th><th>ID</th><th>Name</th></tr>
{obj_html(self, 'Image')}
{obj_html(self.getParent(), 'Dataset')}
{obj_html(self.getProject(), 'Project')}
</table>
"""

# get entries for thumbnail and dimensions
encoded_image = base64.b64encode(self.getThumbnail()).decode('utf-8')
dimensions = f"""(
{self.getSizeT()},
{self.getSizeC()},
{self.getSizeZ()},
{self.getSizeY()},
{self.getSizeX()})"""
physical_dims = """({:.3f}, {:.3f}, {:.3f})""".format(
self.getPixelSizeZ(),
self.getPixelSizeY(),
self.getPixelSizeX())
physical_units = f"""(
{self.getPixelSizeZ(units=True).getUnit()},
{self.getPixelSizeY(units=True).getUnit()},
{self.getPixelSizeX(units=True).getUnit()})"""

table_dimensions = f"""
<table>\n
<tr>\n
<td><b>Dimensions (TCZYX): </b></td> <td class=text-right>{dimensions}</td>\n
</tr>\n
<tr>\n
<td><b>Voxel/Pixel dimensions (ZYX): </b></td> <td class=text-right>{physical_dims}</td>\n
</tr>\n
<tr>\n
<td><b>Physical units: </b></td> <td class=text-right>{physical_units}</td>\n
</tr>\n
<tr>\n
<td><b>Channel Names: </b></td> <td class=text-right>{self.getChannelLabels()}</td>\n
</tr>\n
</table>
"""

table_assembly = f"""
<table>
<tr>
<td><div class="thumbnail">
<img src="data:image/jpeg;base64,{encoded_image}" alt="Thumbnail">
</div></td>
<td class="align-top"><h2>Image information </h2>
{table_imageinfo}
</td>
</tr>
</table>
<table>
<tr>
<td>{table_dimensions}</td>
</tr>
</table>
"""

return '\n'.join([
html_style_header,
'<body>',
table_assembly,
'</body>',
'</html>'
])


ImageWrapper = _ImageWrapper

# INSTRUMENT AND ACQUISITION #
Expand Down
103 changes: 103 additions & 0 deletions src/omero/gateway/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,106 @@ def propertiesToDict(m, prefix=None):
except:
d[items[-1]] = value
return nested_dict

def image_to_html(image):
import base64

html_style_header = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Details</title>
<style>
img {
min-width: 250px; /* Set the minimum width for all images */
min-height: 250px; /* Set the minimum height for all images */
}
.align-top {
vertical-align: top;
}
.text-right {
text-align: right;
}
</style>
</head>
"""

def obj_html(obj, otype):
return f"""<tr>
<td><b>{otype}</b></td>
<td class=text-right>{obj.id if obj else ""}</td>
<td class=text-right>{obj.name if obj else ""}</td>
</tr>
"""

# create a sub-table for image information
table_imageinfo = f"""
<table>
<tr><th></th><th>ID</th><th>Name</th></tr>
{obj_html(image, 'Image')}
{obj_html(image.getParent(), 'Dataset')}
{obj_html(image.getProject(), 'Project')}
</table>
"""

# get entries for thumbnail and dimensions
encoded_image = base64.b64encode(image.getThumbnail()).decode('utf-8')
dimensions = f"""(
{image.getSizeT()},
{image.getSizeC()},
{image.getSizeZ()},
{image.getSizeY()},
{image.getSizeX()})"""
physical_dims = """({:.3f}, {:.3f}, {:.3f})""".format(
image.getPixelSizeZ(),
image.getPixelSizeY(),
image.getPixelSizeX())
physical_units = f"""(
{image.getPixelSizeZ(units=True).getUnit()},
{image.getPixelSizeY(units=True).getUnit()},
{image.getPixelSizeX(units=True).getUnit()})"""

table_dimensions = f"""
<table>\n
<tr>\n
<td><b>Dimensions (TCZYX): </b></td> <td class=text-right>{dimensions}</td>\n
</tr>\n
<tr>\n
<td><b>Voxel/Pixel dimensions (ZYX): </b></td> <td class=text-right>{physical_dims}</td>\n
</tr>\n
<tr>\n
<td><b>Physical units: </b></td> <td class=text-right>{physical_units}</td>\n
</tr>\n
<tr>\n
<td><b>Channel Names: </b></td> <td class=text-right>{image.getChannelLabels()}</td>\n
</tr>\n
</table>
"""

table_assembly = f"""
<table>
<tr>
<td><div class="thumbnail">
<img src="data:image/jpeg;base64,{encoded_image}" alt="Thumbnail">
</div></td>
<td class="align-top"><h2>Image information </h2>
{table_imageinfo}
</td>
</tr>
</table>
<table>
<tr>
<td>{table_dimensions}</td>
</tr>
</table>
"""

return '\n'.join([
html_style_header,
'<body>',
table_assembly,
'</body>',
'</html>'
])

0 comments on commit da9d644

Please sign in to comment.