Public
Edited
Apr 10, 2024
2 forks
1 star
Also listed in…
Media directory
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { get_labels } from "@pac02/what-kind-of-articles-have-you-created"
Insert cell
import { aq, op } from "@uwdata/arquero"
Insert cell
categories = get_labels(
[
"Q11032",
"Q2001305",
"Q1616075",
"Q14350",
"Q24634210",
"Q17232649",
"Q1002697",
"Q30849",
"Q192283"
],
lang
).then((d) => d.objects())
Insert cell
categories_map = new Map(
categories.map(({ label, qid }) => [label + " (" + qid + ")", qid])
)
Insert cell
features_map = new Map(
features.map(({ label, qid }) => [label + " (" + qid + ")", qid])
)
Insert cell
features = get_labels(
[
"P1476",
"P31",
"P571",
"P159",
"P407",
"P2936",
"P856",
"P1387",
"P1142",
"P1687",
"P236",
"P2013",
"P2003",
"P4264",
"P4033",
"P2002",
"P2397",
"P7085",
"P9852"
],
lang
).then((d) => d.objects())
Insert cell
query = `SELECT ?item ?itemLabel ?sitelinks ?value ?valueLabel WHERE {
?item (wdt:P31/(wdt:P279*)) wd:${category} ;
wdt:${pcountry} wd:${country} ;
rdfs:label ?itemLabel ;
wikibase:sitelinks ?sitelinks.
MINUS { ?item wdt:P576 _:b1. }
OPTIONAL {?item wdt:${feature} ?value.
OPTIONAL {
?value rdfs:label ?valueLabel.
FILTER(LANG(?valueLabel) = "${lang}")
}
}
FILTER(LANG(?itemLabel) = "${lang}")
}
ORDER BY DESC (?sitelinks)
LIMIT 10000`
Insert cell
url = `https://query.wikidata.org/sparql?query=${encodeURIComponent(query)}`
Insert cell
data = fetch(url, {
headers: { accept: "application/sparql-results+json" }
}).then((d) => d.json())
Insert cell
array = data.results.bindings.map((d) => ({
item: d.item.value,
itemLabel: d.itemLabel.value,
sitelinks: d.sitelinks.value,
value: d.value?.value,
valueLabel: d.valueLabel?.value
}))
Insert cell
df = aq
.from(array)
.dedupe()
.derive({ item2: (d) => `<a href="${d.item}"> ${d.itemLabel}` })
Insert cell
df.view()
Insert cell
codes = FileAttachment("query.json").json()
Insert cell
// https://query.wikidata.org/#SELECT%20%3Fcodeiso2%20%3Fcodeiso3%20%3Fitem%20%3FitemLabel%20%20WHERE%20%7B%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%22.%20%7D%0A%20%20%3Fitem%20p%3AP463%20%3Fstatement.%0A%20%20%3Fstatement%20ps%3AP463%20wd%3AQ1065.%0A%20%20MINUS%20%7B%20%3Fstatement%20pq%3AP582%20%3Fend.%20%7D%0A%20%20%3Fitem%20wdt%3AP297%20%3Fcodeiso2%3B%20wdt%3AP298%20%3Fcodeiso3.%20%0A%7D%0AORDER%20BY%20%28%3Fstart%29
Insert cell
codes2 = codes.map((d) => ({
code: d.codeiso2,
country: d.itemLabel,
qid: d.item.replace("http://www.wikidata.org/entity/", "")
}))
Insert cell
codes_map = new Map(
codes2.map(({ code, country, qid }) => [country + " (" + qid + ")", qid])
)
Insert cell
viewof country2 = Inputs.text({ datalist: codes_map, value: "Q11032" })
Insert cell
get_default = () => {
const params = new URL(document.URL).searchParams;
const defaults = {
country: "Q1041",
lang: "en",
feature: "P856",
category: "Q11032"
};
if (params.has("country")) {
defaults.country = params.get("country");
}
if (params.has("lang")) {
defaults.lang = params.get("lang");
}
if (params.has("feature")) {
defaults.feature = params.get("feature");
}
if (params.has("category")) {
defaults.category = params.get("category");
}

return defaults;
}
Insert cell
import { textcolor } from "@observablehq/text-color-annotations-in-markdown"
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more