|
from typing import Any, Union |
|
|
|
import gradio as gr |
|
import pandas as pd |
|
|
|
|
|
def escape_regex(regex: str) -> str: |
|
|
|
result = regex.encode("unicode_escape").decode("utf-8") |
|
return result |
|
|
|
|
|
def unescape_regex(regex: str) -> str: |
|
|
|
result = regex.encode("utf-8").decode("unicode_escape") |
|
return result |
|
|
|
|
|
def open_accordion(): |
|
return gr.Accordion(open=True) |
|
|
|
|
|
def close_accordion(): |
|
return gr.Accordion(open=False) |
|
|
|
|
|
def open_accordion_with_stats( |
|
overview: pd.DataFrame, base_label: str, caption2column: dict[str, str], total_column: str |
|
): |
|
caption2value = { |
|
caption: len(overview) if column == total_column else overview[column].sum() |
|
for caption, column in caption2column.items() |
|
} |
|
stats_str = ", ".join([f"{value} {caption}" for caption, value in caption2value.items()]) |
|
label = f"{base_label} ({stats_str})" |
|
return gr.Accordion(open=True, label=label) |
|
|
|
|
|
def change_tab(id: Union[int, str]): |
|
return gr.Tabs(selected=id) |
|
|
|
|
|
def get_cell_for_fixed_column_from_df( |
|
evt: gr.SelectData, |
|
df: pd.DataFrame, |
|
column: str, |
|
) -> Any: |
|
"""Get the value of the fixed column for the selected row in the DataFrame. |
|
This is required can *not* with a lambda function because that will not get |
|
the evt parameter. |
|
|
|
Args: |
|
evt: The event object. |
|
df: The DataFrame. |
|
column: The name of the column. |
|
|
|
Returns: |
|
The value of the fixed column for the selected row. |
|
""" |
|
row_idx, col_idx = evt.index |
|
doc_id = df.iloc[row_idx][column] |
|
return doc_id |
|
|