Skip to content

Commit

Permalink
Implement external SPARQL queries #791
Browse files Browse the repository at this point in the history
A new framework for external SPARQL has been implemented.
It is only used for the award aspect for now.
Jinja macros are defined in base.html for setting up HTML elements
from SPARQL querys.
HTML element identifiers should be suffix with either -table
or -iframe.
SPARQL template files should be named aspect_panel.sparql.
  • Loading branch information
fnielsen committed Nov 21, 2020
1 parent 508a1eb commit 8f37b60
Show file tree
Hide file tree
Showing 10 changed files with 338 additions and 209 deletions.
58 changes: 58 additions & 0 deletions scholia/app/static/scholia.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,61 @@ function sparqlToDataTable(sparql, element, options={}) {
'">Edit on query.Wikidata.org</a></caption>');
});
}


function sparqlToDataTable2(sparql, element, filename, options={}) {
// Options: linkPrefixes={}, paging=true
var linkPrefixes = (typeof options.linkPrefixes === 'undefined') ? {} : options.linkPrefixes;
var paging = (typeof options.paging === 'undefined') ? true : options.paging;
var sDom = (typeof options.sDom === 'undefined') ? 'lfrtip' : options.sDom;
var url = "https://query.wikidata.org/sparql?query=" +
encodeURIComponent(sparql) + '&format=json';

$.getJSON(url, function(response) {
var simpleData = sparqlDataToSimpleData(response);

convertedData = convertDataTableData(simpleData.data, simpleData.columns, linkPrefixes=linkPrefixes);
columns = [];
for ( i = 0 ; i < convertedData.columns.length ; i++ ) {
var column = {
data: convertedData.columns[i],
title: capitalizeFirstLetter(convertedData.columns[i]).replace(/_/g, "&nbsp;"),
defaultContent: "",
}
columns.push(column)
}

var table = $(element).DataTable({
data: convertedData.data,
columns: columns,
lengthMenu: [[10, 25, 100, -1], [10, 25, 100, "All"]],
ordering: true,
order: [],
paging: paging,
sDom: sDom,
});

$(element).append(
'<caption><span style="float:left; font-size:smaller;"><a href="https://query.wikidata.org/#' +
encodeURIComponent(sparql) +
'">Wikidata Query Service</a></span>' +
'<span style="float:right; font-size:smaller;"><a href="https://github.com/fnielsen/scholia/blob/master/templates/' +
filename + '">' +
filename.replace("_", ": ") +
'</a></span></caption>'
);
});
};




function sparqlToIframe(sparql, element, filename) {
url = "https://query.wikidata.org/embed.html#" + encodeURIComponent(sparql);
$(element).attr('src', url);
$(element).parent().after(
'<span style="float:right; font-size:smaller"><a href="https://github.com/fnielsen/scholia/blob/master/templates/' + filename + '">' +
filename.replace("_", ": ") +
'</a></span>');
};

131 changes: 25 additions & 106 deletions scholia/app/templates/award.html
Original file line number Diff line number Diff line change
@@ -1,111 +1,30 @@
{% extends "base.html" %}

{% set aspect = "award" %}

{% block scripts %}
{{super()}}

<script type="text/javascript">
listOfRecipientsSparql = `
SELECT
?year
?recipient ?recipientLabel
?example_work ?example_workLabel
WITH {
SELECT DISTINCT ?recipient ?year (SAMPLE(?work) AS ?example_work) WHERE {
?recipient p:P166 ?award_statement .
?award_statement ps:P166 wd:{{ q }} .
OPTIONAL {
{ ?award_statement pq:P585 ?time }
UNION
{ ?award_statement pq:P580 ?time }
BIND(YEAR(?time) AS ?year)
}
OPTIONAL { ?work wdt:P50 ?recipient . }
}
GROUP BY ?recipient ?year
} AS %result
WHERE {
INCLUDE %result
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,da,de,es,fr,jp,no,ru,sv,zh" . }
}
ORDER BY DESC(?year)
`

coAwardsSparql = `
SELECT
?number_of_corecipients
?award ?awardLabel ?awardDescription
WITH {
SELECT DISTINCT ?award (COUNT(?recipient) AS ?number_of_corecipients) WHERE {
?recipient wdt:P166 wd:{{ q }} .
?recipient wdt:P166 ?award .
FILTER (?award != wd:{{ q }})
}
GROUP BY ?award
} AS %result
WHERE {
INCLUDE %result
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,da,de,es,fr,jp,no,ru,sv,zh" . }
}
ORDER BY DESC(?number_of_corecipients)
`

