MCPcopy Create free account
hub / github.com/Datakitpage/Datakit / createDuckDBBridge

Function createDuckDBBridge

frontend/src/lib/python/init.ts:348–454  ·  view source on GitHub ↗
(duckDBStore: any)

Source from the content-addressed store, hash-verified

346 * Create DuckDB bridge for Python
347 */
348export function createDuckDBBridge(duckDBStore: any): DuckDBBridge {
349 const bridge = {
350 async queryToPandas(sql: string): Promise<any> {
351 if (!pyodideInstance) {
352 throw new Error("Pyodide not initialized");
353 }
354
355 // Execute query in DuckDB
356 const result = await duckDBStore.executePaginatedQuery(sql, 1, 10000, false, false);
357
358 if (!result || !result.data) {
359 throw new Error("Query returned no data");
360 }
361
362 // Convert to pandas DataFrame
363 const dataJson = JSON.stringify({
364 columns: result.columns,
365 data: result.data
366 });
367
368 // Create DataFrame directly in Python
369 const pyCode = `
370import pandas as pd
371import json
372
373_query_result_data = json.loads('''${dataJson}''')
374_query_result_df = pd.DataFrame(_query_result_data['data'], columns=_query_result_data['columns'])
375_query_result_df
376 `;
377
378 const df = await pyodideInstance.runPythonAsync(pyCode);
379 return df;
380 },
381
382 async pandasToTable(df: any, tableName: string): Promise<void> {
383 if (!pyodideInstance) {
384 throw new Error("Pyodide not initialized");
385 }
386
387 // Convert DataFrame to records
388 const records = await pyodideInstance.runPython(`
389 import json
390 json.dumps(${df.name}.to_dict('records'))
391 `);
392
393 const data = JSON.parse(records);
394 const columns = Object.keys(data[0] || {});
395
396 // Convert to format expected by DuckDB
397 const rows = data.map((record: any) => columns.map(col => record[col]));
398
399 // Create table in DuckDB
400 await duckDBStore.loadData(rows, columns, tableName, []);
401 },
402
403 getTableNames(): string[] {
404 return duckDBStore.getAvailableTables();
405 },

Callers 1

pythonStore.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected