File: to_html.py

package info (click to toggle)
python-loompy 3.0.7%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,272 kB
  • sloc: python: 3,152; sh: 63; makefile: 16
file content (62 lines) | stat: -rw-r--r-- 1,846 bytes parent folder | download | duplicates (2)
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
from typing import *


def to_html(ds: Any) -> str:
	"""
	Return an HTML representation of the loom file or view, showing the upper-left 10x10 corner.
	"""
	rm = min(10, ds.shape[0])
	cm = min(10, ds.shape[1])
	html = "<p>"
	if ds.attrs.__contains__("title"):
		html += "<strong>" + ds.attrs["title"] + "</strong> "
	html += f"{ds.shape[0]} rows, {ds.shape[1]} columns, {len(ds.layers)} layer{'s' if len(ds.layers) > 1 else ''}<br/>(showing up to 10x10)<br/>"
	html += ds.filename + "<br/>"
	for (name, val) in ds.attrs.items():
		html += f"name: <em>{val}</em><br/>"
	html += "<table>"
	# Emit column attributes
	for ca in ds.col_attrs.keys():
		html += "<tr>"
		for ra in ds.row_attrs.keys():
			html += "<td>&nbsp;</td>"  # Space for row attrs
		html += "<td><strong>" + ca + "</strong></td>"  # Col attr name
		for v in ds.col_attrs[ca][:cm]:
			html += "<td>" + str(v) + "</td>"
		if ds.shape[1] > cm:
			html += "<td>...</td>"
		html += "</tr>"

	# Emit row attribute names
	html += "<tr>"
	for ra in ds.row_attrs.keys():
		html += "<td><strong>" + ra + "</strong></td>"  # Row attr name
	html += "<td>&nbsp;</td>"  # Space for col attrs
	for v in range(cm):
		html += "<td>&nbsp;</td>"
	if ds.shape[1] > cm:
		html += "<td>...</td>"
	html += "</tr>"

	# Emit row attr values and matrix values
	for row in range(rm):
		html += "<tr>"
		for ra in ds.row_attrs.keys():
			html += "<td>" + str(ds.row_attrs[ra][row]) + "</td>"
		html += "<td>&nbsp;</td>"  # Space for col attrs

		for v in ds[row, :cm]:
			html += "<td>" + str(v) + "</td>"
		if ds.shape[1] > cm:
			html += "<td>...</td>"
		html += "</tr>"
	# Emit ellipses
	if ds.shape[0] > rm:
		html += "<tr>"
		for v in range(rm + 1 + len(ds.row_attrs.keys())):
			html += "<td>...</td>"
		if ds.shape[1] > cm:
			html += "<td>...</td>"
		html += "</tr>"
	html += "</table>"
	return html