recentPublicationsByRecipientsSparql = `
SELECT ?publication_date ?work ?workLabel ?recipient ?recipientLabel
WITH {
SELECT
(MAX(?publication_datetimes) AS ?publication_datetime)
?work ?recipient
WHERE {
?recipient wdt:P166 wd:{{ q }} .
?work wdt:P50 ?recipient .
OPTIONAL { ?work wdt:P577 ?publication_datetimes . }
}
GROUP BY ?work ?recipient
} AS %result
WHERE {
INCLUDE %result
BIND(xsd:date(?publication_datetime) AS ?publication_date)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,da,de,es,fr,jp,no,ru,sv,zh" . }
}
ORDER BY DESC(?publication_date)
LIMIT 500
`

genderDistributionSparql = `
SELECT ?count ?gender ?genderLabel
WITH {
SELECT (COUNT(DISTINCT ?recipient) AS ?count) ?gender WHERE {
?recipient wdt:P166 wd:{{ q }} .
?recipient wdt:P21 ?gender .
}
GROUP BY ?gender
} AS %result
WHERE {
INCLUDE %result
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,da,de,ep,fr,jp,nl,no,ru,sv,zh" . }
}
ORDER BY DESC(?count)
`



$(document).ready(function() {
sparqlToDataTable(listOfRecipientsSparql, "#list-of-recipients");
sparqlToDataTable(recentPublicationsByRecipientsSparql,
"#recent-publications-by-recipients");
sparqlToDataTable(coAwardsSparql, "#co-awards");
sparqlToDataTable(genderDistributionSparql, "#gender-distribution");
});
</script>


{% endblock %}
{% block in_ready %}

{{ sparql_to_table('list-of-recipients') }}

{% block page_content %}
{{ sparql_to_iframe('images-of-recipients') }}

{{ sparql_to_table('recent-publications-by-recipients') }}

{{ sparql_to_iframe('topics-of-works-by-recipients') }}

{{ sparql_to_table('coawards') }}

{{ sparql_to_iframe('locations-of-recipients') }}

{{ sparql_to_table('gender-distribution') }}

{% endblock %}



{% block page_content %}

<h1 id="h1">Award</h1>

Expand All @@ -116,47 +35,47 @@ <h1 id="h1">Award</h1>

<h2 id="List-of-recipients">List of recipients</h2>

<table class="table table-hover" id="list-of-recipients"></table>
<table class="table table-hover" id="list-of-recipients-table"></table>


<h2 id="Images-of-recipients">Images of recipients</h2>

<div class="embed-responsive embed-responsive-4by3">
<iframe class="embed-responsive-item" src="https://query.wikidata.org/embed.html#%23defaultView%3AImageGrid%0ASELECT%0A%20%20%3Fyear%0A%20%20%3Frecipient%20%3FrecipientLabel%20%0A%20%20%3Fimage%0AWITH%20%7B%0A%20%20SELECT%20%3Frecipient%20%3Fyear%20%28SAMPLE%28%3Fimage_%29%20AS%20%3Fimage%29%20WHERE%20%7B%0A%20%20%20%20%3Frecipient%20p%3AP166%20%3Faward_statement%20.%20%0A%20%20%20%20%3Faward_statement%20ps%3AP166%20wd%3A{{ q }}%20.%0A%20%20%20%20OPTIONAL%20%7B%0A%20%20%20%20%20%20%3Faward_statement%20pq%3AP585%20%3Ftime%20.%0A%09%20%20BIND%28YEAR%28%3Ftime%29%20AS%20%3Fyear%29%0A%09%7D%0A%20%20%20%20%3Frecipient%20wdt%3AP18%20%3Fimage_%20.%0A%20%20%7D%0A%20%20GROUP%20BY%20%3Frecipient%20%3Fyear%0A%7D%20AS%20%25result%0AWHERE%20%7B%0A%20%20INCLUDE%20%25result%20%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%2Cda%2Cde%2Ces%2Cfr%2Cjp%2Cno%2Cru%2Csv%2Czh%22%20.%20%7D%20%20%0A%7D%0AORDER%20BY%20DESC%28%3Fyear%29%0A%20%20"></iframe>
<iframe class="embed-responsive-item" id="images-of-recipients-iframe"></iframe>
</div>


<h2 id="Topics-of-works-by-recipients">Topics of works by recipients</h2>

