Public
Edited
Feb 13, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof qid = Inputs.text({ value: "Q12794967", label: "Enter a qid" })
Insert cell
Insert cell
df_top = get_top(qid, 20, lang)
Insert cell
df_country = get_countryofcitizenship(qid, 20, lang)
Insert cell
df_occupation = get_occupation(qid, 20, lang)
Insert cell
df_places = get_placesofbirth(qid, lang)
Insert cell
geoplaces = geo.coords2geo(df_places, { lat: "lat", lon: "lon" })
Insert cell
Inputs.table(df_places)
Insert cell
get_top = (qid, n = 20, lang) => {
const query = `SELECT ?item ?itemLabel ?itemDescription (COUNT(*) AS ?count) WHERE {
?item wdt:P31 wd:Q5;
p:P735 ?statement.
?statement ps:P735 wd:${qid}.
OPTIONAL { ?statement pq:P1545 ?rank. }
BIND(IF(BOUND(?rank),?rank, 0) AS ?rank2)
FILTER(xsd:integer(?rank2) < xsd:integer(2))
?sitelink schema:about ?item.
FILTER (SUBSTR(str(?sitelink), 11, 15) = ".wikipedia.org/") .
SERVICE wikibase:label { bd:serviceParam wikibase:language "${lang},en". }
}
GROUP BY ?item ?itemLabel ?itemDescription
ORDER BY DESC (?count)
LIMIT ${n}`
return fetch(`https://query.wikidata.org/sparql?query=${encodeURIComponent(query)}`, {headers: {accept: "application/sparql-results+json"}})
.then(response => response.json())
.then(
d => d.results.bindings.map(
res => (
{
item: res.item.value,
itemLabel: res.itemLabel.value,
itemDescription: res.itemDescription.value,
count: res.count.value
}
)
)
)
.then(d => aq.from(d))
}
Insert cell
get_countryofcitizenship = (qid, n = 20, lang) => {
const query = `SELECT ?country ?countryLabel (COUNT(*) AS ?count) WHERE {
?item wdt:P31 wd:Q5;
wdt:P27 ?country;
p:P735 ?statement.
?statement ps:P735 wd:${qid}.
OPTIONAL { ?statement pq:P1545 ?rank. }
BIND(IF(BOUND(?rank),?rank, 0) AS ?rank2)
FILTER(xsd:integer(?rank2) < xsd:integer(2))
SERVICE wikibase:label { bd:serviceParam wikibase:language "${lang},en". }
}
GROUP BY ?country ?countryLabel
ORDER BY DESC (?count)
LIMIT ${n}`
return fetch(`https://query.wikidata.org/sparql?query=${encodeURIComponent(query)}`, {headers: {accept: "application/sparql-results+json"}})
.then(response => response.json())
.then(
d => d.results.bindings.map(
res => (
{
country: res.country.value,
countryLabel: res.countryLabel.value,
count: res.count.value
}
)
)
)
.then(d => aq.from(d))
}
Insert cell
get_occupation = (qid, n = 20, lang) => {
const query = `SELECT ?occupation ?occupationLabel (COUNT(*) AS ?count) WHERE {
?item wdt:P31 wd:Q5;
wdt:P106 ?occupation;
p:P735 ?statement.
?statement ps:P735 wd:${qid}.
OPTIONAL { ?statement pq:P1545 ?rank. }
BIND(IF(BOUND(?rank),?rank, 0) AS ?rank2)
FILTER(xsd:integer(?rank2) < xsd:integer(2))
SERVICE wikibase:label { bd:serviceParam wikibase:language "${lang},en". }
}
GROUP BY ?occupation ?occupationLabel
ORDER BY DESC (?count)
LIMIT ${n}`
return fetch(`https://query.wikidata.org/sparql?query=${encodeURIComponent(query)}`, {headers: {accept: "application/sparql-results+json"}})
.then(response => response.json())
.then(
d => d.results.bindings.map(
res => (
{
occupation: res.occupation.value,
occupationLabel: res.occupationLabel.value,
count: res.count.value
}
)
)
)
.then(d => aq.from(d))
}
Insert cell
get_placesofbirth = (qid, lang) => {
const query = `SELECT ?item ?itemLabel ?place ?placeLabel ?lat ?lon WHERE {
?item wdt:P31 wd:Q5;
wdt:P19 ?place;
p:P735 ?statement.
?statement ps:P735 wd:${qid}.
OPTIONAL { ?statement pq:P1545 ?rank. }
BIND(IF(BOUND(?rank),?rank, 0) AS ?rank2)
FILTER(xsd:integer(?rank2) < xsd:integer(2))
?place p:P625 [
psv:P625 [
wikibase:geoLongitude ?lon;
wikibase:geoLatitude ?lat;
]
].
SERVICE wikibase:label { bd:serviceParam wikibase:language "${lang},en". }
}
`;
return fetch(
`https://query.wikidata.org/sparql?query=${encodeURIComponent(query)}`,
{ headers: { accept: "application/sparql-results+json" } }
)
.then((response) => response.json())
.then((d) =>
d.results.bindings.map((res) => ({
item: res.item.value,
itemLabel: res.itemLabel.value,
place: res.place.value,
placeLabel: res.placeLabel.value,
lat: res.lat.value,
lon: res.lon.value
}))
);
}
Insert cell
import {get_label} from "@pac02/user-level-gender-statistics-for-wikipedia"
Insert cell
// colors from https://colorhunt.co/palette/86a3b8e8d2a6f48484f55050
function sparkbar(max) {
return (x) => htl.html`<div style="
background: #C0EEF2;
color: #444444 ;
width: ${(100 * x) / max}%;
float: left;
padding-right: 3px;
padding-left: 3px;
box-sizing: border-box;
overflow: visible;
display: flex;
justify-content: start;">${x.toLocaleString("en")}`;
}
Insert cell
geo = require("geotoolbox@1.9.3")
Insert cell
view = require("geoverview@1.2.1").then((f) => f.view)
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