Skip to content

Commit

Permalink
add HTML results format for the sparql query interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Callidon committed Dec 11, 2018
1 parent 4bb8b77 commit 460b8ee
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
11 changes: 5 additions & 6 deletions http_server/sparql_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ def execute_query(query, default_graph_uri, next_link, dataset, mimetype, url):
return Response(responses.w3c_json_streaming(bindings, next_page, stats, url), content_type='application/json')
if mimetype == "application/xml" or mimetype == "application/sparql-results+xml":
return Response(responses.w3c_xml(bindings, next_page, stats), content_type="application/xml")
return Response(responses.raw_json_streaming(bindings, next_page, stats, url), content_type='application/json')
if mimetype == "application/json":
return Response(responses.raw_json_streaming(bindings, next_page, stats, url), content_type='application/json')
# otherwise, return the HTML version
return render_template("sage_page.html", query=query, default_graph_uri=default_graph_uri, bindings=bindings, next_page=next_page, stats=stats)


def sparql_blueprint(dataset, logger):
Expand All @@ -61,17 +64,13 @@ def sparql_index():
])
try:
url = secure_url(request.base_url)
# serve the HTML web page
if mimetype == "text/html":
graphs = [dinfo for dinfo in dataset.describe(url)]
return render_template("interfaces.html", dataset=graphs)
# parse arguments
if request.method == "GET":
query = request.args.get("query") or None
default_graph_uri = request.args.get("default-graph-uri") or None
next_link = request.args.get("next") or None
# ensure that both the query and default-graph-uri params are set
if query is None or default_graph_uri is None:
if (query is None or default_graph_uri is None) and (next_link is None or default_graph_uri is None):
return sage_http_error("Invalid request sent to server: a GET request must contains both parameters 'query' and 'default-graph-uri'. See <a href='http://sage.univ-nantes.fr/documentation'>the API documentation</a> for reference.")
elif request.method == "POST" and request.is_json:
# POST query
Expand Down
18 changes: 18 additions & 0 deletions http_server/static/sage-voc.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,21 @@ sage:quota a owl:FunctionalProperty ;
rdfs:label "use the time quota"@en ;
rdfs:comment "Indicates the time quota (in milliseconds) used by a Sage endpoint to evaluate SPARQL queries against a RDF dataset "@en ,
"Indique le quota en temps (en millisecondes) utilisé le point d'accès Sage pour l'évaluation de requêtes SPARQL en utilisant un Dataset RDF"@fr .

sage:ResultsPage a rdf:Class ;
rdfs:label "A page of results from a SPARQL query sent to a SaGe endpoint"@en ;
rdfs:label "Une page de résulats obtenu arès évaluation d'une requête SPARQL par un endpoint SaGe"@fr.

sage:SolutionBinding a rdfs:Class ;
rdfs:label "A solution binding ?variable -> value"@en ;
rdfs:label "Un mapping de solution ?variable -> value"@fr .

sage:hasResult a owl:FunctionalProperty ;
rdfs:domain sage:ResultsPage ;
rdfs:range sage:SolutionBinding ;
rdfs:label "has a solution binding"@en .

sage:next a owl:FunctionalProperty ;
rdfs:domain sage:ResultsPage ;
rdfs:range sage:ResultsPage ;
rdfs:label "has a next page of query results"@en .
40 changes: 40 additions & 0 deletions http_server/templates/sage_page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>SPARQL query results</title>
<link rel="stylesheet" href="{{ url_for('static', filename='node_modules/bootstrap/dist/css/bootstrap.min.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='node_modules/@fortawesome/fontawesome-free/css/all.css')}}">
</head>
<body>
<div class="container" itemscope itemtype="http://sage.univ-nantes.fr/sage-voc#ResultsPage">
<h1>SPARQL query results</h1>

<h2>Query Statistics</h2>
<ul>
<li><strong>Default graph URI:</strong> {{default_graph_uri}}</li>
<li><strong>SPARQL query evaluated:</strong> <code>{{query}}</code></li>
<li><strong>Plan loading time:</strong> {{stats['import']}} milliseconds</li>
<li><strong>Plan serialization time:</strong> {{stats['export']}} milliseconds</li>
</ul>

{% if next_page %}
<h2>Hypermedia controls</h2>
<p><a href="{{ url_for('sparql-interface.sparql_index', next=next_page)}}&default-graph-uri={{default_graph_uri}}" itemprop="http://sage.univ-nantes.fr/sage-voc#next">Next page of results</a></p>
{% endif %}

<h2>Query Results</h2>
{% for binding in bindings %}
<ul>
{% for key, value in binding.items() %}
{% if value.startswith('http') %}
<li><strong>{{key}}:</strong> <a href="{{value}}" target="_blank">{{value}}</a></li>
{% else %}
<li><strong>{{key}}:</strong> {{value}}</li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
</div>
</body>
</html>

0 comments on commit 460b8ee

Please sign in to comment.