async function renderSnippet(rawStr, dependencies = {}) {
if(!dependencies.Plot) dependencies.Plot = Plot
if(!dependencies.d3) dependencies.d3 = d3
if(!dependencies.Inputs) dependencies.Inputs = Inputs
if(!dependencies.width) dependencies.width = width
dependencies = Object.assign(dependencies, { Plot, d3, width })
let str = rawStr;
let code = `return ${str}`
let arr = Object.keys(dependencies).concat([code])
const func = new AsyncFunction(...arr)
const results = await func(...Object.values(dependencies))
.then((r) => r)
.catch(e => {
const missingVariables = Object.keys(dependencies).filter(d => !dependencies[d]).join(", ")
return htl.html`<strong style="color:#ff5400;">Error:</strong> For this code to run, you need to define the variables: <code>${missingVariables}</code>`
})
console.log(results.constructor === Array && typeof results[0] === "object")
if((results.constructor === Array && typeof results[0] === "object")
|| typeof results.objects === "function"
) {
return Inputs.table(results)
} else if(results.constructor === Array && (typeof results[0] === "string" || typeof results[0] === "number")) {
return results.slice(0, 20).join(", ") + "...."
}
return results;
}