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 = ''' Paper Authors pdf Supplementary arXiv GitHub Hugging Face Spaces ''' def _preprcess_table(self) -> None: self.table['title_lowercase'] = self.table.title.str.lower() rows = [] for row in self.table.itertuples(): paper = f'{row.title}' pdf = f'pdf' supp = f'supp' if isinstance( row.supp, str) else '' arxiv = f'arXiv' if isinstance( row.arxiv, str) else '' github = f'GitHub' if isinstance( row.github, str) else '' hf_space = f'Space' if isinstance( row.hf_space, str) else '' row = f''' {paper} {row.authors} {pdf} {supp} {arxiv} {github} {hf_space} ''' 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''' {self.table_header} {table_data}
''' return html