• Stars
    star
    1,985
  • Rank 23,178 (Top 0.5 %)
  • Language
    Python
  • License
    BSD 3-Clause "New...
  • Created over 12 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

RDFLib is a Python library for working with RDF, a simple yet powerful language for representing information.

RDFLib

Build Status Documentation Status Coveralls branch

GitHub stars Downloads PyPI PyPI DOI

Contribute with Gitpod Gitter Matrix

RDFLib is a pure Python package for working with RDF. RDFLib contains most things you need to work with RDF, including:

  • parsers and serializers for RDF/XML, N3, NTriples, N-Quads, Turtle, TriX, Trig and JSON-LD
  • a Graph interface which can be backed by any one of a number of Store implementations
  • store implementations for in-memory, persistent on disk (Berkeley DB) and remote SPARQL endpoints
  • a SPARQL 1.1 implementation - supporting SPARQL 1.1 Queries and Update statements
  • SPARQL function extension mechanisms

RDFlib Family of packages

The RDFlib community maintains many RDF-related Python code repositories with different purposes. For example:

  • rdflib - the RDFLib core
  • sparqlwrapper - a simple Python wrapper around a SPARQL service to remotely execute your queries
  • pyLODE - An OWL ontology documentation tool using Python and templating, based on LODE.
  • pyrdfa3 - RDFa 1.1 distiller/parser library: can extract RDFa 1.1/1.0 from (X)HTML, SVG, or XML in general.
  • pymicrodata - A module to extract RDF from an HTML5 page annotated with microdata.
  • pySHACL - A pure Python module which allows for the validation of RDF graphs against SHACL graphs.
  • OWL-RL - A simple implementation of the OWL2 RL Profile which expands the graph with all possible triples that OWL RL defines.

Please see the list for all packages/repositories here:

Help with maintenance of all of the RDFLib family of packages is always welcome and appreciated.

Versions & Releases

  • 7.0.0a0 current main branch and supports Python 3.8.1+ only.
  • 6.x.y current release and support Python 3.7+ only. Many improvements over 5.0.0
  • 5.x.y supports Python 2.7 and 3.4+ and is mostly backwards compatible with 4.2.2.

See https://rdflib.dev for the release overview.

Documentation

See https://rdflib.readthedocs.io for our documentation built from the code. Note that there are latest, stable 5.0.0 and 4.2.2 documentation versions, matching releases.

Installation

The stable release of RDFLib may be installed with Python's package management tool pip:

$ pip install rdflib

Alternatively manually download the package from the Python Package Index (PyPI) at https://pypi.python.org/pypi/rdflib

The current version of RDFLib is 6.3.2, see the CHANGELOG.md file for what's new in this release.

Installation of the current main branch (for developers)

With pip you can also install rdflib from the git repository with one of the following options:

$ pip install git+https://github.com/rdflib/rdflib@main

or

$ pip install -e git+https://github.com/rdflib/rdflib@main#egg=rdflib

or from your locally cloned repository you can install it with one of the following options:

$ poetry install  # installs into a poetry-managed venv

or

$ pip install -e .

Getting Started

RDFLib aims to be a pythonic RDF API. RDFLib's main data object is a Graph which is a Python collection of RDF Subject, Predicate, Object Triples:

To create graph and load it with RDF data from DBPedia then print the results:

from rdflib import Graph
g = Graph()
g.parse('http://dbpedia.org/resource/Semantic_Web')

for s, p, o in g:
    print(s, p, o)

The components of the triples are URIs (resources) or Literals (values).

URIs are grouped together by namespace, common namespaces are included in RDFLib:

from rdflib.namespace import DC, DCTERMS, DOAP, FOAF, SKOS, OWL, RDF, RDFS, VOID, XMLNS, XSD

You can use them like this:

from rdflib import Graph, URIRef, Literal
from rdflib.namespace import RDFS, XSD

g = Graph()
semweb = URIRef('http://dbpedia.org/resource/Semantic_Web')
type = g.value(semweb, RDFS.label)

Where RDFS is the RDFS namespace, XSD the XML Schema Datatypes namespace and g.value returns an object of the triple-pattern given (or an arbitrary one if multiple exist).

Or like this, adding a triple to a graph g:

g.add((
    URIRef("http://example.com/person/nick"),
    FOAF.givenName,
    Literal("Nick", datatype=XSD.string)
))

The triple (in n-triples notation) <http://example.com/person/nick> <http://xmlns.com/foaf/0.1/givenName> "Nick"^^<http://www.w3.org/2001/XMLSchema#string> . is created where the property FOAF.givenName is the URI <http://xmlns.com/foaf/0.1/givenName> and XSD.string is the URI <http://www.w3.org/2001/XMLSchema#string>.

You can bind namespaces to prefixes to shorten the URIs for RDF/XML, Turtle, N3, TriG, TriX & JSON-LD serializations:

g.bind("foaf", FOAF)
g.bind("xsd", XSD)

This will allow the n-triples triple above to be serialised like this:

print(g.serialize(format="turtle"))

With these results:

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

<http://example.com/person/nick> foaf:givenName "Nick"^^xsd:string .

New Namespaces can also be defined:

dbpedia = Namespace('http://dbpedia.org/ontology/')

abstracts = list(x for x in g.objects(semweb, dbpedia['abstract']) if x.language=='en')

See also ./examples

Features

The library contains parsers and serializers for RDF/XML, N3, NTriples, N-Quads, Turtle, TriX, JSON-LD, RDFa and Microdata.

The library presents a Graph interface which can be backed by any one of a number of Store implementations.

This core RDFLib package includes store implementations for in-memory storage and persistent storage on top of the Berkeley DB.

A SPARQL 1.1 implementation is included - supporting SPARQL 1.1 Queries and Update statements.

