Package: rdflib-endpoint
💫 SPARQL endpoint for RDFLibrdflib-endpointis a SPARQL endpoint based on RDFLib toeasily serve RDF files locally, machine learning models, or any other logic implemented in Python viacustom SPARQL functions.It aims to enable python developers to easily deploy functions that can be queried in a federated fashion using SPARQL. For example: using a python function to resolve labels for specific identifiers, or run a classifier given entities retrieved using aSERVICEquery to another SPARQL endpoint.Feel free to create anissue, or send a pull request if you are facing issues or would like to see a feature implemented.ℹ️ How it worksrdflib-endpointcan be used directly from the terminal to quickly serve RDF files through a SPARQL endpoint automatically deployed locally.It can also be used to define custom SPARQL functions: the user defines and registers custom SPARQL functions, and/or populate the RDFLib Graph using Python, then the endpoint is started usinguvicorn/gunicorn.The deployed SPARQL endpoint can be used as aSERVICEin a federated SPARQL query from regular triplestores SPARQL endpoints. Tested on OpenLink Virtuoso (Jena based) and Ontotext GraphDB (RDF4J based). The endpoint is CORS enabled by default to enable querying it from client JavaScript (can be turned off).Built withRDFLibandFastAPI.📦️ InstallationThis package requires Python >=3.8, install it  fromPyPIwith:pipinstallrdflib-endpointTheuvicornandgunicorndependencies are not included by default, if you want to install them use the optional dependencyweb:pipinstall"rdflib-endpoint[web]"If you want to userdlib-endpointas a CLI you can install with the optional dependencycli:pipinstall"rdflib-endpoint[cli]"If you want to useoxigraphas backend triplestore you can install with the optional dependencyoxigraph:pipinstall"rdflib-endpoint[oxigraph,cli]"[!WARNING]
Oxigraph andoxrdflibdo not support custom functions, so it can be only used to deploy graphs without custom functions.⌨️ Use the CLIrdflib-endpointcan be used from the command line interface to perform basic utility tasks, such as serving or converting RDF files locally.Make sure you installedrdflib-endpointwith theclioptional dependencies:pipinstall"rdflib-endpoint[cli]"⚡️ Quickly serve RDF files through a SPARQL endpointUserdflib-endpointas a command line interface (CLI) in your terminal to quickly serve one or multiple RDF files as a SPARQL endpoint.You can use wildcard and provide multiple files, for example to serve all turtle, JSON-LD and nquads files in the current folder you could run:rdflib-endpointserve*.ttl*.jsonld*.nqThen access the YASGUI SPARQL editor onhttp://localhost:8000If you installed with the Oxigraph optional dependency you can use it as backend triplestore, it is faster and supports some functions that are not supported by the RDFLib query engine (such asCOALESCE()):rdflib-endpointserve--storeOxigraph"*.ttl""*.jsonld""*.nq"🔄 Convert RDF files to another formatrdflib-endpointcan also be used to quickly merge and convert files from multiple formats to a specific format:rdflib-endpointconvert"*.ttl""*.jsonld""*.nq"--output"merged.trig"✨ Deploy your SPARQL endpointrdflib-endpointenables you to easily define and deploy SPARQL endpoints based on RDFLib Graph, ConjunctiveGraph, and Dataset. Additionally it provides helpers to defines custom functions in the endpoint.Checkout theexamplefolder for a complete working app example to get started, including a docker deployment. A good way to create a new SPARQL endpoint is to copy thisexamplefolder, and start from it.🚨 Deploy as a standalone APIDeploy your SPARQL endpoint as a standalone API:fromrdflibimportConjunctiveGraphfromrdflib_endpointimportSparqlEndpoint# Start the SPARQL endpoint based on a RDFLib Graph and register your custom functionsg=ConjunctiveGraph()# TODO: Add triples in your graph# Then use either SparqlEndpoint or SparqlRouter, they take the same argumentsapp=SparqlEndpoint(graph=g,path="/",cors_enabled=True,# Metadata used for the SPARQL service description and Swagger UI:title="SPARQL endpoint for RDFLib graph",description="A SPARQL endpoint to serve machine learning models, or any other logic implemented in Python.\n[Source code](https://github.com/vemonet/rdflib-endpoint)",version="0.1.0",public_url='https://your-endpoint-url/',# Example query displayed in YASGUI default tabexample_query="""PREFIX myfunctions: <https://w3id.org/um/sparql-functions/>SELECT ?concat ?concatLength WHERE {BIND("First" AS ?first)BIND(myfunctions:custom_concat(?first, "last") AS ?concat)}""",# Additional example queries displayed in additional YASGUI tabsexample_queries={"Bio2RDF query":{"endpoint":"https://bio2rdf.org/sparql","query":"""SELECT DISTINCT * WHERE {?s a ?o .} LIMIT 10""",},"Custom function":{"query":"""PREFIX myfunctions: <https://w3id.org/um/sparql-functions/>SELECT ?concat ?concatLength WHERE {BIND("First" AS ?first)BIND(myfunctions:custom_concat(?first, "last") AS ?concat)}""",}})Finally deploy this app usinguvicorn(see below)🛣️ Deploy as a router to include in an existing APIDeploy your SPARQL endpoint as anAPIRouterto include in an existingFastAPIAPI. TheSparqlRouterconstructor takes the same arguments as theSparqlEndpoint, apart fromenable_corswhich needs be enabled at the API level.fromfastapiimportFastAPIfromrdflibimportConjunctiveGraphfromrdflib_endpointimportSparqlRouterg=ConjunctiveGraph()sparql_router=SparqlRouter(graph=g,path="/",# Metadata used for the SPARQL service description and Swagger UI:title="SPARQL endpoint for RDFLib graph",description="A SPARQL endpoint to serve machine learning models, or any other logic implemented in Python.\n[Source code](https://github.com/vemonet/rdflib-endpoint)",version="0.1.0",public_url='https://your-endpoint-url/',)app=FastAPI()app.include_router(sparql_router)📝 Define custom SPARQL functionsThis option makes it easier to define functions in your SPARQL endpoint, e.g.BIND(myfunction:custom_concat("start", "end") AS ?concat). It can be used with theSparqlEndpointandSparqlRouterclasses.Create aapp/main.pyfile in your project folder with your custom SPARQL functions, and endpoint parameters:importrdflibfromrdflibimportConjunctiveGraphfromrdflib.plugins.sparql.evalutilsimport_evalfromrdflib_endpointimportSparqlEndpointdefcustom_concat(query_results,ctx,part,eval_part):"""Concat 2 strings in the 2 senses and return the length as additional Length variable"""# Retrieve the 2 input argumentsargument1=str(_eval(part.expr.expr[0],eval_part.forget(ctx,_except=part.expr._vars)))argument2=str(_eval(part.expr.expr[1],eval_part.forget(ctx,_except=part.expr._vars)))evaluation=[]scores=[]# Prepare the 2 result string, 1 for eval, 1 for scoresevaluation.append(argument1+argument2)evaluation.append(argument2+argument1)scores.append(len(argument1+argument2))scores.append(len(argument2+argument1))# Append the results for our custom functionfori,resultinenumerate(evaluation):query_results.append(eval_part.merge({part.var:rdflib.Literal(result),# With an additional custom var for the lengthrdflib.term.Variable(part.var+'Length'):rdflib.Literal(scores[i])}))returnquery_results,ctx,part,eval_part# Start the SPARQL endpoint based on a RDFLib Graph and register your custom functionsg=ConjunctiveGraph()# Use either SparqlEndpoint or SparqlRouter, they take the same argumentsapp=SparqlEndpoint(graph=g,path="/",# Register the functions:functions={'https://w3id.org/um/sparql-functions/custom_concat':custom_concat},cors_enabled=True,# Metadata used for the SPARQL service description and Swagger UI:title="SPARQL endpoint for RDFLib graph",description="A SPARQL endpoint to serve machine learning models, or any other logic implemented in Python.\n[Source code](https://github.com/vemonet/rdflib-endpoint)",version="0.1.0",public_url='https://your-endpoint-url/',# Example queries displayed in the Swagger UI to help users try your functionexample_query="""PREFIX myfunctions: <https://w3id.org/um/sparql-functions/>SELECT ?concat ?concatLength WHERE {BIND("First" AS ?first)BIND(myfunctions:custom_concat(?first, "last") AS ?concat)}""")✒️ Or directly define the custom evaluationYou can also directly provide the custom evaluation function, this will override thefunctions.Refer to theRDFLib documentationto define the custom evaluation function. Then provide it when instantiating the SPARQL endpoint:importrdflibfromrdflib.plugins.sparql.evaluateimportevalBGPfromrdflib.namespaceimportFOAF,RDF,RDFSdefcustom_eval(ctx,part):"""Rewrite triple patterns to get super-classes"""ifpart.name=="BGP":# rewrite triplestriples=[]fortinpart.triples:ift[1]==RDF.type:bnode=rdflib.BNode()triples.append((t[0],t[1],bnode))triples.append((bnode,RDFS.subClassOf,t[2]))else:triples.append(t)# delegate to normal evalBGPreturnevalBGP(ctx,triples)raiseNotImplementedError()app=SparqlEndpoint(graph=g,custom_eval=custom_eval)🦄 Run the SPARQL endpointYou can then run the SPARQL endpoint server from the folder where your script is defined withuvicornonhttp://localhost:8000(it is installed automatically when you install therdflib-endpointpackage)uvicornmain:app--app-direxample/app--reloadCheckout in theexample/README.mdfor more details, such as deploying it with docker.📂 Projects using rdflib-endpointHere are some projects usingrdflib-endpointto deploy custom SPARQL endpoints with python:The Bioregistry, an open source, community curated registry, meta-registry, and compact identifier resolver.proycon/codemeta-server, server for codemeta, in memory triple store, SPARQL endpoint and simple web-based visualisation for end-user🛠️ ContributingTo run the project in development and make a contribution checkout thecontributing page.