From 925f7b9bffda170dee71d4c474e8b1d7791e9bc9 Mon Sep 17 00:00:00 2001 From: S Anand Date: Mon, 25 Nov 2024 14:16:10 +0800 Subject: [PATCH] ENH: Extract all rows, show 100. Add CSV download Squashed commit of the following: commit cc0a24c21d0ed142c47f16a8b1e6f7200b29281f Author: S Anand Date: Mon Nov 25 14:14:41 2024 +0800 Final touches commit 49c92bfc896e05d34288399686d13e241db99d54 Author: krishna-gramener Date: Tue Nov 12 11:44:36 2024 +0530 results will display top 100 results commit 3952bb416c889a01db8651d669fa79ce7bf546e6 Author: krishna-gramener Date: Tue Nov 12 11:25:25 2024 +0530 download button added commit 56085575dad7dac7329e9b6478b7518fb4f9b1ec Author: krishna-gramener Date: Tue Nov 12 11:03:23 2024 +0530 download button added --- script.js | 56 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/script.js b/script.js index 82b3ca8..b06450f 100644 --- a/script.js +++ b/script.js @@ -23,6 +23,7 @@ const loading = html`
Loading...
`; +let latestQueryResult = []; // -------------------------------------------------------------------- // Set up Markdown const marked = new Marked( @@ -32,7 +33,7 @@ const marked = new Marked( const language = hljs.getLanguage(lang) ? lang : "plaintext"; return hljs.highlight(code, { language }).value; }, - }), + }) ); marked.use({ @@ -63,7 +64,7 @@ render( ` : html`Sign in to upload files`, - $upload, + $upload ); // -------------------------------------------------------------------- @@ -82,10 +83,10 @@ fetch("config.json")

${body}

- `, + ` ), - $demos, - ), + $demos + ) ); $demos.addEventListener("click", async (e) => { @@ -200,7 +201,7 @@ const DB = { else if (typeof sampleValue === "boolean") sqlType = "INTEGER"; // SQLite has no boolean else if (sampleValue instanceof Date) sqlType = "TEXT"; // Store dates as TEXT return [col, sqlType]; - }), + }) ); const createTableSQL = `CREATE TABLE IF NOT EXISTS ${tableName} (${cols.map((col) => `[${col}] ${typeMap[col]}`).join(", ")})`; db.exec(createTableSQL); @@ -215,7 +216,7 @@ const DB = { cols.map((col) => { const value = row[col]; return value instanceof Date ? value.toISOString() : value; - }), + }) ) .stepReset(); } @@ -279,7 +280,7 @@ async function drawTables() { ${column.dflt_value ?? "NULL"} ${column.pk ? "Yes" : "No"} - `, + ` )} @@ -287,7 +288,7 @@ async function drawTables() { - `, + ` )} `; @@ -321,7 +322,7 @@ async function drawTables() { `, query, ], - $tablesContainer, + $tablesContainer ); $query.focus(); }); @@ -357,8 +358,7 @@ ${DB.schema() 3. Write SQL to answer the question. Use SQLite sytax. Replace generic filter values (e.g. "a location", "specific region", etc.) by querying a random value from data. -Wrap columns with spaces inside []. -Limit answers to 100 rows unless asked not to.`, +Wrap columns with spaces inside [].`, user: query, }); render(html`${unsafeHTML(marked.parse(result))}`, $sql); @@ -369,8 +369,15 @@ Limit answers to 100 rows unless asked not to.`, // Render the data using the utility function if (data.length > 0) { - const tableHtml = renderTable(data); - render(tableHtml, $result); + latestQueryResult = data; + const downloadButton = html` + + `; + const tableHtml = renderTable(data.slice(0, 100)); + render([downloadButton, tableHtml], $result); } else { render(html`

No results found.

`, $result); } @@ -427,9 +434,28 @@ function renderTable(data) { ${columns.map((col) => html`${row[col]}`)} - `, + ` )} `; } + +$result.addEventListener("click", (e) => { + const $downloadButton = e.target.closest("#download-button"); + if ($downloadButton && latestQueryResult.length > 0) { + download(dsvFormat(",").format(latestQueryResult), "datachat.csv", "text/csv"); + } +}); + +// -------------------------------------------------------------------- +// Function to download CSV file +function download(content, filename, type) { + const blob = new Blob([content], { type }); + const url = URL.createObjectURL(blob); + const link = document.createElement("a"); + link.href = url; + link.download = filename; + link.click(); + URL.revokeObjectURL(url); +}