File size: 3,194 Bytes
750ddf0
 
 
 
 
 
 
76fa02e
ca00147
e5832e1
750ddf0
 
 
 
 
 
 
 
 
 
 
ca00147
 
 
 
 
72f5d21
 
 
ca00147
72f5d21
ca00147
72f5d21
ca00147
72f5d21
ca00147
 
 
 
 
 
 
 
 
 
 
 
 
 
e5832e1
3b22c2d
e5832e1
 
 
 
 
 
76fa02e
 
 
 
e5832e1
76fa02e
3b22c2d
76fa02e
 
 
 
 
 
 
 
 
 
 
 
 
750ddf0
ca00147
750ddf0
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from __future__ import annotations

import pandas as pd


class PaperList:
    def __init__(self):
        self.table = pd.read_csv('papers.csv')
        self._preprcess_table()

        self.table_header = '''
            <tr>
                <td width="50%">Paper</td>
                <td width="25%">Authors</td>
                <td width="5%">pdf</td>
                <td width="5%">Supplementary</td>
                <td width="5%">arXiv</td>
                <td width="5%">GitHub</td>
                <td width="5%">Hugging Face Spaces</td>
            </tr>'''

    def _preprcess_table(self) -> None:
        self.table['title_lowercase'] = self.table.title.str.lower()

        rows = []
        for row in self.table.itertuples():
            paper = f'<a href="{row.url}" target="_blank">{row.title}</a>'
            pdf = f'<a href="{row.pdf}" target="_blank">pdf</a>'
            supp = f'<a href="{row.supp}" target="_blank">supp</a>' if isinstance(
                row.supp, str) else ''
            arxiv = f'<a href="{row.arxiv}" target="_blank">arXiv</a>' if isinstance(
                row.arxiv, str) else ''
            github = f'<a href="{row.github}" target="_blank">GitHub</a>' if isinstance(
                row.github, str) else ''
            hf_space = f'<a href="{row.hf_space}" target="_blank">Space</a>' if isinstance(
                row.hf_space, str) else ''
            row = f'''
                <tr>
                    <td>{paper}</td>
                    <td>{row.authors}</td>
                    <td>{pdf}</td>
                    <td>{supp}</td>
                    <td>{arxiv}</td>
                    <td>{github}</td>
                    <td>{hf_space}</td>
                </tr>'''
            rows.append(row)
        self.table['html_table_content'] = rows

    def render(self, search_query: str, case_sensitive: bool,
               names_with_link: list[str]) -> tuple[int, str]:
        df = self.table
        if search_query:
            if case_sensitive:
                df = df[df.title.str.contains(search_query)]
            else:
                df = df[df.title_lowercase.str.contains(search_query.lower())]
        has_supp = 'Supp' in names_with_link
        has_arxiv = 'arXiv' in names_with_link
        has_github = 'GitHub' in names_with_link
        has_hf_space = 'HF Space' in names_with_link
        df = self.filter_table(df, has_supp, has_arxiv, has_github,
                               has_hf_space)
        return len(df), self.to_html(df)

    def filter_table(self, df: pd.DataFrame, has_supp: bool, has_arxiv: bool,
                     has_github: bool, has_hf_space: bool) -> pd.DataFrame:
        if has_supp:
            df = df[~df.supp.isna()]
        if has_arxiv:
            df = df[~df.arxiv.isna()]
        if has_github:
            df = df[~df.github.isna()]
        if has_hf_space:
            df = df[~df.hf_space.isna()]
        return df

    def to_html(self, df: pd.DataFrame) -> str:
        table_rows = df.html_table_content
        table_data = ''.join(table_rows)
        html = f'''<table>
            {self.table_header}
            {table_data}
        </table>'''
        return html