rdflib-endpoint

đŸ’Ģ Deploy SPARQL endpoints from RDFLib Graphs to serve RDF files, machine learning models, or any other logic implemented in Python

https://github.com/vemonet/rdflib-endpoint

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • ✓
    CITATION.cff file
    Found CITATION.cff file
  • ✓
    codemeta.json file
    Found codemeta.json file
  • ✓
    .zenodo.json file
    Found .zenodo.json file
  • ○
    DOI references
  • ○
    Academic publication links
  • ○
    Committers with academic emails
  • ○
    Institutional organization owner
  • ○
    JOSS paper metadata
  • ○
    Scientific vocabulary similarity
    Low similarity (16.6%) to scientific vocabulary

Keywords

fastapi oxigraph python rdf rdflib sparql sparql-endpoints
Last synced: 6 months ago · JSON representation ·

Repository

đŸ’Ģ Deploy SPARQL endpoints from RDFLib Graphs to serve RDF files, machine learning models, or any other logic implemented in Python

Basic Info
Statistics
  • Stars: 90
  • Watchers: 4
  • Forks: 20
  • Open Issues: 1
  • Releases: 24
Topics
fastapi oxigraph python rdf rdflib sparql sparql-endpoints
Created almost 5 years ago · Last pushed 10 months ago
Metadata Files
Readme Contributing License Citation

README.md

# đŸ’Ģ SPARQL endpoint for RDFLib [![PyPI - Version](https://img.shields.io/pypi/v/rdflib-endpoint.svg?logo=pypi&label=PyPI&logoColor=silver)](https://pypi.org/project/rdflib-endpoint/) [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/rdflib-endpoint.svg?logo=python&label=Python&logoColor=silver)](https://pypi.org/project/rdflib-endpoint/) [![Test package](https://github.com/vemonet/rdflib-endpoint/actions/workflows/test.yml/badge.svg)](https://github.com/vemonet/rdflib-endpoint/actions/workflows/test.yml) [![Publish package](https://github.com/vemonet/rdflib-endpoint/actions/workflows/release.yml/badge.svg)](https://github.com/vemonet/rdflib-endpoint/actions/workflows/release.yml) [![Coverage Status](https://coveralls.io/repos/github/vemonet/rdflib-endpoint/badge.svg?branch=main)](https://coveralls.io/github/vemonet/rdflib-endpoint?branch=main) [![license](https://img.shields.io/pypi/l/rdflib-endpoint.svg?color=%2334D058)](https://github.com/vemonet/rdflib-endpoint/blob/main/LICENSE.txt) [![types - Mypy](https://img.shields.io/badge/types-mypy-blue.svg)](https://github.com/python/mypy)

rdflib-endpoint is a SPARQL endpoint based on RDFLib to easily serve RDF files locally, machine learning models, or any other logic implemented in Python via custom 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 a SERVICE query to another SPARQL endpoint.

Feel free to create an issue, or send a pull request if you are facing issues or would like to see a feature implemented.

â„šī¸ How it works

rdflib-endpoint can 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 using uvicorn.

The deployed SPARQL endpoint can be used as a SERVICE in 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 with RDFLib and FastAPI.

đŸ“Ļī¸ Installation

This package requires Python >=3.8, install it from PyPI with:

shell pip install rdflib-endpoint

The uvicorn and gunicorn dependencies are not included by default, if you want to install them use the optional dependency web:

bash pip install "rdflib-endpoint[web]"

If you want to use rdlib-endpoint as a CLI you can install with the optional dependency cli:

bash pip install "rdflib-endpoint[cli]"

If you want to use oxigraph as backend triplestore you can install with the optional dependency oxigraph:

bash pip install "rdflib-endpoint[cli,oxigraph]"

[!WARNING] Oxigraph and oxrdflib do not support custom functions, so it can be only used to deploy graphs without custom functions.

âŒ¨ī¸ Use the CLI

rdflib-endpoint can be used from the command line interface to perform basic utility tasks, such as serving or converting RDF files locally.

Make sure you installed rdflib-endpoint with the cli optional dependencies:

bash pip install "rdflib-endpoint[cli]"

âšĄī¸ Quickly serve RDF files through a SPARQL endpoint

Use rdflib-endpoint as 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:

bash rdflib-endpoint serve *.ttl *.jsonld *.nq

Then access the YASGUI SPARQL editor on http://localhost:8000

If 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 as COALESCE()):

bash rdflib-endpoint serve --store Oxigraph "*.ttl" "*.jsonld" "*.nq"

