Download explorer.js from Publicrecords/eu-ted-data-explorer: direct link, hf CLI and curl.
- Browser
- Download file 5.75 kB
-
https://huggingface.co/spaces/Publicrecords/eu-ted-data-explorer/resolve/main/explorer.js
- Command line
-
hf download hf://spaces/Publicrecords/eu-ted-data-explorer/explorer.js
-
curl -L -o explorer.js https://huggingface.co/spaces/Publicrecords/eu-ted-data-explorer/resolve/main/explorer.js
5.75 kB
| (function(){ | |
| const cfg = window.EXPLORER_CONFIG; | |
| const statusEl = document.getElementById("status"); | |
| const errEl = document.getElementById("error"); | |
| const tableEl = document.getElementById("table"); | |
| const searchEl = document.getElementById("search"); | |
| const filterEl = document.getElementById("filter"); | |
| const sourceEl = document.getElementById("source"); | |
| const countEl = document.getElementById("count"); | |
| function parseCSV(text){ | |
| const rows=[]; | |
| let row=[], field="", i=0, inQuotes=false; | |
| const s=text.replace(/^\uFEFF/, ""); | |
| while(i<s.length){ | |
| const c=s[i]; | |
| if(inQuotes){ | |
| if(c==='"'){ | |
| if(s[i+1]==='"'){ field+='"'; i+=2; continue; } | |
| inQuotes=false; i++; continue; | |
| } | |
| field+=c; i++; continue; | |
| } | |
| if(c==='"'){ inQuotes=true; i++; continue; } | |
| if(c===","){ row.push(field); field=""; i++; continue; } | |
| if(c==="\n" || (c==="\r" && s[i+1]==="\n")){ | |
| row.push(field); field=""; | |
| if(row.length>1 || row[0]!=="") rows.push(row); | |
| row=[]; | |
| i += (c==="\r") ? 2 : 1; | |
| continue; | |
| } | |
| if(c==="\r"){ row.push(field); field=""; if(row.length>1||row[0]!=="") rows.push(row); row=[]; i++; continue; } | |
| field+=c; i++; | |
| } | |
| if(field!=="" || row.length){ row.push(field); rows.push(row); } | |
| if(!rows.length) return {headers:[], records:[]}; | |
| const headers=rows[0].map(h=>h.trim()); | |
| const records=rows.slice(1).map(r=>{ | |
| const o={}; | |
| headers.forEach((h,idx)=>{ o[h]=r[idx]??""; }); | |
| return o; | |
| }); | |
| return {headers, records}; | |
| } | |
| function isUrl(v){ return /^https?:\/\//i.test(v||""); } | |
| function render(headers, records){ | |
| const q=(searchEl.value||"").trim().toLowerCase(); | |
| const fv=filterEl && filterEl.value; | |
| const filtered=records.filter(rec=>{ | |
| if(fv && filterEl.dataset.col && String(rec[filterEl.dataset.col]||"")!==fv) return false; | |
| if(!q) return true; | |
| return headers.some(h=>String(rec[h]??"").toLowerCase().includes(q)); | |
| }); | |
| countEl.textContent = `${filtered.length.toLocaleString()} of ${records.length.toLocaleString()} rows`; | |
| const show=filtered.slice(0, 500); | |
| let html="<table><thead><tr>"+headers.map(h=>`<th>${escapeHtml(h)}</th>`).join("")+"</tr></thead><tbody>"; | |
| for(const rec of show){ | |
| html+="<tr>"; | |
| for(const h of headers){ | |
| const v=String(rec[h]??""); | |
| if(isUrl(v)) html+=`<td><a href="${escapeAttr(v)}" target="_blank" rel="noopener noreferrer">${escapeHtml(v)}</a></td>`; | |
| else html+=`<td>${escapeHtml(v)}</td>`; | |
| } | |
| html+="</tr>"; | |
| } | |
| html+="</tbody></table>"; | |
| if(filtered.length>500) html+=`<p class="meta">Showing the first 500 matching rows.</p>`; | |
| tableEl.innerHTML=html; | |
| } | |
| function escapeHtml(s){ | |
| return String(s).replace(/[&<>"']/g, ch=>({"&":"&","<":"<",">":">","\"":""","'":"'"}[ch])); | |
| } | |
| function escapeAttr(s){ return escapeHtml(s).replace(/`/g,"`"); } | |
| function fillFilter(headers, records){ | |
| const col=cfg.filterColumn; | |
| if(!col || !filterEl) return; | |
| if(!headers.includes(col)){ filterEl.style.display="none"; return; } | |
| filterEl.dataset.col=col; | |
| const vals=[...new Set(records.map(r=>String(r[col]??"")).filter(Boolean))].sort(); | |
| filterEl.innerHTML=`<option value="">All ${escapeHtml(col)}</option>`+vals.map(v=>`<option value="${escapeAttr(v)}">${escapeHtml(v)}</option>`).join(""); | |
| } | |
| async function loadOne(ds){ | |
| const url=`https://huggingface.co/datasets/${ds.id}/resolve/main/${ds.file}`; | |
| const res=await fetch(url, {headers:{"Accept":"text/csv,*/*"}}); | |
| if(!res.ok) throw new Error(`Could not load ${ds.id} (${res.status})`); | |
| const text=await res.text(); | |
| const parsed=parseCSV(text); | |
| const tag=ds.label || ds.id; | |
| return parsed.records.map(r=>({...r, _dataset: tag})); | |
| } | |
| async function loadAll(){ | |
| errEl.hidden=true; | |
| statusEl.textContent="Loading public Hugging Face dataset…"; | |
| tableEl.innerHTML=""; | |
| const selected = sourceEl ? sourceEl.value : "all"; | |
| const sources = (selected==="all") ? cfg.datasets : cfg.datasets.filter(d=>d.id===selected); | |
| try{ | |
| const batches=await Promise.all(sources.map(loadOne)); | |
| let records=batches.flat(); | |
| let headers=[]; | |
| const seen=new Set(); | |
| for(const rec of records){ | |
| for(const k of Object.keys(rec)){ | |
| if(!seen.has(k)){ seen.add(k); headers.push(k); } | |
| } | |
| } | |
| if(headers.includes("_dataset") && cfg.datasets.length===1){ | |
| headers=headers.filter(h=>h!=="_dataset"); | |
| records=records.map(r=>{ const o={...r}; delete o._dataset; return o; }); | |
| } | |
| window.__ROWS={headers, records}; | |
| fillFilter(headers, records); | |
| statusEl.textContent="Loaded from Hugging Face (public, no login)."; | |
| render(headers, records); | |
| }catch(e){ | |
| statusEl.textContent=""; | |
| errEl.hidden=false; | |
| errEl.textContent="Could not load the public dataset: "+e.message; | |
| } | |
| } | |
| if(sourceEl && cfg.datasets.length>1){ | |
| sourceEl.innerHTML=`<option value="all">All datasets in this family</option>`+ | |
| cfg.datasets.map(d=>`<option value="${escapeAttr(d.id)}">${escapeHtml(d.label||d.id)}</option>`).join(""); | |
| sourceEl.addEventListener("change", loadAll); | |
| }else if(sourceEl){ | |
| sourceEl.style.display="none"; | |
| const lab=document.getElementById("source-label"); | |
| if(lab) lab.style.display="none"; | |
| } | |
| searchEl.addEventListener("input", ()=>{ if(window.__ROWS) render(window.__ROWS.headers, window.__ROWS.records); }); | |
| if(filterEl) filterEl.addEventListener("change", ()=>{ if(window.__ROWS) render(window.__ROWS.headers, window.__ROWS.records); }); | |
| loadAll(); | |
| })(); | |