Skip to content

Commit 72485fa

Browse files
committed
add results table
1 parent cf13e4a commit 72485fa

File tree

2 files changed

+100
-24
lines changed

2 files changed

+100
-24
lines changed

app/callbacks.py

Lines changed: 63 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from dash import clientside_callback
2525
from dash import dcc
2626
from dash import html
27-
from dash.exceptions import PreventUpdate
2827

2928

3029
dash._dash_renderer._set_react_version("18.2.0") # type: ignore
@@ -940,12 +939,15 @@ def gm_scoring_apply(
940939
return df
941940

942941

943-
# TODO: add the logic for outputing data in the results table, issue #33
942+
# TODO: Add tests
944943
@app.callback(
945944
Output("gm-results-alert", "children"),
946945
Output("gm-results-alert", "is_open"),
946+
Output("gm-results-table", "data"),
947+
Output("gm-results-table", "columns"),
948+
Output("gm-results-table-card-body", "style"),
947949
Input("gm-results-button", "n_clicks"),
948-
Input("gm-table-select-all-checkbox", "value"),
950+
Input("gm-table", "derived_virtual_data"),
949951
Input("gm-table", "derived_virtual_selected_rows"),
950952
State("processed-links-store", "data"),
951953
State({"type": "gm-scoring-dropdown-menu", "index": ALL}, "value"),
@@ -954,47 +956,84 @@ def gm_scoring_apply(
954956
)
955957
def gm_update_results_datatable(
956958
n_clicks: int | None,
957-
select_all: list | None,
959+
virtual_data: list[dict] | None,
958960
selected_rows: list[int] | None,
959961
processed_links: str,
960962
dropdown_menus: list[str],
961963
radiobuttons: list[str],
962964
cutoffs_met: list[str],
963-
) -> tuple[str, bool]:
965+
) -> tuple[str, bool, list[dict], list[dict], dict]:
964966
"""Update the results DataTable based on scoring filters.
965967
966968
Args:
967-
n_clicks: Number of times the "Show Spectra" button has been clicked.
968-
select_all: Value of the select-all checkbox.
969+
n_clicks: Number of times the "Show Results" button has been clicked.
970+
virtual_data: Current filtered data from the GCF table.
969971
selected_rows: Indices of selected rows in the GCF table.
970972
processed_links: JSON string of processed links data.
971973
dropdown_menus: List of selected dropdown menu options.
972974
radiobuttons: List of selected radio button options.
973975
cutoffs_met: List of cutoff values for METCALF method.
974976
975977
Returns:
976-
tuple: Alert message and visibility state
978+
Tuple containing alert message, visibility state, table data and settings.
977979
"""
978980
triggered_id = ctx.triggered_id
979981

980982
if triggered_id in ["gm-table-select-all-checkbox", "gm-table"]:
981-
return "", False
983+
return "", False, [], [], {"display": "none"}
982984

983985
if n_clicks is None:
984-
raise PreventUpdate
986+
return "", False, [], [], {"display": "none"}
987+
988+
if not selected_rows:
989+
return (
990+
"No GCFs selected. Please select GCFs and try again.",
991+
True,
992+
[],
993+
[],
994+
{"display": "none"},
995+
)
996+
997+
if not virtual_data:
998+
return "No data available.", True, [], [], {"display": "none"}
985999

9861000
try:
987-
data = json.loads(processed_links)
988-
if len(data) == 0:
989-
return (
990-
"No processed links available. Provide input data containing links and try again.",
991-
True,
992-
)
993-
if selected_rows is None or len(selected_rows) == 0:
994-
return "No GCFs selected. Please select GCFs and try again.", True
995-
df = pd.DataFrame(data)
996-
except (json.JSONDecodeError, KeyError, pd.errors.EmptyDataError):
997-
return "", False
998-
df_results = gm_scoring_apply(df, dropdown_menus, radiobuttons, cutoffs_met)
999-
print(df_results.head())
1000-
return "", False
1001+
links_data = json.loads(processed_links)
1002+
if len(links_data) == 0:
1003+
return "No processed links available.", True, [], [], {"display": "none"}
1004+
1005+
# Get selected GCF IDs
1006+
selected_gcfs = [virtual_data[i]["GCF ID"] for i in selected_rows]
1007+
1008+
# Convert links data to DataFrame
1009+
links_df = pd.DataFrame(links_data)
1010+
1011+
# Apply scoring filters
1012+
filtered_df = gm_scoring_apply(links_df, dropdown_menus, radiobuttons, cutoffs_met)
1013+
1014+
# Filter for selected GCFs and aggregate results
1015+
results = []
1016+
for gcf_id in selected_gcfs:
1017+
gcf_links = filtered_df[filtered_df["gcf_id"] == gcf_id]
1018+
if not gcf_links.empty:
1019+
results.append(
1020+
{
1021+
"GCF ID": gcf_id,
1022+
"# Links": len(gcf_links),
1023+
"Average Score": round(gcf_links["score"].mean(), 2),
1024+
}
1025+
)
1026+
1027+
if not results:
1028+
return "No matching links found for selected GCFs.", True, [], [], {"display": "none"}
1029+
1030+
columns = [
1031+
{"name": "GCF ID", "id": "GCF ID"},
1032+
{"name": "# Links", "id": "# Links", "type": "numeric"},
1033+
{"name": "Average Score", "id": "Average Score", "type": "numeric"},
1034+
]
1035+
1036+
return "", False, results, columns, {"display": "block"}
1037+
1038+
except Exception as e:
1039+
return f"Error processing results: {str(e)}", True, [], [], {"display": "none"}

app/layouts.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,42 @@
257257
),
258258
]
259259
)
260+
261+
gm_results_table = dbc.Card(
262+
[
263+
dbc.CardHeader(
264+
["Candidate Links"],
265+
id="gm-results-table-card-header",
266+
style={"color": "#888888"},
267+
),
268+
dbc.CardBody(
269+
[
270+
dash_table.DataTable(
271+
id="gm-results-table",
272+
columns=[],
273+
data=[],
274+
editable=False,
275+
filter_action="none",
276+
sort_action="none",
277+
sort_mode="multi",
278+
page_action="native",
279+
page_current=0,
280+
page_size=10,
281+
style_cell={"textAlign": "left", "padding": "5px"},
282+
style_header={
283+
"backgroundColor": "#FF6E42",
284+
"fontWeight": "bold",
285+
"color": "white",
286+
},
287+
style_data={"border": "1px solid #ddd"},
288+
),
289+
],
290+
id="gm-results-table-card-body",
291+
style={"display": "none"},
292+
),
293+
]
294+
)
295+
260296
# gm tab content
261297
gm_content = dbc.Row(
262298
[
@@ -265,6 +301,7 @@
265301
dbc.Col(gm_table, width=10, className="mx-auto"),
266302
dbc.Col(gm_scoring_accordion, width=10, className="mx-auto dbc"),
267303
dbc.Col(gm_results, width=10, className="mx-auto"),
304+
dbc.Col(gm_results_table, width=10, className="mt-3 mx-auto"),
268305
]
269306
)
270307
# mg tab content

0 commit comments

Comments
 (0)