Inputs.file & DuckDB


I wanted in a notebook to show how duckdb works with observablehq. Standard approach based on observable documentation works fine:

db = DuckDBClient.of({
  match : FileAttachment("example.csv")

after attaching the file. However, I wanted allow users to attach a file from disk based on Inputs.file. My code is

viewof csvfile = Inputs.file({label: "CSV file", accept: ".csv", required: true})
db_input = DuckDBClient.of({
  match : FileAttachment(csvfile["name"])

this gives me the following error:

Is there a workaround ?

Reproducible example here

Maybe try this:

db_input = DuckDBClient.of({match: await csvfile.csv()})

1 Like

Thanks for the reply ! Worked like a charm !

1 Like

It seems to me that this first convert the csv stream to a JS array, which is further loaded by DuckDB. So the specific DuckDB velocity when dealing with (parsing) CSV files is probably shunted?
Likewise, a semicolon delimiter won’t be automatically recognized, although it is with FileAttachment().

I guess that the DuckDBClient needs to now the file’s type (.csv), which it can guess from the filename extension in “FileAttachement”, but not from the Input.file stream

I’d eventually suggest (upon scanning DuckDbClient.js code):
db_input = DuckDBClient.of({
match : {
file : Object.assign(csvfile, {mimeType:‘text/csv’}),
//delim: ‘;’ // to specify if <> ‘,’

1 Like