Skip to content
Snippets Groups Projects
Commit 0103bfef authored by Quentin Ulmer's avatar Quentin Ulmer :construction_worker_tone2:
Browse files

- prepare for Frontend Data #2

parent b7875060
No related branches found
No related tags found
1 merge request!4Merge newest version into main
......@@ -5,46 +5,38 @@ from SPARQLWrapper import SPARQLWrapper, JSON
import os
import json
def sparql_query(request):
"""
This function executes a SPARQL query and returns the results in JSON format.
The SPARQL endpoint is read from the environment variables. (set in Docker-compose)
"""
sparql_endpoint = 'http://fuseki:3030/data/query'
sparql = SPARQLWrapper(sparql_endpoint)
sparql.setReturnFormat(JSON)
sparql.setQuery("""
PREFIX : <http://h-da.de/fbi/art/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX wd: <http://www.wikidata.org/entity/>
SELECT ?g ?db ?dd ?pb ?pd
WHERE {
?p a :person ;
rdfs:label "Michelangelo";
:gender ?g;
:date_of_birth ?db;
:date_of_death ?dd ;
:place_of_death ?pd .
}
""")
try:
response = sparql.query().convert()
# Log the raw response for debugging
if isinstance(response, bytes):
raw_response = response.decode('utf-8')
else:
raw_response = response
print("Raw response:", raw_response) # Debugging output
# Ensure the response is properly decoded
if isinstance(response, bytes):
response = json.loads(response.decode('utf-8'))
results = response.get("results", {}).get("bindings", [])
return JsonResponse(results, safe=False)
except Exception as e:
return JsonResponse({"error": str(e)}, status=500)
"""
This function executes a SPARQL query received from the frontend and returns the results in JSON format.
The SPARQL endpoint is read from the environment variables. (set in Docker-compose)
"""
sparql_endpoint = 'http://fuseki:3030/data/query'
sparql = SPARQLWrapper(sparql_endpoint)
sparql.setReturnFormat(JSON)
try:
# Extract SPARQL query from POST request
body = json.loads(request.body)
sparql_query = body.get('query', '')
if not sparql_query:
return JsonResponse({"error": "No query provided"}, status=400)
sparql.setQuery(sparql_query)
response = sparql.query().convert()
# Log the raw response for debugging
if isinstance(response, bytes):
raw_response = response.decode('utf-8')
else:
raw_response = response
print("Raw response:", raw_response) # Debugging output
# Ensure the response is properly decoded
if isinstance(response, bytes):
response = json.loads(response.decode('utf-8'))
results = response.get("results", {}).get("bindings", [])
return JsonResponse(results, safe=False)
except Exception as e:
return JsonResponse({"error": str(e)}, status=500)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment