Published
Edited
Dec 10, 2021
2 stars
Insert cell
Insert cell
Insert cell
iowa_generation.view()
Insert cell
oil_price.view()
Insert cell
Insert cell
iowa_generation
.join(oil_price, "year")
.filter((d) => op.utcyear(d.year) >= 2002)
.filter((d) => op.utcyear(d.year) <= 2005)
.view()
Insert cell
Insert cell
Insert cell
query_join_filter = aq
.query("iowa_generation")
.join("oil_price", "year")
.filter((d, $) => op.utcyear(d.year) >= $.year_start)
.filter((d, $) => op.utcyear(d.year) <= $.year_end)
Insert cell
Insert cell
query_join_filter
.params({ year_start: 2002, year_end: 2005 })
.evaluate(
null,
lookup({ iowa_generation: iowa_generation, oil_price: oil_price })
)
.view()
Insert cell
Insert cell
JSON.stringify(query_join_filter.toObject(), null, 2) // pretty-print: set third argument of JSON.stringify() to 2
Insert cell
Insert cell
body = ({
query: query_join_filter.toObject(),
data: {
catalog: _.mapValues(
{ iowa_generation: iowa_generation, oil_price: oil_price },
tableToObject
)
},
params: { year_start: 2001, year_end: 2004 }
})
Insert cell
Insert cell
Insert cell
fn_result = () =>
fetch(arquero_url, {
body: JSON.stringify(body),
headers: {
"content-type": "application/json",
"x-functions-key": Secret("arquero-function")
},
method: "POST"
}).then((response) => {
return response.json();
})
Insert cell
Insert cell
Insert cell
Insert cell
result ? aq.fromJSON(result).view() : "need to call web-service"
Insert cell
Insert cell
result ? chart(result).render() : "need to call web-service"
Insert cell
Insert cell
Insert cell
import {aq, op} from '@uwdata/arquero'
Insert cell
import { vl } from "@vega/vega-lite-api-v5"
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
tableToObject = function (x) {
return JSON.parse(x.toJSON({ schema: false }));
}
Insert cell
Insert cell
Insert cell
a = aq.table()
Insert cell
tableToObject(a)
Insert cell
Insert cell
arquero_url = "https://arquero-function.azurewebsites.net/api/arquero"
Insert cell
clientArqservice = makeClientArqservice(
"https://arquero-function.azurewebsites.net/api/arquero",
Secret("arquero-function")
)
Insert cell
viewof resultArqservice = Inputs.button("Call web-service", {
value: aq.table(),
reduce: async () => {
// create request-body
const body = bodyArqservice(
query_join_filter,
{ catalog: { iowa_generation: iowa_generation, oil_price: oil_price } },
{ year_start: 2001, year_end: 2004 }
);

// call web service
const resp = await clientArqservice(body);

// parse response
return parseArqservice(resp);
}
})
Insert cell
Inputs.table(resultArqservice)
Insert cell
/* (String, String, Object) -> Promise Object
*
* Given a URL, an API key, and the body for a request,
* return a promise to an object representing the response.
*
* This core function; we build other functions around it,
* for example, to wrap the URL and API key.
*/
fetchArqservice = function (url, secret, body) {
return fetch(url, {
body: JSON.stringify(body),
headers: {
"content-type": "application/json",
"x-functions-key": Secret("arquero-function")
},
method: "POST"
}).then((response) => {
return response;
});
}
Insert cell
/* (String, String) -> (Object -> Promise Object)
*
* Given a base URL and an API key, return a function
* that, given an object (request body), returns a
* promise to an object that represents an arquero table.
*
* You can set the URL and API Key *once* within a notebook,
* then use (many times) the client function returned here.
*/
makeClientArqservice = function (url, secret) {
return async function (body) {
return await fetchArqservice(url, secret, body);
};
}
Insert cell
/* (Query, Object, Object) -> Object
*
* Given an arqero query, a data object, and an optional set
* of parameters, return an object to use as the body of a
* request.
*
* The `data` object must contain one of `table`, or `catalog`:
* - `table`: single arquero table.
* - `catalog`: named object containing arquero tables.
*
* Depending on the `query`, `data` would contain either an
* object named `table` or an object named `catalog`.
*/
bodyArqservice = function (query, data, params) {
const body = {
query: query.toObject(),
data: {
table: data.table ? tableToObject(data.table) : null,
catalog: data.catalog ? _.mapValues(data.catalog, tableToObject) : null
},
params: params
};

return body;
}
Insert cell
/* Response -> Promise Table
*
* Given a response from the arquero service, return a promise
* to an arquero table.
*
*/
parseArqservice = async function (response) {
return aq.table(await response.json());
}
Insert cell
This is meant to be imported by other notebooks:
Insert cell
arqService = ({
makeClient: makeClientArqservice,
body: bodyArqservice,
parse: parseArqservice
})
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