Support filtering by search strings and columns at the same time. (#4)
Browse files- Support filtering by search strings and columns at the same time. (df10b19d6e4187b27cde2efc16e5e888fd4ba543)
app.py
CHANGED
|
@@ -146,27 +146,23 @@ def refresh(agg: str = "max"):
|
|
| 146 |
|
| 147 |
|
| 148 |
# Function to update the table based on search query
|
| 149 |
-
def
|
| 150 |
df = leaderboard_df
|
| 151 |
-
if search_query:
|
| 152 |
search_terms = search_query.split(";")
|
| 153 |
search_terms = [term.strip().lower() for term in search_terms]
|
| 154 |
pattern = "|".join(search_terms)
|
| 155 |
df = df[df["Model"].str.lower().str.contains(pattern, regex=True)]
|
| 156 |
# Drop any columns which are all NaN
|
| 157 |
df = df.dropna(how="all", axis=1)
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
# Drop rows with NaN values
|
| 167 |
-
df = df.copy().dropna(how="all", axis=0, subset=[c for c in df.columns if c in cols])
|
| 168 |
-
# Recompute average
|
| 169 |
-
df.insert(loc=1, column="Average", value=df.mean(axis=1, numeric_only=True))
|
| 170 |
return df
|
| 171 |
|
| 172 |
|
|
@@ -201,9 +197,9 @@ with demo:
|
|
| 201 |
with gr.Row():
|
| 202 |
refresh_button = gr.Button("Refresh")
|
| 203 |
|
| 204 |
-
cols_bar.change(
|
| 205 |
agg.change(refresh, inputs=[agg], outputs=[leaderboard_table])
|
| 206 |
-
search_bar.submit(
|
| 207 |
refresh_button.click(refresh, inputs=[], outputs=[leaderboard_table])
|
| 208 |
|
| 209 |
demo.launch()
|
|
|
|
| 146 |
|
| 147 |
|
| 148 |
# Function to update the table based on search query
|
| 149 |
+
def filter_and_search(cols: list[str], search_query: str):
|
| 150 |
df = leaderboard_df
|
| 151 |
+
if len(search_query) > 0:
|
| 152 |
search_terms = search_query.split(";")
|
| 153 |
search_terms = [term.strip().lower() for term in search_terms]
|
| 154 |
pattern = "|".join(search_terms)
|
| 155 |
df = df[df["Model"].str.lower().str.contains(pattern, regex=True)]
|
| 156 |
# Drop any columns which are all NaN
|
| 157 |
df = df.dropna(how="all", axis=1)
|
| 158 |
+
if len(cols) > 0:
|
| 159 |
+
index_cols = list(leaderboard_df.columns[:1])
|
| 160 |
+
new_cols = index_cols + cols
|
| 161 |
+
df = df.copy()[new_cols]
|
| 162 |
+
# Drop rows with NaN values
|
| 163 |
+
df = df.copy().dropna(how="all", axis=0, subset=[c for c in df.columns if c in cols])
|
| 164 |
+
# Recompute average
|
| 165 |
+
df.insert(loc=1, column="Average", value=df.mean(axis=1, numeric_only=True))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
return df
|
| 167 |
|
| 168 |
|
|
|
|
| 197 |
with gr.Row():
|
| 198 |
refresh_button = gr.Button("Refresh")
|
| 199 |
|
| 200 |
+
cols_bar.change(filter_and_search, inputs=[cols_bar, search_bar], outputs=[leaderboard_table])
|
| 201 |
agg.change(refresh, inputs=[agg], outputs=[leaderboard_table])
|
| 202 |
+
search_bar.submit(filter_and_search, inputs=[cols_bar, search_bar], outputs=[leaderboard_table])
|
| 203 |
refresh_button.click(refresh, inputs=[], outputs=[leaderboard_table])
|
| 204 |
|
| 205 |
demo.launch()
|