Skip to content

Commit

Permalink
Clean up StructureResultsPanel class
Browse files Browse the repository at this point in the history
  • Loading branch information
edan-bainglass committed Jan 11, 2025
1 parent 5d9ad7e commit 70a4d9a
Showing 1 changed file with 39 additions and 43 deletions.
82 changes: 39 additions & 43 deletions src/aiidalab_qe/app/result/components/viewer/structure/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,57 @@

class StructureResultsPanel(ResultsPanel[StructureResultsModel]):
def _render(self):
if not hasattr(self, "widget"):
structure = self._model.get_structure()
self.widget = StructureDataViewer(structure=structure)
# Select the Cell tab by default
self.widget.configuration_box.selected_index = 2
self.table_description = ipw.HTML("""
<h4 style='margin: 10px 0;'>
Structure table information: Atom coordinates in Å
</h4>
<p style='margin: 5px 0; color: #555;'>
You can click on a row to select an atom. Multiple atoms
can be selected by clicking on additional rows. To unselect
an atom, click on the selected row again.
</p>
""")
self.atom_coordinates_table = TableWidget()
self._generate_table(structure.get_ase())
if hasattr(self, "widget"):
# HACK to resize the NGL viewer in cases where it auto-rendered when its
# container was not displayed, which leads to a null width. This hack restores
# the original dimensions.
ngl = self.widget._viewer
ngl._set_size("100%", "300px")
ngl.control.zoom(0.0)
return

structure_info = self._get_structure_info(structure)
structure = self._model.get_structure()
self.widget = StructureDataViewer(structure=structure)

self.results_container.children = [
structure_info,
self.widget,
self.table_description,
self.atom_coordinates_table,
]
self.widget.configuration_box.selected_index = 2 # select the Cell tab

self.atom_coordinates_table = TableWidget()
self._generate_table(structure.get_ase())

self.atom_coordinates_table.observe(self._change_selection, "selected_rows")
# Listen for changes in self.widget.displayed_selection and update the table
self.widget.observe(self._update_table_selection, "displayed_selection")
structure_info = self._get_structure_info(structure)

ipw.link(
(self.widget, "displayed_selection"),
(self.atom_coordinates_table, "selected_rows"),
)

# HACK to resize the NGL viewer in cases where it auto-rendered when its
# container was not displayed, which leads to a null width. This hack restores
# the original dimensions.
ngl = self.widget._viewer
ngl._set_size("100%", "300px")
ngl.control.zoom(0.0)
self.results_container.children = [
structure_info,
self.widget,
ipw.HTML("""
<h4 style='margin: 10px 0;'>
Structure information: Atom coordinates in Å
</h4>
<p style='margin: 5px 0; color: #555;'>
You can click on a row to select an atom. Multiple atoms can be
selected by clicking on additional rows. To unselect an atom, click
on the selected row again.
</p>
"""),
self.atom_coordinates_table,
]

def _get_structure_info(self, structure):
formatted = format_time(structure.ctime)
relative = relative_time(structure.ctime)
return ipw.HTML(
f"""
<div style='line-height: 1.4;'>
<strong>PK:</strong> {structure.pk}<br>
<strong>Label:</strong> {structure.label}<br>
<strong>Description:</strong> {structure.description}<br>
<strong>Number of atoms:</strong> {len(structure.sites)}<br>
<strong>Creation time:</strong> {format_time(structure.ctime)} ({relative_time(structure.ctime)})<br>
<strong>Creation time:</strong> {formatted} ({relative})<br>
</div>
"""
)
Expand All @@ -79,15 +83,7 @@ def _generate_table(self, structure):
for index, (symbol, tag, position) in enumerate(
zip(chemical_symbols, tags, positions), start=1
):
# Format position values to two decimal places
formatted_position = [f"{coord:.2f}" for coord in position]
data.append([index, symbol, tag, *formatted_position])
self.atom_coordinates_table.data = data

def _change_selection(self, _):
selected_indices = self.atom_coordinates_table.selected_rows
self.widget.displayed_selection = selected_indices

def _update_table_selection(self, change):
selected_indices = change.new
self.atom_coordinates_table.selected_rows = selected_indices
self.atom_coordinates_table.data = data

0 comments on commit 70a4d9a

Please sign in to comment.