RDFLib is open source and is maintained on GitHub. RDFLib releases, current and previous are listed on PyPI

Multiple other projects are contained within the RDFlib "family", see https://github.com/RDFLib/.

Running tests

Running the tests on the host

Run the test suite with pytest.

poetry install
poetry run pytest

Running test coverage on the host with coverage report

Run the test suite and generate a HTML coverage report with pytest and pytest-cov.

poetry run pytest --cov

Viewing test coverage

Once tests have produced HTML output of the coverage report, view it by running:

poetry run pytest --cov --cov-report term --cov-report html
python -m http.server --directory=htmlcov

Contributing

RDFLib survives and grows via user contributions! Please read our contributing guide and developers guide to get started. Please consider lodging Pull Requests here:

To get a development environment consider using Gitpod or Google Cloud Shell.

Open in Gitpod Open in Cloud Shell

You can also raise issues here:

Support & Contacts

For general "how do I..." queries, please use https://stackoverflow.com and tag your question with rdflib. Existing questions:

If you want to contact the rdflib maintainers, please do so via:

More Repositories

1

sparqlwrapper

A wrapper for a remote SPARQL endpoint
Python
492
star
2

rdflib-jsonld

JSON-LD parser and serializer plugins for RDFLib
Python
275
star
3

pySHACL

A Python validator for SHACL
Python
242
star
4

pyLODE

An OWL ontology documentation tool using Python and templating, based on LODE
Python
161
star
5

OWL-RL

A simple implementation of the OWL2 RL Profile on top of RDFLib: it expands the graph with all possible triples that OWL RL defines. It can be used together with RDFLib to expand an RDFLib Graph object, or as a stand alone service with its own serialization.
HTML
140
star
6

rdflib-sqlalchemy

RDFLib store using SQLAlchemy dbapi as back-end
Python
132
star
7

graph-pattern-learner

Evolutionary Graph Pattern Learner that learns SPARQL queries for a given set of source-target-pairs from an endpoint.
Python
83
star
8

pyrdfa3

RDFa 1.1 distiller/parser library: can extract RDFa 1.1 (and RDFa 1.0, if properly set via a @version attribute) from (X)HTML, SVG, or XML in general. The module can be used to produce serialized versions of the extracted graph, or simply an RDFLib Graph.
HTML
67
star
9

FuXi

Chimezie Ogbuji's FuXi reasoner. NON-FUNCTIONING, RETAINED FOR ARCHIVAL PURPOSES. For working code plus version and associated support requirements see:
Web Ontology Language
51
star
10

rdflib-web

Two Flask web-apps for quickly setting up a SPARQL Endpoint or a LOD app for RDFLib
Python
43
star
11

pymicrodata

This a module to extract RDF from an HTML5 page annotated with microdata. The module implements the algorithm defined and published by the W3C Semantic Web Interest Group task force, in March 2012. The module can be used to produce serialized versions of the extracted graph, or simply an RDFLib Graph Object.
HTML
42
star
12

pyLDAPI

A very small module to add Linked Data API functionality to a Python Flask installation
Python
41
star
13

rdflib-hdt

A Store back-end for rdflib to allow for reading and querying HDT documents
C++
23
star
14

VocPrez

A tool (API and web front-end) for the read-only delivery system of SKOS vocabularies.
Python
21
star
15

rdfextras

(discontinued) RDFExtras is a collection of packages providing extras based on RDFLib - PINNED TO RDFLIB 3
Python
20
star
16

rdflib-sparql

(discontinued) A SPARQL 1.1 implementation for RDFLib - PINNED TO RDFLIB 3
Python
18
star
17

prez

Prez is a data-configurable Linked Data API framework that delivers profiles of Knowledge Graph data according to the Content Negotiation by Profile standard.
Python
18
star
18

rdflib-leveldb

A LevelDB-backed RDFLib Store for RDFLib=>6.0
Python
15
star
19

pyTARQL

Python implementation of TARQL
Python
14
star
20

rdflib-rdfjson

RDF/JSON parser and serializer plugins for RDFLib
Python
11
star
21

PyRDFa

Ivan Herman's Python RDFa 1.1 parser, previous version
Python
11
star
22

VocExcel

Converts templated Excel files to SKOS RDF
Python
11
star
23

prez-ui

Vue
10
star
24

rdflib-sqlite

RDFLib Store backed by SQLIte - MOTHBALLED
Python
9
star
25

rdflib-sparqlstore

(discontinued) An RDFLib store around Ivan Herman et al.'s SPARQL service wrapper, with namespace bindings
Python
8
star
26

geosparql-dggs

An implementation of GeoSPARQL's Simple Features SPARQL functions for AusPIX Discreet Global Grid System (DGGS) geometries, as per GeoSPARQL
Python
7
star
27

rdflib-postgresql

RDFLib Store backed by PostgreSQL - MOTHBALLED
Python
7
star
28

profilewiz

A tool for doing things with Profiles
Python
7
star
29

rdflib-benchmark

Benchmarking code + data for rdflib
Python
5
star
30

rdflib-rdfstar

Python
4
star
31

rdflib-sparql-thrift

A BinaryRDF in Thrift SPARQL result parser
Python
4
star
32

timefuncs

Python
3
star
33

rdflib-kyotocabinet

RDFLib Store backed by Kyoto Cabinet (Python2.6+)
Python
3
star
34

rdflib-mysql

RDFLib Store backed by MySQL - MOTHBALLED
Python
3
star
35

rdflib-zodb

RDFLib Store backed by ZODB3 Tests passing but (as at 2021-11-18) work-in-progress w.r.t. being a releasable module.
Python
3
star
36

rdflib-geosparql

An implementation of the GeoSPARQL standard's functions for use with RDFlib Graphs
1
star