8000 Get rid of pandas dependency by yakutovicha · Pull Request #222 · aiidalab/aiidalab-home · GitHub
[go: up one dir, main page]

Skip to content
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
111 changes: 86 additions & 25 deletions home/process.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import inspect
import os
import re
import sys
import threading
import time
Expand All @@ -13,7 +14,6 @@
from collections.abc import Mapping

import ipywidgets as ipw
import pandas as pd
import traitlets as tl

# AiiDA imports
Expand All @@ -27,6 +27,7 @@
from aiida.common.links import LinkType
from aiida.tools.query.calculation import CalculationQueryBuilder
from IPython.display import HTML, Javascript, clear_output, display
from jinja2 import Template


class CantRegisterCallbackError(Exception):
Expand All @@ -38,6 +39,85 @@ def __init__(self, function):
)


PROCESS_TABLE_TEMPLATE = Template(
"""
<style>
.df { border: none; }
.df tbody tr:nth-child(odd) { background-color: #e5e7e9; }
.df tbody tr:nth-child(odd):hover { background-color: #f5b7b1; }
.df tbody tr:nth-child(even):hover { background-color: #f5b7b1; }
.df tbody td { min-width: 150px; text-align: center; border: none; }
.df th { text-align: center; border: none; border-bottom: 1px solid black; }
</style>
<table class="df">
<thead>
<tr>
{% for header in headers %}
<th>{{ header }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for row in rows %}
<tr>
{% for header in headers %}
<td>{{ row[header] }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
"""
)


def _stringify_process_cell(value):
if value is None:
return ""
return str(value)


def _normalize_process_rows(projected):
if not projected:
return [], []

headers = list(projected[0])
rows = [
{
header: _stringify_process_cell(value)
for header, value in zip(headers, entry)
}
for entry in projected[1:]
]
return headers, rows


def _filter_process_rows(rows, description_contains):
if not description_contains:
return rows

pattern = re.compile(description_contains)
return [row for row in rows if pattern.search(row.get("Description", ""))]


def _add_process_links(rows, path_to_root):
linked_rows = []
for row in rows:
linked_row = dict(row)
pk = linked_row.get("PK", "")
linked_row["PK"] = (
f"""<a href={path_to_root}home/process.ipynb?id={pk} target="_blank">{pk}</a>"""
if pk
else ""
)
linked_rows.append(linked_row)
return linked_rows


def _render_process_table(headers, rows):
return PROCESS_TABLE_TEMPLATE.render(headers=headers, rows=rows)


def get_running_calcs(process):
"""Takes a process and yeilds running children calculations."""

Expand Down Expand Up @@ -576,22 +656,6 @@ def __init__(self, path_to_root="../", **kwargs):

def update(self, _=None):
"""Perform the query."""
pd.set_option("max_colwidth", 40)
# Here we are defining properties of 'df' class (specified while exporting pandas table into html).
# Since the exported object is nothing more than HTML table, all 'standard' HTML table settings
# can be applied to it as well.
# For more information on how to controle the table appearance please visit:
# https://css-tricks.com/complete-guide-table-element/
self.table.value = """
<style>
.df { border: none; }
.df tbody tr:nth-child(odd) { background-color: #e5e7e9; }
.df tbody tr:nth-child(odd):hover { background-color: #f5b7b1; }
.df tbody tr:nth-child(even):hover { background-color: #f5b7b1; }
.df tbody td { min-width: 150px; text-align: center; border: none }
.df th { text-align: center; border: none; border-bottom: 1px solid black;}
</style>
"""
builder = CalculationQueryBuilder()
filters = builder.get_filters(
all_entries=False,
Expand Down Expand Up @@ -630,19 +694,16 @@ def update(self, _=None):
"description",
],
)
dataf = pd.DataFrame(projected[1:], columns=projected[0])
headers, rows = _normalize_process_rows(projected)

# Keep only process that contain the requested string in the description.
if self.description_contains:
dataf = dataf[dataf.Description.str.contains(self.description_contains)]
rows = _filter_process_rows(rows, self.description_contains)

self.output.value = f"{len(dataf)} processes shown"
self.output.value = f"{len(rows)} processes shown"

# Add HTML links.
dataf["PK"] = dataf["PK"].apply(
lambda x: f"""<a href={self.path_to_root}home/process.ipynb?id={x} target="_blank">{x}</a>"""
)
self.table.value += dataf.to_html(classes="df", escape=False, index=False)
rows = _add_process_links(rows, self.path_to_root)
self.table.value = _render_process_table(headers, rows)

@tl.validate("incoming_node")
def _validate_incoming_node(self, provided):
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ install_requires =
humanfriendly~=10.0
ipython~=7.0
ipywidgets~=8.0
pandas~=2.2
traitlets~=5.0
nbclassic~=1.3
pexpect~=4.9
Expand Down
44 changes: 44 additions & 0 deletions tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,47 @@ def test_process_list_widget(multiply_add_completed_workchain):
widget.update()
assert "processes shown" in widget.output.value
assert "<table" in widget.table.value
assert (
f"home/process.ipynb?id={multiply_add_completed_workchain.pk}"
in widget.table.value
)


def test_process_list_widget_filters_descriptions(generate_calc_job_node):
matching_process = generate_calc_job_node(inputs={"parameters": orm.Int(1)})
matching_process.description = "calc-42 complete"

other_process = generate_calc_job_node(inputs={"parameters": orm.Int(2)})
other_process.description = "skip me"

process_without_description = generate_calc_job_node(
inputs={"parameters": orm.Int(3)}
)

widget = home_process.ProcessListWidget()
widget.description_contains = r"calc-\d+"
widget.update()

assert widget.output.value == "1 processes shown"
assert "calc-42 complete" in widget.table.value
assert "skip me" not in widget.table.value
assert f"home/process.ipynb?id={matching_process.pk}" in widget.table.value
assert f"home/process.ipynb?id={other_process.pk}" not in widget.table.value
assert (
f"home/process.ipynb?id={process_without_description.pk}"
not in widget.table.value
)


def test_process_list_widget_renders_empty_results(multiply_add_completed_workchain):
widget = home_process.ProcessListWidget()
widget.process_label = "definitely-no-such-process-label"
widget.update()

assert widget.output.value == "0 processes shown"
assert "<table" in widget.table.value
assert "<th>PK</th>" in widget.table.value
assert (
f"home/process.ipynb?id={multiply_add_completed_workchain.pk}"
not in widget.table.value
)
0