Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pandas dependency #674

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
57 changes: 30 additions & 27 deletions aiidalab_widgets_base/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,14 +602,6 @@ def __init__(self, path_to_root="../", **kwargs):

def update(self, _=None):
"""Perform the query."""
import pandas as pd

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; }
Expand All @@ -620,6 +612,7 @@ def update(self, _=None):
.df th { text-align: center; border: none; border-bottom: 1px solid black;}
</style>
"""

builder = CalculationQueryBuilder()
filters = builder.get_filters(
all_entries=False,
Expand All @@ -630,16 +623,9 @@ def update(self, _=None):
)
relationships = {}
if self.incoming_node:
relationships = {
**relationships,
**{"with_outgoing": orm.load_node(self.incoming_node)},
}

relationships["with_outgoing"] = orm.load_node(self.incoming_node)
if self.outgoing_node:
relationships = {
**relationships,
**{"with_incoming": orm.load_node(self.outgoing_node)},
}
relationships["with_incoming"] = orm.load_node(self.outgoing_node)

query_set = builder.get_query_set(
filters=filters,
Expand All @@ -658,19 +644,36 @@ def update(self, _=None):
"description",
],
)
dataf = pd.DataFrame(projected[1:], columns=projected[0])

# Keep only process that contain the requested string in the description.
if self.description_contains:
dataf = dataf[dataf.Description.str.contains(self.description_contains)]
if not projected:
self.output.value = "0 processes shown"
return

self.output.value = f"{len(dataf)} processes shown"
headers = projected[0]
rows = projected[1:]

# Add HTML links.
dataf["PK"] = dataf["PK"].apply(
lambda x: f"""<a href={self.path_to_root}aiidalab-widgets-base/notebooks/process.ipynb?id={x} target="_blank">{x}</a>"""
)
self.table.value += dataf.to_html(classes="df", escape=False, index=False)
# Filter rows by description if necessary
if self.description_contains:
rows = [row for row in rows if self.description_contains in row[5]]

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

# Generate HTML table
table_html = "<table class='df'><thead><tr>"
table_html += "".join(f"<th>{header}</th>" for header in headers)
table_html += "</tr></thead><tbody>"

for row in rows:
row_html = "<tr>"
for i, cell in enumerate(row):
if i == 0: # PK column
cell_row = f"<a href={self.path_to_root}aiidalab-widgets-base/notebooks/process.ipynb?id={cell} target='_blank'>{cell}</a>"
row_html += f"<td>{cell_row}</td>"
row_html += "</tr>"
table_html += row_html

table_html += "</tbody></table>"
self.table.value += table_html

@tl.validate("incoming_node")
def _validate_incoming_node(self, provided):
Expand Down
44 changes: 22 additions & 22 deletions aiidalab_widgets_base/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,40 +92,40 @@ class DictViewer(ipw.VBox):
:type downloadable: bool"""

def __init__(self, parameter, downloadable=True, **kwargs):
import pandas as pd

# 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.widget = ipw.HTML()
ipw.dlink((self, "value"), (self.widget, "value"))

self.value += """
self.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 tr:nth-child(odd):hover { background-color: #f5b7b1; }
.df tbody tr:nth-child(even):hover { background-color: #f5b7b1; }
.df tbody td { min-width: 300px; text-align: center; border: none }
.df th { text-align: center; border: none; border-bottom: 1px solid black;}
.df th { text-align: center; border: none; border-bottom: 1px solid black; }
</style>
"""

pd.set_option("max_colwidth", 40)
dataf = pd.DataFrame(
sorted(parameter.get_dict().items()),
columns=["Key", "Value"],
)
self.value += dataf.to_html(
classes="df", index=False
) # specify that exported table belongs to 'df' class
# this is used to setup table's appearance using CSS
# Extract and sort the parameter dictionary
data = sorted(parameter.get_dict().items())

# Generate HTML table
table_html = "<table class='df'>"
table_html += "<thead><tr><th>Key</th><th>Value</th></tr></thead><tbody>"
for key, value in data:
table_html += f"<tr><td>{key}</td><td>{value}</td></tr>"
table_html += "</tbody></table>"

self.value += table_html

# Generate CSV download link if requested
if downloadable:
payload = base64.b64encode(dataf.to_csv(index=False).encode()).decode()
csv_content = "Key,Value\n" + "\n".join(
f"{key},{value}" for key, value in data
)
payload = base64.b64encode(csv_content.encode()).decode()
fname = f"{parameter.pk}.csv"
self.value += f"""Download table in csv format: <a download="{fname}"
self.value += f"""Download table in CSV format: <a download="{fname}"
href="data:text/csv;base64,{payload}" target="_blank">{fname}</a>"""

super().__init__([self.widget], **kwargs)
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ install_requires =
nglview~=3.0
spglib>=1.14,<3
vapory~=0.1.2
pandas~=2.1
python_requires = >=3.9
include_package_data = True
zip_safe = False
Expand Down
Loading