66
77import pandas as pd
88import streamlit as st
9- from sqlalchemy import and_ , asc , func , or_ , tuple_
9+ from sqlalchemy import and_ , asc , desc , func , or_ , tuple_
1010from streamlit .delta_generator import DeltaGenerator
1111from streamlit_extras .no_default_selectbox import selectbox
1212
@@ -74,7 +74,7 @@ def render(
7474 ],
7575 )
7676
77- table_filter_column , column_filter_column , test_filter_column , table_actions_column = st .columns ([.3 , .3 , .3 , .4 ], vertical_alignment = "bottom" )
77+ table_filter_column , column_filter_column , test_filter_column , sort_column , table_actions_column = st .columns ([.2 , .2 , .2 , .1 , .25 ], vertical_alignment = "bottom" )
7878 testgen .flex_row_end (table_actions_column )
7979
8080 actions_column , disposition_column = st .columns ([.5 , .5 ])
@@ -123,6 +123,15 @@ def render(
123123 label = "Test Type" ,
124124 )
125125
126+ with sort_column :
127+ sortable_columns = (
128+ ("Table" , "table_name" ),
129+ ("Column" , "column_name" ),
130+ ("Test Type" , "test_type" ),
131+ )
132+ default = [(sortable_columns [i ][1 ], "ASC" ) for i in (0 , 1 , 2 )]
133+ sorting_columns = testgen .sorting_selector (sortable_columns , default )
134+
126135 if user_can_disposition :
127136 with disposition_column :
128137 multi_select = st .toggle ("Multi-Select" , help = "Toggle on to perform actions on multiple test definitions" )
@@ -142,7 +151,7 @@ def render(
142151
143152 with st .container ():
144153 with st .spinner ("Loading data ..." ):
145- df = get_test_definitions (test_suite , table_name , column_name , test_type )
154+ df = get_test_definitions (test_suite , table_name , column_name , test_type , sorting_columns )
146155
147156 selected , selected_test_def = render_grid (df , multi_select , filters_changed )
148157
@@ -1147,6 +1156,7 @@ def get_test_definitions(
11471156 table_name : str | None = None ,
11481157 column_name : str | None = None ,
11491158 test_type : str | None = None ,
1159+ sorting_columns : list [str ] | None = None ,
11501160) -> pd .DataFrame :
11511161 clauses = [TestDefinition .test_suite_id == test_suite .id ]
11521162 if table_name :
@@ -1155,7 +1165,15 @@ def get_test_definitions(
11551165 clauses .append (TestDefinition .column_name .ilike (column_name ))
11561166 if test_type :
11571167 clauses .append (TestDefinition .test_type == test_type )
1158- test_definitions = TestDefinition .select_where (* clauses )
1168+
1169+ sort_funcs = {"ASC" : asc , "DESC" : desc }
1170+ test_definitions = TestDefinition .select_where (
1171+ * clauses ,
1172+ order_by = tuple ([
1173+ sort_funcs [direction ](func .lower (getattr (TestDefinition , attribute )))
1174+ for (attribute , direction ) in sorting_columns
1175+ ]) if sorting_columns else None ,
1176+ )
11591177
11601178 df = to_dataframe (test_definitions , TestDefinitionSummary .columns ())
11611179 date_service .accommodate_dataframe_to_timezone (df , st .session_state )
0 commit comments