Published
Edited
May 19, 2020
Importers
Insert cell
Insert cell
chunk = (json, parts) => {
return json.reduce((acc, curr) => {
const index = acc.length - 1

if (index < 0) {
acc.push([curr])
return acc
}

if (acc[index].length === parts) {
acc.push([curr])
} else {
acc[index].push(curr)
}

return acc
}, [])
}
Insert cell
async function solve_many_chunk(json, orientServer = 'http://127.0.0.1:8000') {
const promised = await Promise.all(chunk(json, 10).map(chunk_json => {
return fetch(orientServer + '/solve-many', {
body: JSON.stringify(chunk_json),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
method: 'POST'
}).then(res => {
return res.json()
}).then(res => {
return res
.filter(d => d !== null)
.map(d => d.flat())
.flat()
.filter(d => d !== null)
.map(d => {
const results = {}
Object.keys(d)
.filter(key => !key.includes('%'))
.map(key => {
results[key] = d[key]
})

return results
})
})
}))
return promised.flat()
}
Insert cell
_dump_vars = (json) => {
const map = {}
json.forEach(d => {
map[d.name] = d
})

return map
}
Insert cell
function dump_vars(orientServer = 'http://127.0.0.1:8000') {
return fetch(orientServer + '/dump-vars')
.then(response => response.json())
.then(json => {

const map = {}
json.forEach(d => {
map[d.name] = d
})

return map
})
}
Insert cell
makeQuery = (query = []) => {
return new Query(query)
}
Insert cell
extend_query = (array, ...exts) => {
let query = array

const extend_one = (arr, ext) => arr.map(d => ext.map((_, i) => Object.assign({}, d, ext[i])))

exts.forEach(ext => {
query = extend_one(query, ext).flat()
})

return query
}
Insert cell
class Query {
constructor(query = []) {
this.query = query
}
add (query) {
this.query = extend_query(this.query, [query])
return this
}
extend(query) {
this.query = extend_query(this.query, query)
return this
}
compile() {
return this.query
}
}
Insert cell
function report_from_result(vars, result, starting_assignments) {
const html = md`

| name | val | type | desc |
| ---- | --- | ---- | ---- |
${Object.keys(result)
// .filter(d => vars && vars[d] && vars[d].description)
.sort()
.map(d => `| ${!starting_assignments[d] ? `**${d}**` : d} | ${result[d]} | ${vars[d] && vars[d].type ? vars[d].type : ''} | ${vars[d] && vars[d].description ? vars[d].description : ''} |\n`)}
`
html.value = result
return html
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more