File size: 1,625 Bytes
3133b5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ced4316
 
 
 
 
 
 
 
 
 
 
 
3133b5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from typing import Any, Union

import gradio as gr
import pandas as pd


def escape_regex(regex: str) -> str:
    # "double escape" the backslashes
    result = regex.encode("unicode_escape").decode("utf-8")
    return result


def unescape_regex(regex: str) -> str:
    # reverse of escape_regex
    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