Skip to content

Commit 87dfb17

Browse files
committed
Create test for GenericArrayDataTableWidget
1 parent d69b2a6 commit 87dfb17

2 files changed

Lines changed: 51 additions & 9 deletions

File tree

src/alc_aiidalab_widgets/widgets/tables.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,14 @@ def _render_array(self, change: dict) -> None:
8787
if len(values.shape) > 2:
8888
self.children = [
8989
self.array_selector,
90-
HTML("<p>To many dimension to create 2D table from array.</p>"),
90+
HTML("<p>To many dimensions to create 2D table from array.</p>"),
9191
]
9292
return
9393
if len(values.shape) == 1:
9494
nrows = values.shape[0]
9595
ncols = 1
9696
else:
9797
ncols, nrows = values.shape # type: ignore
98-
values = values.reshape(1, -1)
99-
100-
# Unique styling prefix to prevent CSS bleeding into other Jupyter elements
10198

10299
# Build Table Header (Column Indices)
103100
html = "<table style='width:100%; border: 1px solid #ddd; text-align: left; "
@@ -118,11 +115,19 @@ def _render_array(self, change: dict) -> None:
118115
for r in range(nrows):
119116
html += "<tr>"
120117
html += f'<th class="row-idx">{r}</th>'
121-
for c in range(ncols):
122-
val = values[c, r]
123-
# Format floats to 6 decimals, leave ints/strings as they are
118+
if ncols > 1:
119+
for c in range(ncols):
120+
formatted_val = (
121+
f"{values[c, r]}"
122+
if isinstance(values[c, r], float | npfloat)
123+
else str(values[c, r])
124+
)
125+
html += f"<td>{formatted_val}</td>"
126+
else:
124127
formatted_val = (
125-
f"{val:.6f}" if isinstance(val, float | npfloat) else str(val)
128+
f"{values[r]:.6f}"
129+
if isinstance(values[r], float | npfloat)
130+
else str(values[r])
126131
)
127132
html += f"<td>{formatted_val}</td>"
128133
html += "</tr>"

tests/test_tables.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
from aiida.orm import ArrayData
55
from ipywidgets import HTML, Dropdown
66

7-
from alc_aiidalab_widgets.widgets.tables import XYZArrayDataTableWidget
7+
from alc_aiidalab_widgets.widgets.tables import (
8+
GenericArrayDataTableWidget,
9+
XYZArrayDataTableWidget,
10+
)
811

912

1013
def test_xyz_arraydata_table():
@@ -26,3 +29,37 @@ def test_xyz_arraydata_table():
2629
assert "4263.123456" in widget.children[1].value
2730
assert widget.children[0].value == "Array_2"
2831
assert "1.000000" in widget.children[1].value
32+
33+
34+
def test_generic_arraydata_table():
35+
"""Test the basic arraydata table viewer."""
36+
array = ArrayData()
37+
array1 = numpy.array([0, 1, 2, 32, 4, 5], dtype=float)
38+
array2 = numpy.array([0, 1, 2, 32, 4, 5], dtype=int)
39+
array3 = numpy.array([[0, 1, 2], [32, 4, 5]], dtype=int)
40+
array4 = numpy.zeros((2, 2, 2), dtype=float)
41+
array.set_array("Array_1", array1)
42+
array.set_array("Array_2", array2)
43+
array.set_array("Array_3", array3)
44+
array.set_array("Array_4", array4)
45+
widget = GenericArrayDataTableWidget(array)
46+
assert isinstance(widget.children[0], Dropdown)
47+
assert isinstance(widget.children[1], HTML)
48+
assert "32.00000" in widget.children[1].value
49+
assert "0.000000" in widget.children[1].value
50+
assert widget.children[0].value == "Array_1"
51+
52+
widget.array_selector.index = 1
53+
assert "32" in widget.children[1].value
54+
assert widget.children[0].value == "Array_2"
55+
assert "1" in widget.children[1].value
56+
assert "Array 2" in widget.children[1].value
57+
58+
widget.array_selector.index = 2
59+
assert widget.children[0].value == "Array_3"
60+
assert "32" in widget.children[1].value
61+
assert "1" in widget.children[1].value
62+
assert "Array_3" not in widget.children[1].value
63+
64+
widget.array_selector.index = 3
65+
assert "To many dimensions" in widget.children[1].value

0 commit comments

Comments
 (0)