2424from dash import clientside_callback
2525from dash import dcc
2626from dash import html
27- from dash .exceptions import PreventUpdate
2827
2928
3029dash ._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)
955957def 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" }
0 commit comments