import gradio as gr import pandas as pd import re SHEET_ID = '1MV7Z0fwJvnU4TFdPsBEC4tl-h3DSvHbee8qxaHcSYHs' SHEET_NAME = 'Sheet1' csv_url = f'https://docs.google.com/spreadsheets/d/{SHEET_ID}/gviz/tq?tqx=out:csv&sheet={SHEET_NAME}' class DataList: def __init__(self): self.table = pd.read_csv(csv_url) self._preprocess_table() self.table_header = ''' Name Description Module Type (Format) Hashtags ''' def _preprocess_table(self) -> None: self.table['name_lowercase'] = self.table['Name'].str.lower() rows = [] for row in self.table.itertuples(): source = f'{row.Name}' if isinstance( row.URL, str) else '{row.Name}' row = f''' {source} {row.Description} {row.Module} {row.Type} {row.Hashtags} ''' rows.append(row) self.table['html_table_content'] = rows def render(self, search_query: str, filter_names: list[str], filter_names2: list[str], filter_names3: list[str], show_filter_checkbox: bool) -> tuple[int, str]: self.table = pd.read_csv(csv_url) self._preprocess_table() self.table_header = ''' Name Description Module Type (Format) Hashtags ''' df = self.table if search_query: df = df[df.name_lowercase.str.contains(search_query.lower())] df1 = self.filter_table1(df, filter_names) df2 = self.filter_table2(df1,filter_names2) df3 = self.filter_table3(df2,filter_names3,show_filter_checkbox) result = self.to_html(df3, self.table_header) return result @staticmethod def filter_table1(df: pd.DataFrame, filter_names: list[str]) -> pd.DataFrame: df = df.loc[df.Module.isin(set(filter_names))] return df @staticmethod def filter_table2(df: pd.DataFrame,filter_names2: list[str]) -> pd.DataFrame: df.loc[:,'Type'] = df['Type'].fillna("").astype(str); df= df[df.Type.str.contains('|'.join(filter_names2),case=False, regex=True)] return df @staticmethod def filter_table3(df: pd.DataFrame,filter_names3: list[str],show_filter_checkbox: bool) -> pd.DataFrame: if not show_filter_checkbox: return df if not filter_names3: return df df.loc[:,'Hashtags'] = df['Hashtags'].fillna("").astype(str); df= df[df.Hashtags.str.contains('|'.join(filter_names3),case=False, regex=True)] return df @staticmethod def to_html(df: pd.DataFrame, table_header: str) -> str: table_data = ''.join(df.html_table_content) html = f''' {table_header} {table_data}
''' return html data_list = DataList() css=""" .app.svelte-fc1inc.svelte-fc1inc:not(.fill_width) { max-width: 100% !important; } """ def toggle_filter(show_filter: bool): return gr.update(visible=show_filter) with gr.Blocks(css=css, theme=gr.themes.Default(primary_hue="blue", secondary_hue="gray")) as demo: search_box = gr.Textbox( label='Search Name', placeholder='You can search for titles with regular expressions. e.g. (?