🔄 Convert RDF files to another format

rdflib-endpoint can also be used to quickly merge and convert files from multiple formats to a specific format:

bash rdflib-endpoint convert "*.ttl" "*.jsonld" "*.nq" --output "merged.trig"

✨ Deploy your SPARQL endpoint

rdflib-endpoint enables you to easily define and deploy SPARQL endpoints based on RDFLib Graph, and Dataset. Additionally it provides helpers to defines custom functions in the endpoint.

Checkout the example folder 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 this example folder, and start from it.

🚨 Deploy as a standalone API

Deploy your SPARQL endpoint as a standalone API:

```python from rdflib import Dataset from rdflib_endpoint import SparqlEndpoint

Start the SPARQL endpoint based on a RDFLib Graph and register your custom functions

g = Dataset()

TODO: Add triples in your graph

Then use either SparqlEndpoint or SparqlRouter, they take the same arguments

app = SparqlEndpoint( graph=g, path="/", corsenabled=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. \nSource code", version="0.1.0", publicurl='https://your-endpoint-url/', # Example query displayed in YASGUI default tab examplequery="""PREFIX myfunctions: https://w3id.org/sparql-functions/ SELECT ?concat ?concatLength WHERE { BIND("First" AS ?first) BIND(myfunctions:customconcat(?first, "last") AS ?concat) }""", # Additional example queries displayed in additional YASGUI tabs examplequeries = { "Bio2RDF query": { "endpoint": "https://bio2rdf.org/sparql", "query": """SELECT DISTINCT * WHERE { ?s a ?o . } LIMIT 10""", }, "Custom function": { "query": """PREFIX myfunctions: https://w3id.org/sparql-functions/ SELECT ?concat ?concatLength WHERE { BIND("First" AS ?first) BIND(myfunctions:customconcat(?first, "last") AS ?concat) }""", } } ) ```

Finally deploy this app using uvicorn (see below)

đŸ›Ŗī¸ Deploy as a router to include in an existing API

Deploy your SPARQL endpoint as an APIRouter to include in an existing FastAPI API. The SparqlRouter constructor takes the same arguments as the SparqlEndpoint, apart from enable_cors which needs be enabled at the API level.

```python from fastapi import FastAPI from rdflib import Dataset from rdflib_endpoint import SparqlRouter

g = Dataset() sparqlrouter = 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. \nSource code", version="0.1.0", publicurl='https://your-endpoint-url/', )

app = FastAPI() app.includerouter(sparqlrouter) ```

To deploy this route in a Flask app checkout how it has been done in the curies mapping service of the Bioregistry.

📝 Define custom SPARQL functions

This 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 the SparqlEndpoint and SparqlRouter classes.

Create a app/main.py file in your project folder with your custom SPARQL functions, and endpoint parameters:

````python import rdflib from rdflib import Dataset from rdflib.plugins.sparql.evalutils import eval from rdflibendpoint import SparqlEndpoint

def customconcat(queryresults, ctx, part, evalpart): """Concat 2 strings in the 2 senses and return the length as additional Length variable """ # Retrieve the 2 input arguments argument1 = str(eval(part.expr.expr[0], evalpart.forget(ctx, _except=part.expr.vars))) argument2 = str(eval(part.expr.expr[1], evalpart.forget(ctx, except=part.expr.vars))) evaluation = [] scores = [] # Prepare the 2 result string, 1 for eval, 1 for scores evaluation.append(argument1 + argument2) evaluation.append(argument2 + argument1) scores.append(len(argument1 + argument2)) scores.append(len(argument2 + argument1)) # Append the results for our custom function for i, result in enumerate(evaluation): queryresults.append(evalpart.merge({ part.var: rdflib.Literal(result), # With an additional custom var for the length rdflib.term.Variable(part.var + 'Length'): rdflib.Literal(scores[i]) })) return queryresults, ctx, part, evalpart

Start the SPARQL endpoint based on a RDFLib Graph and register your custom functions

g = Dataset(default_union=True)

Use either SparqlEndpoint or SparqlRouter, they take the same arguments

