|
import gradio as gr |
|
import pandas as pd |
|
|
|
|
|
SHEET_ID = '1l-NbqrTOkbd5pPamav1kkQ9Xk3P40EIKOPL7mfCnRJo' |
|
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 = ''' |
|
<tr> |
|
<td width="30%">Name</td> |
|
<td width="70%">Description</td> |
|
</tr>''' |
|
|
|
def _preprocess_table(self) -> None: |
|
self.table['name_lowercase'] = self.table['Name'].str.lower() |
|
|
|
rows = [] |
|
for row in self.table.itertuples(): |
|
source = f'<a href="{row.URL}" target="_parent">{row.Name}</a>' if isinstance( |
|
row.URL, str) else '{row.Name}' |
|
row = f''' |
|
<tr> |
|
<td>{source}</td> |
|
<td>{row.Description}</td> |
|
</tr>''' |
|
rows.append(row) |
|
self.table['html_table_content'] = rows |
|
|
|
def render(self, search_query: str |
|
) -> tuple[int, str]: |
|
self.table = pd.read_csv(csv_url) |
|
self._preprocess_table() |
|
|
|
self.table_header = ''' |
|
<tr> |
|
<td width="15%">Organization Name</td> |
|
<td width="85%">Organization Overview</td> |
|
</tr>''' |
|
df = self.table |
|
if search_query: |
|
df = df[df.name_lowercase.str.contains(search_query.lower())] |
|
result = self.to_html(df, self.table_header) |
|
return result |
|
|
|
@staticmethod |
|
def filter_table(df: pd.DataFrame, filter_names: list[str]) -> pd.DataFrame: |
|
df = df.loc[df.Type.isin(set(filter_names))] |
|
return df |
|
|
|
@staticmethod |
|
def to_html(df: pd.DataFrame, table_header: str) -> str: |
|
table_data = ''.join(df.html_table_content) |
|
html = f''' |
|
<table> |
|
{table_header} |
|
{table_data} |
|
</table>''' |
|
return html |
|
|
|
|
|
data_list = DataList() |
|
|
|
css = """ |
|
button.svelte-kqij2n{font-weight: bold !important; |
|
background-color: #ebecf0; |
|
color: black; |
|
margin-left: 5px;} |
|
#tlsnlbs{} |
|
#mtcs{} |
|
|
|
#mdls{} |
|
#dts{} |
|
.svelte-kqij2n .selected { |
|
background-color: black; |
|
color: white; |
|
} |
|
.app.svelte-182fdeq.svelte-182fdeq { |
|
padding: 0 !important; |
|
} |
|
span.svelte-s1r2yt{font-weight: bold !important; |
|
} |
|
""" |
|
with gr.Blocks(css=css) as demo: |
|
search_box = gr.Textbox( label='Search Name', placeholder='You can search for titles with regular expressions. e.g. (?<!sur)face',max_lines=1) |
|
search_button = gr.Button('Search') |
|
table = gr.HTML(show_label=False) |
|
demo.load(fn=data_list.render, inputs=[search_box,],outputs=[table,]) |
|
search_box.submit(fn=data_list.render, inputs=[search_box,], outputs=[table,]) |
|
search_button.click(fn=data_list.render, inputs=[search_box,], outputs=[table,]) |
|
|
|
demo.queue() |
|
demo.launch(share=False) |
|
|