Spaces:
Sleeping
Sleeping
Update data_list.py
Browse files- data_list.py +77 -0
data_list.py
CHANGED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import requests
|
| 6 |
+
from huggingface_hub.hf_api import SpaceInfo
|
| 7 |
+
|
| 8 |
+
SHEET_ID = '1BWKw2ygYQUUPcNdSJhW9OkXILWO5i-dCa5Uahn9dHNo'
|
| 9 |
+
SHEET_NAME = 'Datasets'
|
| 10 |
+
csv_url = f'https://docs.google.com/spreadsheets/d/{SHEET_ID}/gviz/tq?tqx=out:csv&sheet={SHEET_NAME}'
|
| 11 |
+
|
| 12 |
+
class DataList:
|
| 13 |
+
def __init__(self):
|
| 14 |
+
self.table = pd.read_csv(csv_url)
|
| 15 |
+
self.table = self.table.astype({'Year':'string'})
|
| 16 |
+
self._preprocess_table()
|
| 17 |
+
|
| 18 |
+
self.table_header = '''
|
| 19 |
+
<tr>
|
| 20 |
+
<td width="15%">Name</td>
|
| 21 |
+
<td width="10%">URL</td>
|
| 22 |
+
<td width="30%">About</td>
|
| 23 |
+
<td width="15%">Publisher</td>
|
| 24 |
+
<td width="10%">Year Updated</td>
|
| 25 |
+
<td width="10%">Type</td>
|
| 26 |
+
<td width="10%">Tag</td>
|
| 27 |
+
</tr>'''
|
| 28 |
+
|
| 29 |
+
def _preprocess_table(self) -> None:
|
| 30 |
+
self.table['name_lowercase'] = self.table['Name'].str.lower()
|
| 31 |
+
|
| 32 |
+
rows = []
|
| 33 |
+
for row in self.table.itertuples():
|
| 34 |
+
source = f'<a href="{row.URL}" target="_blank">Link</a>' if isinstance(
|
| 35 |
+
row.URL, str) else ''
|
| 36 |
+
row = f'''
|
| 37 |
+
<tr>
|
| 38 |
+
<td>{row.Name}</td>
|
| 39 |
+
<td>{source}</td>
|
| 40 |
+
<td>{row.About}</td>
|
| 41 |
+
<td>{row.Publisher}</td>
|
| 42 |
+
<td>{row.Year}</td>
|
| 43 |
+
<td>{row.Type}</td>
|
| 44 |
+
<td>{row.Tags}</td>
|
| 45 |
+
</tr>'''
|
| 46 |
+
rows.append(row)
|
| 47 |
+
self.table['html_table_content'] = rows
|
| 48 |
+
|
| 49 |
+
def render(self, search_query: str,
|
| 50 |
+
case_sensitive: bool,
|
| 51 |
+
filter_names: list[str],
|
| 52 |
+
data_types: list[str]) -> tuple[int, str]:
|
| 53 |
+
df = self.table
|
| 54 |
+
if search_query:
|
| 55 |
+
if case_sensitive:
|
| 56 |
+
df = df[df.name.str.contains(search_query)]
|
| 57 |
+
else:
|
| 58 |
+
df = df[df.name_lowercase.str.contains(search_query.lower())]
|
| 59 |
+
df = self.filter_table(df, filter_names, data_types)
|
| 60 |
+
result = self.to_html(df, self.table_header)
|
| 61 |
+
return result
|
| 62 |
+
|
| 63 |
+
@staticmethod
|
| 64 |
+
def filter_table(df: pd.DataFrame, filter_names: list[str], data_types: list[str]) -> pd.DataFrame:
|
| 65 |
+
df = df.loc[df.Type.isin(set(filter_names))]
|
| 66 |
+
df = df.loc[df.Tags.isin(set(data_types))]
|
| 67 |
+
return df
|
| 68 |
+
|
| 69 |
+
@staticmethod
|
| 70 |
+
def to_html(df: pd.DataFrame, table_header: str) -> str:
|
| 71 |
+
table_data = ''.join(df.html_table_content)
|
| 72 |
+
html = f'''
|
| 73 |
+
<table>
|
| 74 |
+
{table_header}
|
| 75 |
+
{table_data}
|
| 76 |
+
</table>'''
|
| 77 |
+
return html
|