<div class="embed-responsive embed-responsive-4by3">
<iframe class="embed-responsive-item" src="https://query.wikidata.org/embed.html#%23defaultView%3ABubbleChart%0A%23%20Count%20the%20number%20of%20time%20works%20by%20award%20recipients%20have%20set%20a%20main%20topic%0ASELECT%20%3Fcount%20%3Ftopic%20%3FtopicLabel%20%0AWITH%20%7B%0A%20%20SELECT%20%3Ftopic%20%28COUNT%28%3Fwork%29%20AS%20%3Fcount%29%20WHERE%20%7B%0A%20%20%20%20%3Frecipient%20wdt%3AP166%20wd%3A{{ q }}%20.%0A%20%20%20%20%3Fwork%20wdt%3AP50%20%3Frecipient%20.%0A%20%20%20%20%3Fwork%20wdt%3AP921%20%3Ftopic%20.%0A%20%20%7D%0A%20%20GROUP%20BY%20%3Ftopic%0A%7D%20AS%20%25result%0AWHERE%20%7B%0A%20%20INCLUDE%20%25result%20%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%2Cda%2Cde%2Ces%2Cfr%2Cjp%2Cno%2Cru%2Csv%2Czh%22%20.%20%7D%20%20%0A%7D%0AORDER%20BY%20DESC%28%3Fcount%29%0ALIMIT%2050"></iframe>
<iframe class="embed-responsive-item" id="topics-of-works-by-recipients-iframe"></iframe>
</div>


<h2 id="Recent-publications-by-recipients">Recent publications by recipients</h2>

<table class="table table-hover" id="recent-publications-by-recipients"></table>
<table class="table table-hover" id="recent-publications-by-recipients-table"></table>


<h2 id="Co-awards">Co-awards</h2>

Awards with co-recipients

<table class="table table-hover" id="co-awards"></table>
<table class="table table-hover" id="coawards-table"></table>


<h2 id="Locations-of-recipients">Locations of recipients</h2>

<div class="embed-responsive embed-responsive-4by3">
<iframe class="embed-responsive-item" src="https://query.wikidata.org/embed.html#%23defaultView%3AMap%0ASELECT%20DISTINCT%20%3Frecipient%20%3FrecipientLabel%20%3Fimage%20%3Fitem%20%3FitemLabel%20%3Fgeo%20%3Flayer%0AWITH%20%7B%0A%20%20SELECT%20DISTINCT%20%3Frecipient%20%3Fimage%20%3Fitem%20%3Fgeo%20%28%3Fproperty_item_label%20AS%20%3Flayer%29%20WHERE%20%7B%0A%20%20%20%20%3Frecipient%20wdt%3AP166%20wd%3A{{ q }}%20.%0A%20%20%20%20%3Frecipient%20%3Fproperty%20%3Fitem%20.%20%0A%20%20%20%20%3Fitem%20wdt%3AP625%20%3Fgeo%20.%0A%20%20%20%20%3Fproperty_item%20wikibase%3AdirectClaim%20%3Fproperty%20.%0A%20%20%20%20%3Fproperty_item%20rdfs%3Alabel%20%3Fproperty_item_label%20.%20FILTER%20%28LANG%28%3Fproperty_item_label%29%20%3D%20%27en%27%29%0A%20%20%20%20OPTIONAL%20%7B%20%3Fitem%20wdt%3AP18%20%3Fimage%20.%20%7D%0A%20%20%7D%0A%7D%20AS%20%25result%0AWHERE%20%7B%0A%20%20INCLUDE%20%25result%0A%20%20SERVICE%20wikibase%3Alabel%20%7B%20bd%3AserviceParam%20wikibase%3Alanguage%20%22en%2Cda%2Cde%2Ces%2Cfr%2Cjp%2Cnl%2Cno%2Cru%2Csv%2Czh%22%20.%20%7D%20%0A%7D"></iframe>
<iframe class="embed-responsive-item" id="locations-of-recipients-iframe"></iframe>
</div>


<h2 id="Gender-distribution">Gender distribution</h2>

Count of the number of recipient wrt. gender.

<table class="table table-hover" id="gender-distribution"></table>
<table class="table table-hover" id="gender-distribution-table"></table>

Any information missing here? You can help curate the information contained in this profile via its <a href="{{ url_for('app.show_award_empty') }}{{ q }}/missing">missing</a> page.

