hyparquet/demo/demo.js

114 lines
3.4 KiB
JavaScript
Raw Normal View History

2024-06-05 23:22:47 +00:00
import HighTable from 'hightable'
import { compressors } from 'hyparquet-compressors'
import React from 'react'
import ReactDOM from 'react-dom'
import {
parquetMetadata, parquetMetadataAsync, parquetRead, parquetSchema, toJson,
} from '../src/hyparquet.js'
2024-07-26 21:08:57 +00:00
import { asyncBufferFromUrl } from '../src/utils.js'
2024-09-13 03:30:21 +00:00
import { initDropzone } from './dropzone.js'
import { fileLayout, fileMetadata } from './layout.js'
/**
* @typedef {import('../src/types.js').AsyncBuffer} AsyncBuffer
* @typedef {import('../src/types.js').FileMetaData} FileMetaData
*/
const content = document.querySelectorAll('#content')[0]
2024-01-28 02:06:27 +00:00
2024-09-13 03:30:21 +00:00
// Initialize drag-and-drop
initDropzone(handleFileDrop, handleUrlDrop)
2024-01-28 02:50:14 +00:00
/**
* @param {string} url
*/
2024-09-13 03:30:21 +00:00
async function handleUrlDrop(url) {
2024-05-05 17:35:23 +00:00
content.innerHTML = ''
2024-02-05 07:28:59 +00:00
try {
2024-07-26 21:08:57 +00:00
const asyncBuffer = await asyncBufferFromUrl(url)
2024-02-05 07:28:59 +00:00
const metadata = await parquetMetadataAsync(asyncBuffer)
2024-05-05 17:35:23 +00:00
await render(asyncBuffer, metadata, `<a href="${url}">${url}</a>`)
2024-02-05 07:28:59 +00:00
} catch (e) {
2024-07-26 21:08:57 +00:00
console.error('Error fetching url', e)
content.innerHTML += `<div class="error">Error fetching url ${url}\n${e}</div>`
2024-02-05 07:28:59 +00:00
}
2024-02-04 21:34:49 +00:00
}
/**
* @param {File} file
*/
2024-09-13 03:30:21 +00:00
function handleFileDrop(file) {
2024-05-05 17:35:23 +00:00
content.innerHTML = ''
2024-01-28 02:50:14 +00:00
const reader = new FileReader()
2024-05-05 17:35:23 +00:00
reader.onload = async e => {
2024-01-28 02:50:14 +00:00
try {
const arrayBuffer = e.target?.result
if (!(arrayBuffer instanceof ArrayBuffer)) throw new Error('Missing arrayBuffer')
2024-02-05 07:28:59 +00:00
const metadata = parquetMetadata(arrayBuffer)
2024-05-05 17:35:23 +00:00
await render(arrayBuffer, metadata, file.name)
2024-01-28 02:50:14 +00:00
} catch (e) {
2024-02-05 05:21:01 +00:00
console.error('Error parsing file', e)
2024-05-05 17:35:23 +00:00
content.innerHTML = `<strong>${file.name}</strong>`
content.innerHTML += `<div class="error">Error parsing file\n${e}</div>`
2024-01-28 02:06:27 +00:00
}
2024-01-28 02:50:14 +00:00
}
reader.onerror = e => {
console.error('Error reading file', e)
2024-05-05 17:35:23 +00:00
content.innerHTML = `<strong>${file.name}</strong>`
content.innerHTML += `<div class="error">Error reading file\n${e.target?.error}</div>`
2024-01-28 02:50:14 +00:00
}
reader.readAsArrayBuffer(file)
}
/**
2024-06-05 23:22:47 +00:00
* @param {AsyncBuffer} file
* @param {FileMetaData} metadata
* @param {string} name
*/
2024-06-05 23:22:47 +00:00
function render(file, metadata, name) {
renderSidebar(file, metadata, name)
2024-05-05 17:35:23 +00:00
const { children } = parquetSchema(metadata)
2024-06-05 23:22:47 +00:00
const dataframe = {
header: children.map(child => child.element.name),
numRows: Number(metadata.num_rows),
/**
* @param {number} rowStart
* @param {number} rowEnd
* @returns {Promise<any[][]>}
*/
rows(rowStart, rowEnd) {
console.log(`reading rows ${rowStart}-${rowEnd}`)
return new Promise((resolve, reject) => {
parquetRead({ file, compressors, rowStart, rowEnd, onComplete: resolve })
.catch(reject)
})
2024-05-05 17:35:23 +00:00
},
2024-06-05 23:22:47 +00:00
}
renderTable(dataframe)
2024-05-05 17:35:23 +00:00
}
/**
* @param {AsyncBuffer} asyncBuffer
* @param {FileMetaData} metadata
* @param {string} name
*/
2024-02-05 07:28:59 +00:00
function renderSidebar(asyncBuffer, metadata, name) {
const sidebar = /** @type {HTMLElement} */ (document.getElementById('sidebar'))
sidebar.innerHTML = `<div id="filename">${name}</div>`
sidebar.appendChild(fileMetadata(toJson(metadata)))
sidebar.appendChild(fileLayout(metadata, asyncBuffer))
2024-02-04 21:34:49 +00:00
}
/**
2024-06-05 23:22:47 +00:00
* @param {import('hightable').DataFrame} data
*/
2024-06-05 23:22:47 +00:00
function renderTable(data) {
// Load HighTable.tsx and render
const container = document.getElementById('content')
// @ts-expect-error ReactDOM type issue
const root = ReactDOM.createRoot(container)
root.render(React.createElement(HighTable, { data }))
2024-05-05 21:24:21 +00:00
}