app = SparqlEndpoint( graph=g, path="/", # Register the functions: functions={ 'https://w3id.org/sparql-functions/customconcat': customconcat }, corsenabled=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. \nSource code", version="0.1.0", publicurl='https://your-endpoint-url/', # Example queries displayed in the Swagger UI to help users try your function examplequery="""PREFIX myfunctions: https://w3id.org/sparql-functions/ SELECT ?concat ?concatLength WHERE { BIND("First" AS ?first) BIND(myfunctions:customconcat(?first, "last") AS ?concat) }""" ) ````

âœ’ī¸ Or directly define the custom evaluation

You can also directly provide the custom evaluation function, this will override the functions.

Refer to the RDFLib documentation to define the custom evaluation function. Then provide it when instantiating the SPARQL endpoint:

```python import rdflib from rdflib.plugins.sparql.evaluate import evalBGP from rdflib.namespace import FOAF, RDF, RDFS

def custom_eval(ctx, part): """Rewrite triple patterns to get super-classes""" if part.name == "BGP": # rewrite triples triples = [] for t in part.triples: if t[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 evalBGP return evalBGP(ctx, triples) raise NotImplementedError()

app = SparqlEndpoint( graph=g, customeval=customeval ) ```

đŸĻ„ Run the SPARQL endpoint

You can then run the SPARQL endpoint server from the folder where your script is defined with uvicorn on http://localhost:8000

bash cd example uv run uvicorn main:app --reload

Checkout in the example/README.md for more details, such as deploying it with docker.

📂 Projects using rdflib-endpoint

Here are some projects using rdflib-endpoint to 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.
  • AKSW/sparql-file, serve a RDF file as an RDFLib Graph through a SPARQL endpoint.

đŸ› ī¸ Contributing

To run the project in development and make a contribution checkout the contributing page.

Owner

  • Name: Vincent Emonet
  • Login: vemonet
  • Kind: user
  • Location: Maastricht, Netherlands
  • Company: @MaastrichtU-IDS

Citation (CITATION.cff)

cff-version: 1.2.0
message: "If you use this software, please cite it as below."
authors:
  - orcid: https://orcid.org/0000-0002-1501-1082
    email: vincent.emonet@gmail.com
    given-names: Vincent Emonet
title: "RDFLib endpoint"
repository-code: https://github.com/vemonet/rdflib-endpoint
date-released: 2022-12-18
url: https://pypi.org/project/rdflib-endpoint
# doi: 10.48550/arXiv.2206.13787

GitHub Events

Total
  • Create event: 2
  • Issues event: 1
  • Release event: 2
  • Watch event: 13
  • Push event: 7
  • Fork event: 1
Last Year
  • Create event: 2
  • Issues event: 1
  • Release event: 2
  • Watch event: 13
  • Push event: 7
  • Fork event: 1

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 278
  • Total Committers: 4
  • Avg Commits per committer: 69.5
  • Development Distribution Score (DDS): 0.054
Past Year
  • Commits: 13
  • Committers: 1
  • Avg Commits per committer: 13.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Vincent Emonet v****t@g****m 263
Charles Tapley Hoyt c****t@g****m 10
datadave 6****v 3
Steve Bate s****b@s****t 2
Committer Domains (Top 20 + Academic)

Packages

  • Total packages: 2
  • Total downloads:
    • pypi 3,199 last-month
  • Total dependent packages: 1
    (may contain duplicates)
  • Total dependent repositories: 1
    (may contain duplicates)
  • Total versions: 24
  • Total maintainers: 1
proxy.golang.org: github.com/vemonet/rdflib-endpoint
  • Versions: 5
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 5.4%
Average: 5.6%
Dependent repos count: 5.8%
Last synced: 6 months ago
pypi.org: rdflib-endpoint

A package to deploy SPARQL endpoint to serve local RDF files, machine learning models, or any other logic implemented in Python, using RDFLib and FastAPI.

  • Homepage: https://github.com/vemonet/rdflib-endpoint
  • Documentation: https://github.com/vemonet/rdflib-endpoint
  • License: MIT License Copyright (c) 2022-present Vincent Emonet <vincent.emonet@gmail.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  • Latest release: 0.5.3
    published 12 months ago
  • Versions: 19
  • Dependent Packages: 1
  • Dependent Repositories: 1
  • Downloads: 3,199 Last month
Rankings
Dependent packages count: 4.8%
Downloads: 5.4%
Stargazers count: 9.5%
Average: 10.4%
Forks count: 10.5%
Dependent repos count: 21.6%
Maintainers (1)
Last synced: 6 months ago

Dependencies

.github/workflows/publish.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
  • pypa/gh-action-pypi-publish release/v1 composite
.github/workflows/test.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
  • github/codeql-action/analyze v2 composite
  • github/codeql-action/autobuild v2 composite
  • github/codeql-action/init v2 composite
  • warchant/setup-sonar-scanner v4 composite
example/Dockerfile docker
  • tiangolo/uvicorn-gunicorn-fastapi python3.8 build