Expand Down
16 changes: 16 additions & 0 deletions scholia/app/templates/award_coawards.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SELECT
?number_of_corecipients
?award ?awardLabel ?awardDescription
WITH {
SELECT DISTINCT ?award (COUNT(?recipient) AS ?number_of_corecipients) WHERE {
?recipient wdt:P166 wd:{{ q }} .
?recipient wdt:P166 ?award .
FILTER (?award != wd:{{ q }})
}
GROUP BY ?award
} AS %result
WHERE {
INCLUDE %result
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,da,de,es,fr,jp,no,ru,sv,zh" . }
}
ORDER BY DESC(?number_of_corecipients)
14 changes: 14 additions & 0 deletions scholia/app/templates/award_gender-distribution.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SELECT
?count ?gender ?genderLabel
WITH {
SELECT (COUNT(DISTINCT ?recipient) AS ?count) ?gender WHERE {
?recipient wdt:P166 wd:{{ q }} .
?recipient wdt:P21 ?gender .
}
GROUP BY ?gender
} AS %result
WHERE {
INCLUDE %result
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,da,de,ep,fr,jp,nl,no,ru,sv,zh" . }
}
ORDER BY DESC(?count)
23 changes: 23 additions & 0 deletions scholia/app/templates/award_images-of-recipients.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#defaultView:ImageGrid
SELECT
?year
?recipient ?recipientLabel
?image
WITH {
SELECT ?recipient ?year (SAMPLE(?image_) AS ?image) WHERE {
?recipient p:P166 ?award_statement .
?award_statement ps:P166 wd:{{ q }} .
OPTIONAL {
?award_statement pq:P585 ?time .
BIND(YEAR(?time) AS ?year)
}
?recipient wdt:P18 ?image_ .
}
GROUP BY ?recipient ?year
} AS %result
WHERE {
INCLUDE %result
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,da,de,es,fr,jp,no,ru,sv,zh" . }
}
ORDER BY DESC(?year)

23 changes: 23 additions & 0 deletions scholia/app/templates/award_list-of-recipients.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
SELECT
?year
?recipient ?recipientLabel
?example_work ?example_workLabel
WITH {
SELECT DISTINCT ?recipient ?year (SAMPLE(?work) AS ?example_work) WHERE {
?recipient p:P166 ?award_statement .
?award_statement ps:P166 wd:{{ q }} .
OPTIONAL {
{ ?award_statement pq:P585 ?time }
UNION
{ ?award_statement pq:P580 ?time }
BIND(YEAR(?time) AS ?year)
}
OPTIONAL { ?work wdt:P50 ?recipient . }
}
GROUP BY ?recipient ?year
} AS %result
WHERE {
INCLUDE %result
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,da,de,es,fr,jp,no,ru,sv,zh" . }
}
ORDER BY DESC(?year)
16 changes: 16 additions & 0 deletions scholia/app/templates/award_locations-of-recipients.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#defaultView:Map
SELECT DISTINCT ?recipient ?recipientLabel ?image ?item ?itemLabel ?geo ?layer
WITH {
SELECT DISTINCT ?recipient ?image ?item ?geo (?property_item_label AS ?layer) WHERE {
?recipient wdt:P166 wd:{{ q }} .
?recipient ?property ?item .
?item wdt:P625 ?geo .
?property_item wikibase:directClaim ?property .
?property_item rdfs:label ?property_item_label . FILTER (LANG(?property_item_label) = 'en')
OPTIONAL { ?item wdt:P18 ?image . }
}
} AS %result
WHERE {
INCLUDE %result
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,da,de,es,fr,jp,nl,no,ru,sv,zh" . }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SELECT ?publication_date ?work ?workLabel ?recipient ?recipientLabel
WITH {
SELECT
(MAX(?publication_datetimes) AS ?publication_datetime)
?work ?recipient
WHERE {
?recipient wdt:P166 wd:{{ q }} .
?work wdt:P50 ?recipient .
OPTIONAL { ?work wdt:P577 ?publication_datetimes . }
}
GROUP BY ?work ?recipient
} AS %result
WHERE {
INCLUDE %result
BIND(xsd:date(?publication_datetime) AS ?publication_date)
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en,da,de,es,fr,jp,no,ru,sv,zh" . }
}
ORDER BY DESC(?publication_date)
LIMIT 500
17 changes: 17 additions & 0 deletions scholia/app/templates/award_topics-of-works-by-recipients.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#defaultView:BubbleChart
# Count the number of time works by award recipients have set a main topic
SELECT ?count ?topic ?topicLabel
WITH {
SELECT ?topic (COUNT(?work) AS ?count) WHERE {
?recipient wdt:P166 wd:{{ q }} .
?work wdt:P50 ?recipient .
?work wdt:P921 ?topic .
}
GROUP BY ?topic
} AS %result
WHERE {
INCLUDE %result
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,da,de,es,fr,jp,no,ru,sv,zh" . }
}
ORDER BY DESC(?count)
LIMIT 50
Loading

0 comments on commit 8f37b60

Please sign in to comment.